int a=25; bool b=true; if b { cout<<a<<endl; } 

It gives an error during compilation and requires to put brackets (b) , although the condition is "monosyllabic". If the condition a!=1 && a!=2 question would not arise about brackets, but the condition is simple.

Closed due to the fact that the essence of the question is incomprehensible by the participants avp , AnT , 0xdb , Jarvis_J , Harry Jan 3 at 17:08 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    In short - this is not Pascal) is so necessary here - pavel
  • 2
    @Gleb but in python you have colons, and in lua and паскаль keyword is then . This is how the expression parser is implemented in order to accurately determine the position of the conditions and therefore the syntax requires a bracket. As a result, no contradictions and check easier. - Alex Krass
  • 2
    Well, we, apparently, have different concepts about logic :) Your questions are just an expression of emotions: what a moronic C ++. Because well, I just can’t believe that, after thinking for 5-10 minutes, I won’t be able to find answers to such questions on my own ... Therefore, I advise you to find a language that will not cause such a reaction. You see, according to your reaction, you can see that you are not trying to understand the language, but are looking for a reason not to understand it :) Well at least there wasn’t even a classic "compiler for sure is a mistake - it doesn’t compile my source code!" :) - Harry
  • one
    @Gleb, maybe it will surprise you, the developers of the C language (C ++ then inherited its syntax, but this is a completely different story) did it first of all for themselves . Further, those who liked it began to use and develop it, while maintaining compatibility with previous solutions. - avp
  • 2
    This question should be closed, because nonsense - avp

2 answers 2

You do not forget that the if clause can be written in one line. For example,

 bool b = true; int x = 1; int *p = &x; if ( b ) * p += x; 

If we remove the brackets, we get

 bool b = true; int x = 1; int *p = &x; if b * p += x; 

Which is completely unreadable.

The only thing you can put outside the brackets in the if clause is the constexpr keyword, which became possible with the advent of the C ++ standard 17.

You can find a simple example of using this syntax in my personal forum by clicking the New syntax of the if clause in the C ++ 17 standard and How to invert individual words in an object of the std :: string class

  • even before the brackets it would be possible to arrange the negation of the condition !(bool) , but this is also a compiler error. The more I think about the pros, the more I like the python. - Gleb
  • @Gleb: "The sign of negation" in C and C ++ does not have any special status. This is the same operator in expressions as any other operator. - AnT
  • @AnT but you will agree that sometimes I would like to have access to the inversion of the condition and get it quickly by writing a negation before the condition (in a couple of characters)? Say, the not keyword would not violate the logic of the parser, if it were located so if not (bool) (if such a variant of events was added to the parser), or the negation sign standard in cpp ! . or implement the ifnot (bool) elseifnot (bool) elseif (bool) else branching operator, which would immediately take the negation of the condition. - Gleb
  • Syntactic sugar. if not (condition) and if (!condition) different only in the spelling of the condition and the additional word. And reserved words in c ++ are enough. It's not worth it. - MrBin

Well, as you read:

 if b -- a; //??? 

this :

 if(b) --a; if(b--) a; if (b -- a); 

Which of these expressions did the programmer mean? Not to mention other expressions where operator_characters can be found whose priority is higher, and then you need to read this expression from the beginning, then try to check some condition. It’s just that clever people wrote the language, and the syntax of the instructions was invented perfectly sensible.

  • Again, Просто язык писали умные subjective assessment, not required by the question. Explain why the lack of a keyword ending condition is a smart move? And presence : in the python (based on your words) the decision is not smart people. As already mentioned above, in the absence of an opening bracket immediately after if, the end of the condition could be the first space or a newline character. Thus, without a condition ending keyword and with a short condition from one bool type variable, it would be possible to correctly determine the end of the condition, in my opinion. But this is not implemented, what are the reasons for this? - Gleb
  • in your examples: if (b) --a; --> if b --a; if (b) --a; --> if b --a; if (b--) a; --> if b-- a; if (b -- a); --> if b--a; The thing is gaps. But in a particular case, I'm not talking about two arguments. Re-read the question, it is about the only variable of type bool. (and most often in my experience it is exactly such a construction bool isValue; if isValue {} ) - Gleb
  • you just want to argue and not understand. Your case ... - AR Hovsepyan
  • @ Gleb If you don’t like the syntax of the language, it’s unlikely for you to change this syntax. Try using a different language with a different syntax, but if there is no such possibility (for some reason you need to use crosses), the only way out is to accept. - vegorov