Process structure
C/C++ supports three basic program running structures: sequential structure, selection structure, and loop structure.
- Sequential structure: the program executes in sequence without jumping
- Selection structure: according to whether the conditions are met, the corresponding functions can be executed selectively
- Loop structure: Loop executes a piece of code multiple times depending on whether the condition is met or not
1. Select Structure
1.1 if statement
Role: Execute conditional statements
Three forms of if statement
-
Single-line formatted if statement
-
Multi-line formatted if statement
-
Multi-conditional if statement
-
Single-line formatted if statement: if (condition) {statement whose condition meets execution}
Example:
int main() { //Select Structure-Single Line if Statement //Enter a score, if greater than 600, to be considered a college entrance and printed on the screen int score = 0; cout << "Please enter a score:" << endl; cin >> score; cout << "The score you entered is: " << score << endl; //if statement //Note: Don't put a semicolon after the if statement if (score > 600) { cout << "I entered a university!!!" << endl; } system("pause"); return 0; }
Note: Do not put a semicolon after the if conditional expression
- if (condition) {condition meets statement executed} else {condition does not satisfy statement executed};
Example:
int main() { int score = 0; cout << "Please enter your test scores:" << endl; cin >> score; if (score > 600) { cout << "I entered a university" << endl; } else { cout << "I didn't attend a university" << endl; } system("pause"); return 0; }
- If statement with multiple conditions: if (condition 1) {condition 1 satisfies the statement being executed} else if (condition 2) {condition 2 satisfies the statement being executed}... Else{Neither of them satisfies the statement being executed}
Example:
int main() { int score = 0; cout << "Please enter your test scores:" << endl; cin >> score; if (score > 600) { cout << "I entered a university" << endl; } else if (score > 500) { cout << "I entered two universities" << endl; } else if (score > 400) { cout << "I went to three universities" << endl; } else { cout << "I did not take an undergraduate course" << endl; } system("pause"); return 0; }
Nested if statement: In if statement, if statement can be nested to achieve more accurate conditional judgment
Case Requirements:
- Prompt the user to enter a college entrance examination score and make the following judgment based on the score
- Scores greater than 600 are considered as one, two for more than 500, three for more than 400, and the rest are considered as not taking an undergraduate course.
- In one score, if more than 700 points, Beida, 650 points, Tsinghua and 600 people's congresses.
Example:
int main() { int score = 0; cout << "Please enter your test scores:" << endl; cin >> score; if (score > 600) { cout << "I entered a university" << endl; if (score > 700) { cout << "I was admitted to Beida" << endl; } else if (score > 650) { cout << "I was admitted to Tsinghua" << endl; } else { cout << "I passed the National People's Congress" << endl; } } else if (score > 500) { cout << "I entered two universities" << endl; } else if (score > 400) { cout << "I went to three universities" << endl; } else { cout << "I did not take an undergraduate course" << endl; } system("pause"); return 0; }
Exercise case: Three piglets weigh*
There are three piglets ABC. Please*enter*the weight of each piglet and*decide which one is the heaviest?
Note that the same situation should also be discussed!!!
#include <iostream> using namespace std; int main21() { /* Exercise case: Three piglets weigh* There are three piglets ABC. Please*enter*the weight of each piglet and*decide which one is the heaviest? */ int pig_one = 0; int pig_two = 0; int pig_three = 0; cout << "Please enter the weight of the first*piglet:" << endl; cin >> pig_one; cout << "Please enter the weight of the second*piglet:" << endl; cin >> pig_two; cout << "Please enter the weight of the third*piglet:" << endl; cin >> pig_three; cout << "The first piglet*s weight is:" <<pig_one<< endl; cout << "The second piglet*s weight is:" <<pig_two<< endl; cout << "The third*piglet's weight is:" << pig_three<<endl; if (pig_one >pig_two){ if (pig_one>pig_three){ cout << "The first*piglet is the heaviest!" << endl; }else if(pig_one < pig_three) { cout << "The third*piglet is the heaviest!" << endl; }else { cout << "The first*piglet and the third*piglet are both the heaviest!" << endl; } }else if(pig_one < pig_two){ if (pig_two > pig_three) { cout << "The second*piglet is the heaviest!" << endl; }else if(pig_two < pig_three) { cout << "The third*piglet is the heaviest!" << endl; }else { cout << "*The second*piglet and the third*piglet are both the heaviest!" << endl; } }else { if (pig_one == pig_three) { cout << "Three piglets weigh the same weight! -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- *" << endl; }else if (pig_one > pig_three) { cout << "*The first*piglet and the second*piglet are both the heaviest!" << endl; }else { cout << "The third little piglet is the heaviest one! -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- *" << endl; } } system("pause"); return 0; }
1.2 Trinomial Operator
Role: Simple judgment through trinomial operators
Syntax: Expression 1? Expression 2: Expression 3
Explanation:
If the value of expression 1 is true, execute expression 2 and return the result of expression 2.
If the value of expression 1 is false, execute expression 3 and return the result of expression 3.
Example:
int main() { int a = 10; int b = 20; int c = 0; c = a > b ? a : b; cout << "c = " << c << endl; //Trinomial operators in C++ return variables and can continue to assign values (a > b ? a : b) = 100; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; system("pause"); return 0; }
Summary: Compared with if statements, trinomial operators have the advantages of short, neat, and the disadvantage is that they are not clear in structure if they are nested
1.3 switch statement
Role: Execute multi-conditional branch statements
Grammar:
switch(Expression) { case Result 1: Execute statement;break; case Result 2: Execute statement;break; ... default:Execute Statement;break; }
Example:
int main() { //Please rate the movie //10~9 Classic // 8 ~ 7 is very good // 6~5 General // Loose slices under 5 minutes int score = 0; cout << "Please rate the movie" << endl; cin >> score; switch (score) { case 10: case 9: cout << "Classic" << endl; break; case 8: cout << "Very nice" << endl; break; case 7: case 6: cout << "commonly" << endl; break; default: cout << "Rotten slices" << endl; break; } system("pause"); return 0; }
Note that expression types in 1:switch statements can only be integer or character
Note that if there is no break in 2:case, the program will go down all the way
Summary: Compared with if statement, switching has clear structure, high execution efficiency and disadvantage that switching cannot judge intervals
2 Cycle structure
2.1 while Loop Statement
Role: satisfy the loop condition, execute the loop statement
Syntax: while {loop statement}
Interpretation: As long as the result of the loop condition is true, the loop statement is executed
Example:
int main() { int num = 0; while (num < 10) { cout << "num = " << num << endl; num++; } system("pause"); return 0; }
Note: When executing a loop statement, the program must provide an exit to jump out of the loop, otherwise a dead loop will occur
White Loop Practice Case: Guess Numbers
Case description: The system randomly generates a number between 1 and 100. The player guesses. If the guess is wrong, it prompts the player that the number is too large or too small. If the guess is right, congratulate the player on winning and quit the game.
#include <iostream> using namespace std; #include <ctime> int main() { /* The system randomly generates a number between 1 and 100, and the player guesses. If the guess is wrong, it tells the player that the number is too big or too small. If you guess right, congratulate the player on winning and quit the game. */ // Add a random number seed: Use the current system time to generate a random number, preventing every random number from being the same srand( (unsigned int) time(NULL) ); // Generate 1-100 random numbers int a = rand() % 100 + 1; int b = 0; while (true) { cout << "Enter the number you guessed:" << endl; cin >> b; if (a > b) { cout << "Your guess is too small!" << endl; continue; } else if(a<b) { cout << "You guessed too much!" << endl; continue; } else { cout << "Congratulations on your guess!" << endl; break; } } system("pause"); return 0; }
2.2 do...while loop statement
Role: satisfy the loop condition, execute the loop statement
Syntax: do{loop statement} while (loop condition);
Note: The difference with while is that do...while executes a loop statement before determining the loop condition
Example:
int main() { int num = 0; do { cout << num << endl; num++; } while (num < 10); system("pause"); return 0; }
Summary: The difference with a while loop is that do...while executes a loop statement once and then determines the condition of the loop
Exercise case: Number of daffodils
Case description: Narcissus number refers to a three-digit number whose sum of the three powers of the numbers on each digit equals itself
For example: 1^3 + 5^3 + 3^3 = 153
Please use do...while statement to find the number of daffodils in all 3 digits
#include <iostream> using namespace std; int main() { /* The number of daffodils is a three-digit number whose sum of the three powers of the numbers in each digit equals itself For example: 1^3 + 5^3 + 3^3 = 153 Please use do...while statement to find the number of daffodils in all three digits */ int number = 100; int count = 0; do { int a = number % 10; // Number of digits int b = (number / 10) % 10; // Ten digits int c = number / 100; // hundreds digit if ((a * a * a+ b * b * b + c * c * c) == number) { cout << "Narcissistic number:" << number << endl; count++; } number++; } while (number<1000); system("pause"); return 0; }
2.3 for loop statement
Role: satisfy the loop condition, execute the loop statement
Syntax: for (start expression; conditional expression; end loop body) {loop statement;}
Example:
int main() { for (int i = 0; i < 10; i++) { cout << i << endl; } system("pause"); return 0; }
Details:
Note: The expressions in the for loop are separated by semicolons
Summary: while, do...while, for are commonly used looping statements in development. For looping structure is clear and common
Practice case: tapping on the table
Case description: From the beginning to the number 100, if the number contains 7, or the number 10 contains 7, or the number is a multiple of 7, we print tapping on the table and the rest of the numbers are printed out directly.
#include <iostream> using namespace std; int main() { /* Case description: From the beginning to the number 100, if the number digits contain 7, or the number ten contains 7, or if the number is a multiple of 7, We print a tap on the table and the rest of the numbers are printed out directly. */ for (int i = 1; i <= 100; i++) { int a = i % 10; // Number of digits int b = i / 10; // Ten digits int c = i % 7; // Is it a multiple of 7 if (a == 7 || b == 7 || c == 0) { cout << "Knock on the table" << endl; continue; } cout << i << endl; } system("pause"); return 0; }
2.4 Nested Loop
What it does: Nest another layer of loop inside the loop to solve some practical problems
For example, if we want to print the following pictures on the screen, we need to use nested loops
Example:
int main() { //Outer loop once, inner loop once for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { cout << "*" << " "; } cout << endl; } system("pause"); return 0; }
Practice case: Multiplication trick table
Case Description: Using Nested Loops to Implement the Nine-Nine Multiplication Table
#include <iostream> using namespace std; int main() { /* Nested loop What it does: Nest another layer of loop inside the loop to solve some practical problems For example, if we want to print the following pictures on the screen, we need to use nested loops Case Study: Using Nested Loops to Implement a Nine-Nine Multiplication Table */ for (int i = 1; i < 10; i++) { //cout << i << endl; // endl wrap for (int j = 1; j <= i ; j++) { cout <<j<<"x"<<i<<"="<<i*j<<"\t" ; } cout << endl; } system("pause"); return 0; }
3 Jump Statement
3.1 break statement
Role: Used to jump out of a selection structure or circular structure
When to use break:
- Appears in the switch conditional statement to terminate the case and jump out of the switch
- Appears in a loop statement to jump out of the current loop statement
- Appears in a nested loop, jumping out of the most recent inner loop statement
Example 1:
int main() { //1. Use break in switch statements cout << "Please select the difficulty of your challenge copy:" << endl; cout << "1,ordinary" << endl; cout << "2,secondary" << endl; cout << "3,difficulty" << endl; int num = 0; cin >> num; switch (num) { case 1: cout << "You chose the normal difficulty level" << endl; break; case 2: cout << "You chose Medium Difficulty" << endl; break; case 3: cout << "You chose Difficulty" << endl; break; } system("pause"); return 0; }
Example 2:
int main() { //2. Use break in looping statements for (int i = 0; i < 10; i++) { if (i == 5) { break; //Jump out of loop statement } cout << i << endl; } system("pause"); return 0; }
Example 3:
int main() { //Exit inner loop by using break in nested loop statement for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (j == 5) { break; } cout << "*" << " "; } cout << endl; } system("pause"); return 0; }
3.2 continue statement
Role: In a loop statement, skip the remaining statements that have not been executed in this loop and continue with the next loop
Example:
int main() { for (int i = 0; i < 100; i++) { if (i % 2 == 0) { continue; } cout << i << endl; } system("pause"); return 0; }
Note: continue does not terminate the entire loop, break will jump out of it
3.3 goto statement
Role: Can jump statement unconditionally
Syntax: goto tag;
** Interpretation: ** If the name of the tag exists, it will jump to the location of the tag when the goto statement is executed
Example:
int main() { cout << "1" << endl; goto FLAG; cout << "2" << endl; cout << "3" << endl; cout << "4" << endl; FLAG: cout << "5" << endl; system("pause"); return 0; }
Note: goto statements are not recommended in programs to avoid confusing program flow
Reference resources: https://www.bilibili.com/video/BV1et411b73Z?p=15&t=271