C+|C++入門チュートリアル(四)プログラムフロー構造

4027 ワード

4プログラムフロー構造
C++は最も基本的な3種類のプログラム運行構造をサポートする:
  • シーケンス構造
  • 選択構造
  • 循環構造
  • 4.1構造の選択
    4.1.1 if文
    ≪アクション|Action|emdw≫:条件を満たす文の実行
    if文はネストして重畳することができる
    単行フォーマットif文if( ){ ;}
    マルチラインフォーマットif文if( ){ }else{ }
    マルチ条件if文
    if(  )
    {     }
    else if(  2)
    {    1      2   }
    …
    else
    {       };

    練習ケース
    3匹の子豚ABCがいるので、それぞれ3匹の子豚の体重を入力して、誰が一番重いか判断してください.
    #include
    #include
    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;
    }

    コンストラクション関数と解析関数に出力文を追加しました.したがってnewやdeleteを使わないとコンパイラも自動的に空間を解放することがわかる.
    4.1.2 switch文
    作用:多条件分岐文の実行
    構文:
    switch(   )
    {
        case   1:   1;break;
        case   2:   2;break;
        case   3:   3;break;
        case   4:   4;break;
        case   5:   5;break;
        ……
        default:   6;
    }

    注意:breakを付けないと、後のcaseの式はすべて実行されます!
    ifとswitchの違い;
    switchは整数型と文字型しか判断できず、1区間も判断できません
    switchは構造が明確で、実行効率が高い.
    質問:switchはCスタイルまたはC++スタイル文字列を判断できますか?
    答え:だめですが、列挙量を判断できます.(夜はやってみてもいいです)
    列挙してみましたが、いい感じです.実際に列挙は関連定数のセットとして使えるので、使い心地はいいと思います.
    #include
    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循環構造
    4.2.1 whileサイクル
    作用:循環条件を満たし、循環文を実行する
    構文:while( ){ }4.2.2 do whileサイクル
    作用:先に実行して、更に絶えず繰り返して文を実行します
    構文:
    do{
            ;
    }while(      );

    PS.この文はwhile( )以降にセミコロンを付ける必要があることに注意してください.do while文全体の終了に相当します.
    ####4.2.3 forループ
    ≪アクション|Action|emdw≫:条件を満たし、ループ・ステートメントを実行します.
    構文:for( . , ){ }4.3ジャンプ文
    break:現在のループまたはswitchからジャンプ
    continue:このループを終了して次のループに入ります
    goto:タグの位置にジャンプして、多層ループから飛び出すことができます.
    構文:
    goto   ;
    ……
      :
    ……

    注意:タグの名前は一般的に純粋な大文字で書かれています.ジャンプのマークには、コロンを使うべきです.