• 详解C++ 循环


    1.while循环

    只要给定的条件为真,while 循环语句会重复执行一个目标语句

    C++ 中 while 循环的语法:

    while(condition)
    {
       statement(s);
    }
    
    • 1
    • 2
    • 3
    • 4

    实例:计算 1 加到 100 的值:(while循环版本)

    #include 
    using namespace std;
    
    int main()
    {
        int sum = 0, n = 1;
        while( n <= 100 )
        {
            sum += n;
            n++;
        }
        cout<< sum <<endl;  // 5050
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.for 循环

    for 循环允许您编写一个执行特定次数的循环的重复控制结构

    C++ 中 for 循环的语法:

    for ( init; condition; increment )
    {
       statement(s);
    }
    
    • 1
    • 2
    • 3
    • 4

    实例:计算 1 加到 100 的值:(for循环版本)

    #include 
    using namespace std;
    
    int main()
    {
        int sum = 0;
        for( int a = 1 ; a <= 100 ; a++ )
        {
            sum += a;
        }
        cout<< sum <<endl;  // 5050
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    还能优化吗?

    #include 
    using namespace std;
    
    int main()
    {
        int n = 100;
        cout << n*(n+1)/2 <<endl;  // 高斯求和:5050
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.算法的优劣衡量

    对10亿数据集进行测试:

    #include 
    using namespace std;
    
    int main()
    {
        clock_t startTime,endTime;
        startTime = clock();  // 计时开始
    
        long long sum = 0;
        for( int a = 1 ; a <= 1000000000 ; a++ )
        {
            sum += a;
        }
        cout<< sum <<endl;  // 500000000500000000
    
        endTime = clock();  // 计时结束
        cout<< "程序运行时间:" << endTime - startTime <<endl;  // 程序运行时间:3423
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    使用高斯求和算法:

    #include 
    using namespace std;
    
    int main()
    {
        clock_t startTime,endTime;
        startTime = clock();  // 计时开始
    
        long long n = 1000000000;
        cout << n*(n+1)/2 <<endl;  // 高斯求和:500000000500000000
    
        endTime = clock();  // 计时结束
        cout<< "程序运行时间:" << endTime - startTime <<endl;  // 程序运行时间:0
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.C++11新特性 auto 写法

    for 语句允许简单的范围迭代:

    #include 
    using namespace std;
    
    int main()
    {
        string heiheipapa = "Shuangmu";
        for (auto s : heiheipapa)
        {
            cout<< s <<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5.do…while 循环

    do…while 循环与 while 循环类似,但是 do…while 循环会确保至少执行一次循环

    C++ 中 do…while 循环的语法:

    do
    {
       statement(s);
    
    }while( condition );
    
    • 1
    • 2
    • 3
    • 4
    • 5

    do…while的典型使用场景,用户名密码输入:

    #include 
    using namespace std;
    const int MAX_TRY = 3;
    const string PASSWORD = "xiaofei";
    int main()
    {
        string userinput;
        int try_count = 0;
        do
        {
            cout<< "请输入密码:";
            cin>>userinput;
            if ( userinput == PASSWORD )
            {
                cout<< "aha,hacker!" <<endl;
                break;  // 退出循环
            }
            else
            {
                cout<< "密码错误!" <<endl;
                try_count++;
            }
        }while( try_count < 3 );
        return 0;
    }
    
    • 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

    6.break 语句

    break 语句会停止执行最内层的循环,然后开始执行该块之后的下一行代码

    在这里插入图片描述

    #include 
    using namespace std;
     
    int main ()
    {
       // 局部变量声明
       int a = 10;
    
       // do 循环执行
       do
       {
           cout << "a 的值:" << a << endl;
           a = a + 1;
           if( a > 15)
           {
              // 终止循环
              break;
           }
       }while( a < 20 );
     
       return 0;
    }
    /*
    a 的值: 10
    a 的值: 11
    a 的值: 12
    a 的值: 13
    a 的值: 14
    a 的值: 15
    */
    
    • 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

    7.continue 语句

    continue 会跳过当前循环中的代码,强迫开始下一次循环

    在这里插入图片描述

    实例:模拟点名程序

    hacker不想让自己上课被老师点到,于是他偷偷修改了老师的随机点名代码

    #include 
    using namespace std;
    string NAME_LIST[5] = {"Herbert","Baron","hacker","Darren","Elijah"};
    int main()
    {
        srand((unsigned)time(NULL));  // 随机种子
        for( int i = 0 ; i < 1000 ; i++ )
        {
            int num = rand() % 5;  // 生成 0 - 4 的随机数
            if ( NAME_LIST[num] == "hacker" ) continue;
            cout<< NAME_LIST[num] <<endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8.循环嵌套结构

    一个循环内可以嵌套另一个循环。C++ 允许至少 256 个嵌套层次

    C++ 中 嵌套 for 循环 语句的语法:

    for ( init; condition; increment )
    {
       for ( init; condition; increment )
       {
          statement(s);
       }
       statement(s); // 可以放置更多的语句
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    while 循环, do…while 循环也是可以嵌套使用的

    实例:打印一个简易的乘法口诀表:

    #include 
    using namespace std;
    int main()
    {
        for( int i = 1 ; i <= 10 ; i++ )
        {
            for( int j = 1 ; j <= 10 ; j++)
            {
                cout<< i << "*" << j << "=" << i * j <<endl;
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    实例:暴力找质数:

    #include 
    using namespace std;
    int main()
    {
        for( int i = 2 ; i <= 100 ; i++)
        {
            bool is_prime = true;
            for( int j = 2 ; j < i ; j++)
            {
                if( i % j == 0 )
                {
                    is_prime = false;
                    break;
                }
            }
            if( is_prime == true )
            {
                cout<< i << "是质数" <<endl;
            }
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    Android调试Plugin
    一文带你了解 Linux 的 Cache 与 Buffer
    npm的基础
    【Mysql】where 条件子句之逻辑运算符
    1149 Dangerous Goods Packaging
    After Effects 2023 v23.6
    Qt::绘制框架-选择模式-selectedMode
    Vue 全组件 局部组件
    20 个提升效率的 JS 简写技巧
    netsh int ip 添加/删除 TCP 协议 excludedportrange 的方法
  • 原文地址:https://blog.csdn.net/Gherbirthday0916/article/details/127478259