Java programming things 32-If statement syntax (2)

Source: Internet
Author: User

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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.