Java-preliminary Understanding-Chapter III-Statement-IF format 3

Source: Internet
Author: User

I. In-depth parsing of IF statements

There are so many forms of if statements, in order to deal with different situations, a more detailed division of a domain.

Let's write down the three formats of if

1. First format

if (conditional expression)

{

Execute the statement; // format 1 is a given data to be judged once, the result is true, the operation is within the control of the execution of the statement; if the conditional expression evaluates to False, then exits the If language // sentence, then executes the main function in order The remaining statements in the number. only is true, just come in operation, not true, skip the IF control statement directly .

}

2. Second format

if (conditional expression) // if (conditional expression) There is no semicolon after the reason is that the IF statement is a separate existence of a piece, with its own control range, plus a semicolon, it becomes { // the execution statement of the main function.

Execute the statement;

}

Else

{

Execute the statement; // format 1 is carried out once, really how the case is how to operate; Format 2 is to make two judgments, really how to operate, the case of false how to operate.

}

3. Third format

Make multiple judgments, and the writing form is different from the previous two.

if (conditional expression)

{

Execute the statement;

}

else if (conditional expressions) // If statements can be used in relation to the knowledge points of the set interval, with the conditional expression and else dividing the interval, each interval has corresponding execution

{ // the execution statement. The precondition information falls in that interval and executes which statement. Of course, the premise of the interval, the computer is a step by step to determine

Execute the statement; // , which step is determined, in which step to execute, or exit.

}

...//omitted here is else if, the last one is directly write else

Else

{

Execute the statement;

}

Two. If the third form of parsing

If the third format is composed of multiple blocks of code in the structure, but when run, there can only be one execution, which is a single statement .

Else's appearance, is the negation of the previous, only the previous negation, can be executed to the statement after else.

In the following example, the division of several intervals is problematic, but because the preceding code is executed, it exits the IF statement immediately after execution, even if the subsequent error does not control it.

Make a change to the above procedure, as follows:

Three. Extension of If statement

Many times, not three forms of the IF statement exist purely, but have a nested form.

int x=3,y=2; Nesting is the existence of multiple different judgments that intersect.

To raise a question further, there are six pairs of {} in the above program, which can be omitted.

A: all {} In the program can be omitted, why ? The concept of a single statement is still not understood.

Note: The end of the statement has two types, one is a semicolon and the other is the form of curly braces (curly braces are {}).

Before the curly brace ends, the semicolon ends the IF statement. if (false) is equivalent to no control body.

Why there is an if (false) case, inside why there is false such a conditional statement, very strange, did not read ?

Java-preliminary Understanding-Chapter III-Statement-IF format 3

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.