1. Three structures of the program
Sequential structure
Branch structure
Circular structure
2. Conditional statements
The if statement is one of the most useful control structures. if … else … statement syntax:
if (boolean expression)
Statement for performing an operation
or
if (boolean expression)
Statement for performing an operation
else
Statement for performing an operation
The format of the switch statement:
switch (control expression)
{
case Constant expression 1: statement group 1;
[break;]
case Constant expression 2: statement group 2;
[break;]
……
case Constant expression n: statement group n;
[break;]
[default: statement group n+1;[break;]]]
}
The input parameters in switch() can only be integer or character type (including strings), and cannot be used as real (floating point) numbers.
example:
if ( booleanExpression )
statement-1;
else
statement-2;
3. Circular statements
Syntax format of while loop:
While (condition)
{
Statements that require loop execution;
}
Before explaining the use of while loop, make a comparison with the if statement:
While (condition)
{
Statements that require loop execution;
}
if (condition)
{
A statement executed when the conditions are established;
}
Flowchart of while loop
The syntax structure of do…while loop:
do
{
Statements that require loop execution;
}
while (condition);
for loop
foreach loop: only care about individuals in the set, not the quantity
Application of continue and break in loops
When executing a loop, it may be desirable to exit the loop when the loop body is halfway through execution, rather than the entire loop body is executed and exit until the loop condition is judged. At this time, a keyword --break can be applied.
Continue: Continue, end the current loop, enter the next loop
Break; brake, interrupt. End the entire loop