Three major Process Control
1. Sequential control
2. Branch control
3. Cyclic control
There are three types of branch control
1. Single Branch
if (conditional expression) {
statement;
}
2. Dual Branch
if (conditional expression) {
statement;
}else{
Statement
}
3, multi-branch
if (conditional expression) {
statement;
}else if (conditional expression) {
statement;
}else if (conditional expression) {
statement;
}else{
Statement
}
switch (conditional expression) {
case constant 1:
Span style= "color: #c0c0c0;" > statement 1;
break;
case constant 2:
statement 2;
break;
...
statement n;
break;
default:
statement;
break;
"
//conditional expression data type consistent with the constant type after case;
There are three types of cyclic control
For loop
For (cyclic initial value; loop condition; step) {
Statement
}
While loop
while (loop condition) {
statement;//Loop body
}
While loop is the first judgment, then executes the statement;
Do and loop
do{
statement;//Loop body
}while (cyclic conditions);
The Do While loop is executed first, then judged;
Comprehensive examples
Print the pyramid in the console as follows:
*
* * *
* * *
*******
*********
***********
code as follows:
public static void Main (String args[]) {
int lay=7;
for (int i=1;i<=lay;i++) {
for (int k=1;k<=lay-i;k++) {
System.out.print ("");
}
for (int j=1;j<= (i-1) *2-1;j++) {
System.out.print ("*");
}
System.out.println ();
}
}
Java from getting started to mastering 03-Process Control