Java programming those things 32-If statement syntax (2) Zhengzhou game college Chen yuefeng from: http://blog.csdn.net/mailbomb
5.3.1.2 if-else statementIf-else statements implement closed conditions, which are more common in programs. The else keyword is "otherwise", that is, the condition is not true. The syntax format of the IF-else statement is as follows: if (conditional expression) Function Code 1; else Function Code 2; syntax description: The preceding part is the same as that of the IF statement, the else part is followed by the Function Code. According to the syntax format, the function code can only have one sentence. Execution sequence: if the condition is true, Function Code 1 in the IF statement is executed; otherwise, Function Code 2 in else is executed. Sample Code: int n = 12; If (N % 2! = 0) system. out. println ("N is an odd number"); else system. out. println ("N is not an odd number"); then, because the value of N % 2 is 0 and the condition is not true, the code of the else statement is executed, and the program outputs "N is not an odd number ". In actual use, to make the structure clear and write multiple lines of code in the functional code section, the syntax format is: If (conditional expression) {function code block} else {function code block} when the program contains multiple if statements, the else statement matches the nearest if statement. Sample Code: If (condition 1) Function Code 1; if (condition 2) Function Code 2; else Function Code 3; then the else statement matches the if statement corresponding to condition 2, the preceding condition 1 is an independent statement. In actual code, braces can be used to make the entire program structure clearer. For an IF-else statement, because the if condition and else condition are mutually exclusive, only the functional code in one statement can be executed in actual execution. In actual development, some companies require to write else even if the else statement does not write code, so that the condition can be closed. This is not a syntax requirement.