Third week
Conditional branch structure: sometimes, not all program statements are executed in sequence. You want to execute this part of the statement when certain conditions are met, and execute another part of the statement when another condition is met. This requires a "conditional branch structure".
1. if statement of conditional branch structure
//First if(expression){sentence} //Second if(expression){sentence} else{sentence} //Third if(expression){sentence} else if(expression){sentence} ...... else{}
- If statements execute expressions in sequence. As long as one of them is true, it will not be executed later.
- If statements can have no else if, no else, or neither.
- If there is only one statement in the statement group, {} may not be used.
- The expression is false with 0 and the rest is true.
- In the nesting of if statements, else is always paired with the nearest if. If you want to change it, you can split it with braces.
- In multiple contradictory conditions, if you really want to execute only one branch, apply if and multiple else if instead of writing multiple if.
2. switch statement of conditional branch structure
switch(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 }
- switch statement can avoid double evaluation of an expression. The value of an expression must be an integer type.
- Constant expressions must be constants of type integer.
- The value of the expression is equal to the value of the constant expression, and the statement group is executed. It does not mean that the default statement is executed.
- Case penetration: after a switch statement enters a case branch, it will be executed until the first "break;" is encountered, Even if this "break;" It is in the later case branch. If there is no "break;", Will be executed down to "}" at the end of the switch statement, and the language group including "default:" will also be executed.
3. for loop of loop structure
for( Expression 1 ;Expression 2;Expression 3) { Statement group }
- Evaluate expression 1.
- Evaluate "expression 2". If its value is true, execute the "expression 2" in "{}"
And then go to 3); If false, "{}" will not be executed again
Statement group in, end of for statement, go to 5). - Evaluate expression 3.
- Go to 2).
- Continue to execute the program after the for statement.
- If the loop control variable is defined in "expression 1" and only works inside for, you don't have to worry about the duplicate name of the control variable.
- Both "expression 1" and "expression 3" in the for loop structure can be
Several expressions connected by commas. - For loops can be nested to form multiple for loops. The number of loops is equal to the product of the number of loops in each layer.
4. while loop of loop structure
while(expression){ Statement group }
- Judge whether "expression" is true. If not, turn to 4)
- Execute statement group
- Turn 1)
- When the while statement ends, continue to execute the statements after the while statement.
- It does not reach the specified number of times, but stops when a certain condition is met
Loop, it is suitable to use the while statement to implement the loop.
5. do... while loop of loop structure
do{ Statement group }while(expression);
- If you want the loop to execute at least once, you can use the do... while statement.
- After each loop is executed, you must determine whether the value of "expression" is true. If it is true, you will continue the loop. If it is false, you will stop the loop.
Procedure questions attached:
1.if nesting
Example: please write a program, which inputs a year, and gives different outputs according to whether the year is the tenth anniversary of the founding of the people's Republic of China, the tenth anniversary of the founding of the Communist Party of China, and whether it is a leap year.
#include<iostream> using namespace std; int main() { int year; cin >> year; if (year<=0) { cout<<"illegal year."<<endl; } else { cout << "legal year." << endl; if ((year > 1949) && (year - 1949) % 10 == 0) { cout << "luck year." << endl; } else if (year > 1921 && !(year - 1921) % 10) { cout << "good year" << endl; } else if (year % 4 == 0 && year % 100 || year % 400 == 0) { cout << "leap year." << endl; } else { cout << "common year." << endl; } } return 0; }
2.switch week
#include <iostream> using namespace std; int main() { int n; cin>>n; switch(n) { case 1: cout<<"Monday"; break; case 2: cout<<"Tuesday"; break; case 3: cout<<"Wednesday"; break; case 4: cout<<"Thursday"; break; case 5: cout<<"Friday"; break; case 6: cout<<"Saturday"; break; case 7: cout<<"Sunday"; break; default: cout<<"Illegal"; } return 0; }
3.for double cycle
Example: given positive integers n and m, take out two different numbers from 1 to n,
So that the sum is a factor of m, how many different choices are there.
#include<iostream> using namespace std; int main() { int sum=0,m, n; cin >> m >> n; for (int i = 1; i < n; i++) { for ( j=i+1; j< n; j++) { if (m % (i + j)==0) sum += 1; } } cout<<sum; return 0; }
4.while
Example: input several (at least one) positive integers not exceeding 100, and output their
The maximum, minimum, and the sum of all numbers in. The last number entered is 0
, marking the end of input.
#include<iostream> using namespace std; int main(){ int Max=0,Min=200,sum=0,number; cin>>number; while(number){ if(number>Max) Max=number; if(number<Min) Min=number; sum+=number; cin>>number; } cout<<Max<<";"<<Min<<";"<<sum<<";"; return 0; }
Example: use Newton iteration method to find the square root of the input number.
To find the square root of a, first guess a value x 1 =a/2 (or any other value) as its square root, then calculate x 2 according to the following iterative formula, and then substitute x 2 into the right side of the formula to calculate x 3... Until the absolute value of the difference between x n and x n+1 calculated for two consecutive times is less than a certain value ε, That is, the square root is considered to be accurate enough. this ε The smaller the value, the more accurate the calculated square root. (eps:epsilon indicates accuracy or absolute error)
Iteration formula: xn+1= (x n + A / x n) / 2
#include<iostream> using namespace std; double EPS 0.001 int main(){ double a; cin>>a; if(a>=0){ double x=a/2,lastX=x+1+EPS; while(x-lastX>EPS||lastX-x>EPS){ lastX=x; x=(x+a/x)/2; } cout<<x; } else cout<<"it can't be nagitive" ; return 0; }
5.do...while
Output integer powers of all 2 from 1 to 10000:
#include<iostream> using namespace std; int main(){ int n=1; do{ cout<<n<<" "; n*=2; }while(n<10000); return 0; }