One, select structure and if statement
Note: the expression does not add a semicolon a+b, the statement is to add a semicolon a+b;
1. If (expression) statement
if(x>y) cout<<x<<endl;
2. If (expression) statement 1
else statement 2
if(x>y) cout<<x<<endl;
else cout<<y<<endl;
3. if(expression 1) statement 1
else if (expression 2) statement 2
.......
else statement n
if(num>600) cost=0.15;
else if(num>500) cost=0.1;
else cost=0;
Note: else is always paired with the nearest unpaired if on it
4. Conditional operator
Expression 1? Expression 2: Expression 3
max=(a>b)?a:b;
Execute a>b first, if true, assign a to max, if false, assign b to max
2. Multi-branch selection structure (switch statement)
switch(expression) // is numeric or character data
{
case constant expression 1: statement 1
case constant expression 2: statement 2
......
case constant expression n: statement n
default: statement n+1
}
Note: The expression in switch is a key and the expression after case is a door
If it matches, execute it, if there is no break, execute it all the time
The value of each case must be different from each other
Multiple cases can share a set of execution statements. For example, case 1: case2: cout<<"continue";
I personally summarized the above
If there is something worthy of improvement, please leave a message and make progress together!