For some reason, I thought that if the if condition is false, then any operations in it (especially the condition checker) do not affect the further course of the program, but it turned out that this is not so.

static void Main(string[] args) { int x = 6; Console.WriteLine("Начальное значение x = " + x); if (++x == 6) //блок if не должен выполниться т.к. он ложен { Console.WriteLine("++x = " + x); } if (x++ == 7) { Console.WriteLine("x++ = " + x); } Console.WriteLine("Теперь x = " + x); Console.ReadLine(); } 

Result:

 Начальное значение х = 6 х++ = 8 Теперь х = 8 

for some reason it seemed to me logical that if an expression is included in a condition check and the result is false, then it does not seem to change anything.

  • The side effect of the ++ expression does not depend on further checks. The expression itself must be evaluated to obtain the result of the condition, and, accordingly, a side effect will occur. Inheritance of C. - alexlz
  • In my opinion, it is enough just to know what the increment is doing and translate what you are writing into Russian. Compare: int x = 6; if (x + 1 == 7) // true, the most correct option for comparison is // take the sum of X and 1, and compare with 7, and if they are equal to x = 6; if (x ++ == 7) // false // take X (and then increase by 1), compare with 7 and if they are equal to x = 6; if (++ x == 7) // true // increase X by 1, compare the result with 7 and if they are equal. By the way, in the chain and after the first false the calculation is terminated. And this is also explained and just as logical) - Sh4dow
  • Well, the simplest: increment - a function. And if the result of deleting a file is involved in the comparison, is it necessary to return the file for false? live example: -true- -false- if (del (file) and logging_on) log ('file deleted'); - Sh4dow
  • @ Sh4dow A small training programmer belt on the gluteus muscles should solve such problems. If the file needs to be deleted, then why restore? If it cannot be deleted without logging, then first you need to check logging_on ... - alexlz
  • @alexlz, that’s not what I’m talking about, just the author believes that after " if (x++ == 6) " x should remain as it was before the increment. So I gave an analogy with a deleted file, such as it should also remain in place. - Sh4dow

4 answers 4

When the result of an increment operation for something is used, a side effect is obtained. In the if block, it is generally not worth inserting something that has side effects. Either you increase the value of a variable, or you check this value. True, TryParse or TryGetValue all sorts there are quite appropriate in conditional statements, but there the side effect is so syntactically highlighted that you cannot write so “by chance”.

    The expression is evaluated before checking the condition. + you have a hodgepodge with increments

    Equivalent:

     x++; // пре-инкремент ++x, x = 7 bool a = (x == 6); // false if ( a ) // no { Console.WriteLine("++x = " + x); } a = (x == 7); // true x++; // пост-инкремент выполняется после чтения переменной if ( a ) // yes { Console.WriteLine("x++ = " + x); } 

    ZY: I do not rummage in a security, it is the general operations

    • This is exactly the case that says that you need to know the assembler :) - Sh4dow
    • Believe it or not, I know the assembler, and I think that this knowledge has never been useful to me. (Where exactly was not useful? In .NET-development on the desktop and on the phone) - Maxim Kamalov
    • Well, you, pardon me, somehow strangely know, if you can not decompose into atomic operations = / - Sh4dow
    • In my opinion, such things can be understood without knowledge of the assembler. - Modus
    • Such things are strictly described in the language specification (see Ecma-334 and later); the assembler is not required here, and his one is clearly not enough. - AlexeyM

    This code changes the value of x even if the condition is false. This is the increment / decrement mechanism itself.

     if (++x == 6) 

    Or replace with

     if (x == 5) //Если инкремент прибавляет 1 

    Either by decrement else

     else {x--} 

    The principle is that ++ and - in any case is executed. Even if it is in () in the conditional operator. And the code that is executed or not is already indicated in {}

    • Offtopic: it is better to use the second option, it is more correct, so that the increment / decrement as such does not add / subtract one, but chooses the next value of the variable. - nikita_sergeevich February

    if it does not look at any expression in brackets, but only at the result of its execution, he is not interested in whether there are increments, decrements, lambdas or something else inside the expression.

    PS: I can't say that it is more logical here, and here and there there is common sense, you just need to understand the logic of the authors of the language.