Process of program execution
(1) sequential structure (default)
(2) Select Structure
If () ...
If () ... Else if () ...
If () ... Else () ...
if () ... else if () ... else () ...
Single branch:
if (boolean expression ) {
condition is true the statement group to execute
}
Dual Branch:
if (boolean expression ) {
condition is true the statement group to execute
}else{
Statement Group to execute if condition is false
}
Multi-branch:
if (boolean expression 1) {
condition 1 is true statement group to execute 1
}else if (boolean expression 2) {
Condition 2 is true statement group to execute 2
}
...
else if (boolean expression n-1) {
The condition n-1 is true to execute the statement group n-1
}else{
all of the above conditions are false statement Group N to execute
}
(2)switch
Switch ( expression ) {
Case constant Expression 1:
Statement Group 1;
[Break;]
.....
Case constant Expression n-1:
Statement Group n-1;
[Break;]
[Default: statement group N;]
}
Note: The expression operation result type can only be a byte short int char jdk1.7 begins to support String.
Break statement function is end Switch of the implementation.
(3) cycle structure
Loop statements are designed to reduce the number of scripts that the program ape repeats to write the same code. Three cycles of design ideas such as
While and Do...while's ideas at a glance the only difference is do ... While at least one loop statement is executed.
The execution flow for a for statement is----------------------------------------iterate when the initialized variable is not a local variable declared in the loop body after the end of the loop The value of the variable is the value that is determined when the statement is not established after iterating over the statement. such as int i; The value of I after the for (i=0;i<5;i++) {} loop ends is 5
Programming must have a good coding habits, alignment, when the method to take advantage of the method of a type of return value type of methods and if () while () the judgment condition is = = when a beginner to the left to compare the variables on the right side will be = = of the bug placed in the compilation period. Interested friends can choose to see the C-Bug and C-trap part of the book, the language is different but the syntax is similar, you can more clearly understand the program in the Ape Eye and the compiler in the eye of the program, so that improve their coding habits less go a lot of detours.
Java Foundation--------Branching statements and looping statements