No, 'i = 2' doesn't evaluate to 2, because this is not a valid comparison-statement (comparison-statements use '=='). So 'i = 2' is actually an an ASSIGNMENT and the if-statement seems to evaluate assignments to 'true', at least in Java.
Jake86954
Posts
-
Missing '=' in if-statement -
Missing '=' in if-statementMy point is, the conditions 'myBool = false' and 'i=2' are both 'seen' as 'true' as far as the if-statement is concerned. In other words : if (myBool = false) { // This line will always be executed. } if (i = 2) { // This line will always be executed, even if i <> 2. // And, in fact, i will now be 2. }
-
Missing '=' in if-statementYes, it just sets the value of 'myBool' to False and after that, the complete statement is evaluated as true.
-
Fun with OperatorsThis is indeed perfectly correct Java-behaviour. I just added this example because less experienced users (who may not even know the difference between i++ and ++i) can be easily tricked by this.
-
Missing '=' in if-statementMissing '=' in if-statement : if (myBool = false) // This if-statement will now always return 'true'. System.out.println("This line will now ALWAYS be executed !"); The correct syntax is, of course : if (myBool == false)
-
Semicolon after if-statementSemicolon after if-statement : int a = 10; if(a == 2); // Bug ! There shouldn't be a semicolon here ! System.out.println("This line will now ALWAYS be executed !");
-
Fun with OperatorsFun with Operators : int I = 6; I += I++; System.out.println("Result : " + I); // I is now 12 int I = 6; I += ++I; System.out.println("Result : " + I); // I is now 13
-
assignment-bugassignment-bug : int myInt = 5; myInt =- 2; // Bug ! myInt is now -2 instead of the expected 3 ! Of course, the correct syntax is : myInt -= 2; // This makes myInt = 3.