Java programming things 31 -- If statement syntax (1)

Source: Internet
Author: User

Java programming those things 31 -- If statement syntax (1) Zhengzhou game institute Chen yuefeng from: http://blog.csdn.net/mailbomb 5.3 condition statementsConditional statements are a type of statements that are selected and executed based on the conditions. In actual use, the difficulty lies in how to accurately abstract conditions. For example, if the user name and password are correct during program logon, the system is displayed. Otherwise, a prompt box such as "Incorrect password" is displayed. This section introduces conditional statements, focusing on syntax explanation and basic usage. For more information, see the following comprehensive examples. In Java, conditional statements mainly have two types of syntax: If statement and switch statement. 5.3.1 if statementThe Chinese meaning of the IF keyword is that, in terms of detailed syntax induction, there are three types: If statement, if-else statement, and if-else statement. 5.3.1.1 if statementThe syntax format of this type of statement is: If (conditional expression) function code. syntax description: If is a keyword in this statement, followed by a pair of parentheses. This pair of parentheses cannot be omitted at any time, the inner of the parentheses is a specific condition. The syntax requires that the expression result be of the boolean type. The code that follows the function, that is, the code that is executed when the condition is set. When writing a program, the code needs to be indented to express the inclusion relationship intuitively. Note: 1. The function code here can only be one line. The function code for multi-line structure will be described later. 2. If (condition expression) generally does not write a semicolon in the future. The execution process of the IF statement is: if the condition expression is true, the function code is executed. If the condition expression is not true, then, the subsequent functional code is not executed. Sample Code: int A = 10; if (a> = 0) system. out. println ("A is a positive number"); if (a % 2 = 0) system. out. println ("A is an even number"); in this sample code, the first condition is to determine whether the value of variable A is greater than or equal to zero. If the condition is true, the output is executed, the second condition is to determine whether variable A is an even number. If it is true, it is output. Note the following code execution process: int M = 20; If (M> 20) m + = 20; system. out. println (m); according to the preceding syntax format, only M + = 20; this line of code is a functional code, and subsequent output statements form an ordered structure with the preceding conditions, therefore, the output result after the program is executed is 20. If the statement to be executed has multiple sentences when the conditions are met, you can use the statement block for expression. The syntax format is as follows: if (condition expression) {function code block ;} in this syntax format, a code block is used to replace the previous functional code. In this way, you can write any number of lines of code within the code block, and make the logic of the entire program clearer, therefore, this logic is recommended in actual code writing.
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.