Switch-case
Syntax:
Switch (expression/variable)
{
Case value 1: Statement 1;
Break;
Case value 2: Statement 2;
Break;
Default: Statement 3
Break;
.............................
}
Execution Process
WhenProgramRun switch (expression/variable) to judge the expression in brackets first,
Execute the value 1 after case in braces: If the value matches 1, execute Statement 1,
Run break, jump out of the switch, and then executeCode
If the value 1 does not match, start to judge the value 2,
Match or not. If yes, Statement 2 is executed and break is executed. The switch is jumped out,
If none of them match, run the code after default and then break.
While Loop
Syntax:
Int I = 0;
While (condition/expression) --------------------------------- à I <a certain number
{
Loop body;
I ++;
}
Execution Process:
When running the while (condition) program, you must first determine whether the condition is true.
If true is true, the loop body is executed if false is not true,
And I ++ (I value plus 1)
If not, skip the code after the loop execution.
While, you must first determine whether the condition is true and then execute the loop body,
Do-while first executes the loop body and then judges
While loop example:
Do
{
Loop body;
} While (condition );
Execution Process:
When the program runs to do, it directly enters the loop body;
Then, go to the while (condition) line of code to determine.
If the condition is true, execute the loop body again,
Otherwise, jump out of the loop and execute the subsequent code;
At least one loop body is executed, (Do-while)
It is possible that when the initial value of the loop body (while) does not meet the conditions
Do-while first executes the loop body and then judges
While, you must first determine whether the condition is true and then execute the loop body,
Example of do-while:
Comparison between a while loop and a do-while loop
For Loop
For (expression 1; expression 2; expression 3)
{
Loop body;
}
Execution Process:
When the program runs for (expression 1; expression 2; expression 3)
This line of code
First run to: expression 1
Run expression 2 again
The loop body in the execution;
Execution expression 3;
Execute expression 2 again
Then execute the loop body;
For Loop Example 1:
For Loop Example 2: (you can use F11 for step-by-step testing)
Comparison of while and for loops: