C++循环语句与流程控制语句

对命运承诺,对承诺负责。
  • 循环语句

作用:重复执行一段代码

分类

while

do whlie

for

  • while

语法

while(条件表达式)

{

        代码块(循环体)

}

执行流程

先判断条件表达式是否为真,如果为真,执行代码块中的代码;当代码块中循环体执行完毕之后,在判断条件表达式是否为真

如果为真,在执行代码块.....直到表达式为假,退出循环

注意

如果循环体中只有一行代码,此时可以忽略大括号不写

  • 练习1:在控制台输出10以内的数
//输出10以内的数
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
//方案1
// cout << 0 << endl;
// cout << 1 << endl;
// cout << 2 << endl;
// cout << 3 << endl;
// cout << 4 << endl;
// cout << 5 << endl;
// cout << 6 << endl;
// cout << 7 << endl;
// cout << 8 << endl;
// cout << 9 << endl;

//方案2
// int i = 0;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;
// cout << i++ << endl;

//方案3
    int i = 0;
    while(i < 10)
    {
        cout << i++ << endl;
    }
    return 0;
}
  • 练习2:输出100 以内的奇数
//注意:奇数就是对2取余不为0的数
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
//输出100以内的奇数
/*
1,先获取到100以内的数
2,判断获取到的数是否为奇数
*/ 
    int num = 0;
    while(num < 100)
    {
       if(num % 2 != 0)
       {
       cout << num << endl;
       }
       num++;
    }
    return 0;
}
  • 练习3:求100以内数之和
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //求100以内数之和
    //正常算数思想:0+1+2+3+4+..+100
    //变量记录已经计算的数之和
    int sum = 0;
    //变量记录本次要加的数
    int num = 0;
    while(num <= 100)
    {
       sum = sum + num;
       num++;
    }
    cout << "100以内数之和为:" << sum << endl;

    //奥数思想:(首项+尾项) * 项数 / 2;
    int sum2 = (0 + 100) * 101 / 2;
    cout << sum2 << endl;
    return 0;
}
  • 练习4:求10的阶乘
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //求10的阶乘
    //10 * 9 * 8 * 7 * 6 ... * 1
    //定义一个变量记录已经计算的积
    long num = 1;
    //定义一个变量记录本次要乘的数
    int i = 10;
    while(i > 0)
    {
       num = num * i;
       i--;
    }
    cout << "10的阶乘为:" << num << endl;
    return 0;
}
  • 练习5:输出100以内的质数
//质数:只能被1和本身整除的数

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int i = 0;
while(i < 100)
{
   //假设当前i的值是质数
   bool b = true;
   //验证当前i的值是否为质数
   //i % 2 ~ i % i-1,如果这段范围内有一个整除,那么该数就不是质数
   //x记录被除数
   int x = 2;
   while(x < i)
   {
       if(i % x == 0)
       {
           b = false;
       }
       x++;
   }

   if (b)
   {
       cout << i << "是质数" << endl;
   }

   i++;
}
return 0;
}
  • do while

语法

do

{

        循环体

}while(表达式);

执行流程

先执行一次循环体,在判断条件表达式是否为true,如果为true,在此执行循环体,如果为false结束循环

do while 与while

while:先判断,在执行循环体
do while:先执行循环体,在判断条件表达式

面试题

那个循环的循环体至少执行一次?
    do while

  • for

语法

for(表达式1;条件表达式2;表达式3)
{
    循环体4
}

表达式1:初始化变量
表达式2:循环条件
表达式3:改变变量
循环体4:重复执行的代码

执行流程

1,2,4,3,2,4,3,2,4,3...2

面试题

for循环中那个表达式只执行一次?
    表达式1

  • 练习1:随机获取6位数字验证码
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main(int argc, char const *argv[])
{
    //设置随机数种子
    srand(time(NULL));
    for (int i = 0; i < 6; i++)
    {
       //获取随机数
       int num = rand()%10;
       cout << num;
    }
    cout << endl;
    return 0;
}
  • 练习2:随机获取6位验证码,要求有大写字母,小写字母和数字

思路

1,循环6次
2,进入循环后
    2.1:随机生成一个数对3取余
    2.2:定义规则    0    生成1位数字,1    生成小写字母,2    生成大写字母
    2.3:判断本次要生成的内容
        生成数字
            获取一个随机数对10取余
        生成小写字母
            获取一个随机数对26取余 + 97
            将生成的数准换为char
        生成大写字母
            获取一个随机数对26取余 + 65
            将生成的数准换为char

  • 流程控制语句

break:跳出当前循环

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
for(int i  = 0; i < 100; i++)
{
   if (i == 3)
   {
       //跳出当前循环
       break;
   }
   cout << i << endl;
}
return 0;
}

continue:跳出本次循环

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    for(int i = 0; i < 100; i++)
    {   
        if (i == 3)   
        {       
            //跳过本次循环       
            continue;   
        }   
        cout << i << endl;
    }
    return 0;
}
  • goto

作用:跳过

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "1111111" << endl;
    cout << "2222222" << endl;
    goto xxx;
    cout << "3333333" << endl;
    cout << "4444444" << endl;
    xxx:
    cout << "5555555" << endl;
    return 0;
}
  • 循环嵌套

概念:在循环中使用循环

外层管行数,内层管样式

  • 打印各种图形

* * * *
* * * *
* * * *
* * * *

*
* *
* * *
* * * *

* * * *
*     *
*     *
* * * *

*
* *
*   *
*     *
*       *
* * * * * *

  • 鸡兔同笼

目前知道鸡和兔子在同一个笼子中,已知头有32,腿有104
问目前笼子中鸡与兔的数量分别是多少?

思路:(🐓最多多少只)(🐇最多多少只)

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //头32个,脚104只
    int sum = 0;
    //全部是鸡共52只,全部是兔共26只
    for (int i = 0; i <= 26; i++)
    {
        for (int j = 0; j <= 32 ; j++)
        {
            if (((i * 4 + j * 2) == 104) && (i + j ==32))
            {
                sum++;
                cout << "鸡:" << j << "只" << endl;
                cout << "兔:" << i << "只" << endl;
                cout << "------------------------------" << endl; 
            } 
        }
    }
    cout << "共计" << sum << "种方案" << endl;
    return 0;
}
  • 百问百鸡

手上有100文钱,要求买100只鸡
已知:
    小鸡    1文3只
    母鸡    2文1只
    公鸡    3文1只
请问有多少中组合,并说出对应的结果

思路:
    🐥小鸡的数量:100 - 公鸡数 - 母鸡数        
    母鸡的数量:0~50
    🐓公鸡的数量:0~33
    与鸡兔同笼相似,不同的是🐥的数量是3的倍数

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
   int count = 0;
   for(int g = 0; g <= 33; g++)
   {
       for(int m = 0; m <= 50; m++)
       {
           int x = 100 - g - m;
           if (x / 3 + 3 * g + 2 *m == 100 && x % 3 == 0)
           {
               count++;
               cout << "公鸡:" << g << "只" << endl;
               cout << "母鸡:" << m << "只" << endl;
               cout << "小鸡:" << x << "只" << endl;
               cout << "------------------" << endl;
           }

       }
   }
   cout << "共有" << count << "种买法" << endl;
   return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值