C++关键词(三)之流程结构

1. 基本的流程结构

1.1 顺序

代码一步一步的往下执行。

1.2 选择

根据条件,选择不同的代码执行。

1.3 循环

某一条语句会反复的执行。

2.涉及到的关键词

2.1 选择

if else

2.2 循环

for循环 for continue break while循环 while continue break do while 循环 do while continue break

3. 示例代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include  <iostream>

int main(int  argc,char * argv[])
{
    //顺序结构
    std::cout << "Step1: "<<  std::endl;
    std::cout << "Step2: " << std::endl;

    //选择结构
    int nChoice = 0;
    std::cout << "Please Input Your Choice:" << std::endl;
    std::cin>>nChoice;
    if(nChoice % 2 == 0)
    {
        std::cout << "nChoice%2 == 0" << std::endl;
    }
    else
    {
        std::cout << "nChoice%2 @ !=0" << std::endl;
    }

    //循环结构
    //for loop
    for(int i = 0 ; i  < 10 ; i++)
    {
        std::cout << "For Loop " << i  <<std::endl;
    }


    int index = 0;
    while(index   10)
    {
        std::cout << "While Loop "<<  index << std::endl;
        index++;
    }

    int doIndex = 0;
    do
    {
        std::cout << "Do While Loop " << doIndex << std::endl;
        doIndex++;
    }while(doIndex <  10);


    //循环结构 continue
    //for loop
    for(int i = 0 ; i < 10 ; i++)
    {
        if(i %2 == 0)
        {
            continue;
        }
        std::cout << "For Loop "<<  i<<  std::endl;
    }


    int cIndex = 0;
    while(cIndex  < 10)
    {        
        cIndex++;
        if(cIndex %2 == 0)
        {
            continue;
        }
        std::cout << "While Loop "<<  cIndex << std::endl;

    }

    int cDoIndex = 0;
    do
    {   
        cDoIndex++;
        if(cDoIndex%2 == 0)
        {
            continue;
        }
        std::cout  <<"Do While Loop " << cDoIndex<<  std::endl;

    }while(cDoIndex < 10);


   {

        //循环结构 break
        //for loop
        for(int i = 0 ; i <  10 ; i++)
        {
            if(i %2 == 0)
            {
                break;
            }
            std::cout << "For Break Loop " << i  <<std::endl;
        }


        int bIndex = 0;
        while(bIndex  < 10)
        {        
            bIndex++;
            if(bIndex %2 == 0)
            {
                break;
            }
            std::cout << "While Break Loop "<<  bIndex<<  std::endl;

        }

        int bDoIndex = 0;
        do
        {   
            bDoIndex++;
            if(bDoIndex%2 == 0)
            {
                break;
            }
            std::cout  <<"Do While Break Loop " << bDoIndex << std::endl;

        }while(bDoIndex  < 10);
    }
    return 0;
}

4.程序输出

Step1:
Step2:
Please Input Your Choice:
3
nChoice%2 @ !=0
For Loop 0
For Loop 1
For Loop 2
For Loop 3
For Loop 4
For Loop 5
For Loop 6
For Loop 7
For Loop 8
For Loop 9
While Loop 0
While Loop 1
While Loop 2
While Loop 3
While Loop 4
While Loop 5
While Loop 6
While Loop 7
While Loop 8
While Loop 9
Do While Loop 0
Do While Loop 1
Do While Loop 2
Do While Loop 3
Do While Loop 4
Do While Loop 5
Do While Loop 6
Do While Loop 7
Do While Loop 8
Do While Loop 9
For Loop 1
For Loop 3
For Loop 5
For Loop 7
For Loop 9
While Loop 1
While Loop 3
While Loop 5
While Loop 7
While Loop 9
Do While Loop 1
Do While Loop 3
Do While Loop 5
Do While Loop 7
Do While Loop 9
While Break Loop 1
Do While Break Loop 1