Program logic, mainly divided into three kinds
Sequential structure: executed from front to rear, each statement executed only once
Branch structure: Select a branch by judging the condition
Loop structure: Loop program Statement branching structure if else
1. if (Boolean expression) {
conditional execution statement
}
2. if () {}else{}
3. if () {}else if () {}
4. if () {}else if () {}else if () {} else{}
Switch
Determine the condition, match the corresponding char,byte,short,int,string (JDK1.7), enum type (JDK1.5)
Note that Boolean expressions cannot be used. cannot be floating number and long, only one slot,4 byte.
Switch (integer | character | enum | String) {
case content: {
execution when content satisfies; break
;
}
Case content: {
Execute when content is satisfied; break
;
}
Case content: {
Execute when content is satisfied; break
;
}
Case content: {
Execute when content is satisfied; break
;
}
Case content: {
Execute when content is satisfied; break
;
}
Default: {
Execute when content is not satisfied;
Attention
You need to add a break, jump out of the branch structure, or the following branch after the match, will output
switch cannot judge Boolean expression, can only judge the content
Loop Structure
When the code needs to be repeated, the loop can be used to control the while loop for the doing while loop
while
while (Boolean expression) {
circular statement;
modify Loop end condition;
}
} for
for (define variable; loop end condition; cyclic condition change) {
circular statement
}
Todo
do{
Circular statement;
Modify the loop end condition;
}while (boolean expression); Perform one first
If you do not know the number of loops with a while loop if you know the number of loops with a while () behind the For loop do, note the semicolon for (int i=0;;;; i++) {}//dead loop, no loop end condition for (; i<3;i++) {}//error, error: symbol I for (int i=0;i<3;) {}//dead loop for (;;) {}//dead loop
Loop ControlContinue exit this cycle, the following does not execute, continue the next cycle break exit the current entire loop, can only exit a
Break out of multiple loops
//1, add loop tag outer:for () {inner:for () {if () {Break:out
ER;
}}//2, add judgment token Boolean flag = FALSE;
for (;&&!flag;) {inner:for () {if () {flag = true;
break; }
}
}