4 Program flow structure

C++ supports three basic program execution structures:

  • Sequential structure
  • Choose structure
  • Loop structure

4.1 Structure Selection

4.4.1 if statement

Function: Executes statements that meet the criteria

If statements can be nested with superpositions

One-line if statement

If (condition){condition satisfies the statement that needs to be executed; }

Multi-line format if statement

If (condition){condition satisfies the statement executed}else{condition does not meet the statement executed}

Multiconditional if statement
If (condition) {statement that satisfies condition 1} else if(condition 2) {statement that satisfies condition 1 but does not meet condition 2}... Else {statement that neither satisfies};

Practice case

There are three pigs ABC, please input the weight of three pigs, and judge which one is the heaviest.

#include<iostream>
#include<string>
using namespace std;

class Pig {
    string name;
    int weight;

public:
    Pig() { cout << "Pig constructed." << endl; }
    ~Pig() { cout << "Pig disconstructed." << endl; }

    void setName(string n) {
        name = n;
    }
    void setWeight(int w) {
        weight = w;
    }
    int getWeight() {
        return weight;
    }
    string getName() {
        return name;
    }
};

int main() {
    string name;
    int weight;

    Pig pigA;
    cout << "Please enter the name of pig A: ";
    cin >> name;
    pigA.setName(name);
    cout << "Please enter the weight of pig A: ";
    cin >> weight;
    pigA.setWeight(weight);

    Pig pigB;
    cout << "Please enter the name of pig B: ";
    cin >> name;
    pigB.setName(name);
    cout << "Please enter the weight of pig B: ";
    cin >> weight;
    pigB.setWeight(weight);

    Pig pigC;
    cout << "Please enter the name of pig C: ";
    cin >> name;
    pigC.setName(name);
    cout << "Please enter the weight of pig C: ";
    cin >> weight;
    pigC.setWeight(weight);

    if (pigA.getWeight() >= pigB.getWeight() && pigA.getWeight() >= pigC.getWeight()){
        cout << pigA.getName() << " is the heavieat." << endl;
    }
    else if (pigB.getWeight() >= pigA.getWeight() && pigB.getWeight() >= pigC.getWeight()){
        cout << pigB.getName() << " is the heavieat." << endl;
    }
    else{
        cout << pigC.getName() << " is the heavieat." << endl;
    }

    cin.get();
    return 0;
}

I added output statements to the constructor and parser functions. As you can see, the compiler automatically frees up space without the use of new and delete.

4.1.2 switch statement

Function: Executes a multiconditional branch statement

Grammar:

Switch (expression) {case result 1: expression 1; break; Case result 2: expression 2; break; Case result 3: expression 3; break; Case result 4: expression 4; break; Case result 5: expression 5; break; ... Default: expression 6; }

Note: Without a break, all expressions in subsequent cases will be executed!

Difference between if and switch;

Switch can only determine integers and characters, not ranges

Switch has a clear structure and is more efficient.

Question: Can switch determine C-style or C++ style strings?

Answer: No, but you can determine the enumeration. (Try it at night.)

I tried enumeration and felt good. In fact, enumeration can be used as a set of related constants, which feels good to use.

#include<iostream>
using namespace std;

int main() {
    enum Color{red,green,yellow};

    Color color = red;
    int i = color;
    i++;
    color = Color(i);

    switch (color) {
    case red:
        cout << "The color is red." << endl;
        break;

    case green:
        cout << "The color is green. " << endl;
        break;

    case yellow:
        cout << "The color is yellow. " << endl;
        break;
    }

    return 0;
}

4.2 Cyclic structure

2 the while loop

Function: Satisfy the condition of loop, execute the loop statement

Syntax: while(loop condition){loop statement}

4.2.2 Do while loop

Function: First execute, then execute the statement in a loop

Grammar:

Do {loop statement; }while(loop condition);

PS. Note that this statement requires a semicolon after the while. This is the end of the entire do while statement.

#### 4.2.3 for loop

Action: Satisfy the condition, execute the loop statement

Syntax: for(the starting expression. Conditional expression, last loop body){loop statement}

4.3 Jump statement

Break: Break out of the current loop or switch

Continue: End this loop and proceed to the next loop

Goto: Jump to the location you marked, which can be used to break out of multiple loops

Grammar:

Goto tag; ... Tags:...

Note: Tag names are usually in pure uppercase; The jump should be marked with a colon.