The IF statement of the choice structure of Java Syntax Foundation and the switch statement detailed _java

Source: Internet
Author: User


Process Control statement:

In the process of executing a program, the order of execution of each statement has a direct effect on the result of the program. That is to say, the process of the program has a direct impact on the running result. Therefore, we must understand the execution process of each statement. Also, many times we have to implement the functions we want to accomplish by controlling the order in which statements are executed.

Process Control Statement Classification:

Sequential structure selection Structure: If statement, switch statement loop structure: While statement, for statement

First, sequential structure:

Is the simplest and most basic process control in the program, there is no specific grammatical structure, according to the sequence of code, in turn, most of the code in the program is executed in this way.

Overall: Written in front of the first execution, written in the back after the execution

Sequential structure diagram:

Second, the choice of structure:

Select structure: Also known as the branching structure. The choice structure has the specific syntax rule, the code must carry on the concrete logical operation to judge, the logical operation result has two, therefore produces the choice, executes the different code according to the different choice.

The Java language provides two types of selection structure statements:

If statement

Switch statement

Third, select the structure of the IF statement:

The IF statement has three different formats. Here is a specific solution.

1, if statement first format: (suitable for a judgment)

Copy Code code as follows:
if (relational expression) {statement body;}

Execution process:

First, determine whether the relationship expression evaluates to TRUE or false: Executes the body of the statement if it is true and does not execute the body of the statement if it is false

Its corresponding flowchart is:


A: A relational expression, whether simple or complex, must result in a Boolean type.

B:if Statement Control of the statement body if it is a statement, curly braces can be omitted, if it is more than one statement, you can not omit. It is recommended that you never omit.

C: Generally speaking: there are no semicolons with left curly braces, and there are no left curly braces with semicolons.

A:If (a==b && a==c) {...} The special Circumstances:

if (a = = b) {} is written as if (A = b) {}

This time if the int type is an error. The meaning of this time is to assign a B to a, leave a to judge, and a is the type int so the error.

But if a,b are all Boolean types, there is no problem.

2, if statement of the second format: (suitable for two kinds of judgment)

if (relational expression) {
statement body 1;
} else {
statement body 2;

Execution process:

First, the relationship expression is judged to see whether the result is true or false: If true, the statement body 1 is executed, and if false, the statement body 2 is executed.

Its corresponding flowchart is:

We talked about the ternary operator, which, based on comparison, gives two results, so the case is similar to the second format of the IF statement, and they should be able to convert to each other in some cases.

The second format and ternary operator of the IF statement:

The operation of ternary operators can be improved by using an if statement, and vice versa.

When is it not tenable? When the statement body that the IF statement controls is an output statement, it is not tenable. Because the ternary operator is an operator, a result must be required to return, and the output statement cannot be returned as a result.

3, if statement third format: (suitable for a variety of judgments)

If statement Third form:
if (relational expression 1) {
       statement body 1;
  } else if (relational expression 2) {
       statement body 2;
  else {
       statement body n+1;

Execution process:

First, judge the relationship expression 1 to see if the result is true or false.

If true, execute the statement body 1

If false, continue to judge the relationship expression 2 to see if the result is true or false

If true, execute the statement body 2

If false, continue to judge the relationship expression ... To see if the result is true or false


If no relational expression is true, the statement body n+1 is executed.

Its corresponding flowchart is:

It should be noted that in all the statement bodies, only one is executed.

For example: The maximum value in three data is obtained by nesting with an if statement. The code is as follows:

Using an If statement to implement
    int max1;
    if (a > B) {
      max1 = A;
    } else {
      max1 = b;
    System.out.println ("Max1:" +max1);

Third, select the structure of the switch statement:

Switch statement format:

switch (expression) {case
     value 1:
      statement body 1;
      Case Value 2:
      statement body 2;
      statement body n+1;


Switch means this is a switch statement

The value of an expression: Byte,short,int,char (JDK5 can be an enumeration later, JDK7 can be string later) (this sentence may be the question)

The case is followed by the value you want to compare the expression to.

The statement body part can be one or more statements

Break indicates interrupt, end meaning, switch statement can be ended

When the default statement indicates that all cases do not match, it executes the content at that point, similar to the else of the IF statement.

Execution process:

First, the value of the expression is calculated;

Second, and case by comparison, once there is a corresponding value, will execute the corresponding statement, in the process of execution, encounter break will end.

Finally, if all the case does not match the value of the expression, the default statement body part is executed, and the program ends.

Flow chart:


(1) After the case can only be a constant, not a variable, and, after multiple case values can not appear the same

(2) Can default be omitted?

can be omitted. Generally not recommended. Unless the value of the judgment is fixed (i.e. all possible cases are already listed in the case)

(3) Break can be omitted?

Can be omitted, generally not recommended. Otherwise the result may not be what you want, there will be a phenomenon: case penetrating.

(4) The position of default must be at the end?

can appear anywhere in the switch statement.

(5) The end condition of the switch statement:

Situation A: ends when you encounter a break, not when you encounter default.

Case B: Execution ends at the end of the program

2, if and switch each use of the scene:

In making judgments, we have two choices, if statements, and switch statements, so how exactly do we choose to use that statement?

If statement use scenario:

For the result is a Boolean type of judgment

For a range of judgments

Judgment of several constant values

Switch statement usage scenarios:

Judgment of several constant values

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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: 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.