IT Band of Brothers Grammar Java flow control statements branch structure sentence 1

Regardless of what kind of programming language, it will provide two basic flow control structure: branch structure and loop structure. Wherein the branch structure for realizing a piece of code to selectively performed depending on the condition, the loop structure for implementing the code are repeatedly performed according to a certain cycle conditions. Java also provides the syntax of these two flow control structure, Java provides two kinds of branching statements if and switch, and provides a while, do-while and for three loop statements. In addition, JDK5 also provides a new cycle: foreach loop, can be an easier way to traverse the collection of elements of the array. Java also provides break and continue to control the cyclic structure of the program.

Branch structure sentence

Before introducing branch structure first look at the structure of the order, any programming language is the most common program structure is the order structure. Order from top to bottom row by row configuration program is executed, there is no intermediate judgment, and jump.

If there is no flow control between multiple lines of code in the main method, the program is always executed sequentially downward from above the top surface of the first implementation of the code, in the bottom of the code execution back. This means: If there is no flow control, Java method in the statement is a sequential execution flow, down sequentially from the implementation of each statement.

Java provides two common branch control structures: if and switch statements, wherein if statement use Boolean expressions, or Boolean values ​​controlled as a branch condition; and a plurality of switch statement is used to match an integer value, in order to achieve branch control.

 

1 if conditional statement

Full form if statement is as follows:

if (determination condition) {

    Code block (determination condition is established immediately after execution)

}

If the statement if only one statement in the {}, the code may also be used to simplify the following forms:

if (determination condition) block (immediately after performing the determination condition is satisfied);

If the determination condition expression is true, then execute the code in the if statement in the {}; otherwise, exit if statement, if statement to execute code or rear end of the program. Control if statement determines the conditional expression must generate boolean result. If statement execution process shown in Figure 1.

c307073d955546cfb5f2f9da3a4d6ee4.png

Execution flow statement in Figure 1 if 

 

The following program demonstrates the flow of execution if statement.

public class IfDemo{

    public static void main(String[] args){

         int age = 18;

         char gender = 'man';

         if(age >= 18 && gender == '男'){

              System.out.println ( "This is a grown man");

         }

         if(age <18 && gender == '女'){

              System.out.println ( "This is an underage girl");

         }

    }

}

Compile and execute the program, the console prints information 2 shown in FIG.

bbc82e2992b746a2bed6e3c499eaf905.png

Run Results FIG. 2 IfDemo

 

Determine the condition is true the above program first if statement, it will output "This is a grown man." Then proceed to the second if statement, but judging condition evaluates to false if the second statement, not output "This is a minor girl."

Because only one statement if statement in each block, so that the above procedure may be modified as follows:

public class IfDemo{

    public static void main(String[] args){

         int age = 18;

         char gender = 'man';

         if(age >= 18 && gender == '男')

              System.out.println ( "This is a grown man");

         if(age <18 && gender == '女')

              System.out.println ( "This is an underage girl");

    }

}

Guess you like

Origin www.cnblogs.com/itxdl/p/11261969.html