C++自学笔记

04 流程结构

本次记录流程结构,还请各位大佬批评指正!

选择结构

    int a;
    cout << "请输入a" << endl;
    cin >> a;
    if (a > 621)
    {
        cout << "1" << endl;
    }
    else if (a == 621)
    {
        cout << "2" << endl;
    }
    else
    {
        cout << "3" << endl;
    }

嵌套语句

  int a;
   cout << "请输入分数a" << endl;
   cin >> a;
   cout << "分数为" << a << endl;
   if (a > 600)
   {
	   cout << "考上一本" << endl;
	   if (a > 700)
	   {
		   cout << "考上QH" << endl;
	   }
	   else if (a > 650)
	   {
		   cout << "考上BD" << endl;
	   }
	   else
	   {
		   cout << "考上FD" << endl;
	   }
   }
   else if (a > 500)
   {
	   cout << "考上二本" << endl;
   }
   else if (a > 400)
   {
	   cout << "考上三本" << endl;
   }
   else
   {
	   cout << "未考上本科" << endl;
   }

三目运算符

返回的是变量,可以继续赋值

   int a = 10;
   int b = 20;
   int c = 0;
   c = a > b ? a : b;
   a > b ? a : b = 100;
   cout << a << endl;
   cout << b << endl;
   cout << c << endl;

switch语句

优点:结构清晰,效率高
缺点:只能判断整型或字符型,不能是区间

   //给电影打分 9~10 7~8 5~6 5以下
   int a;
   cout << "给电影打分" << endl;
   cin >> a;
   cout << "打的分数为" << a << endl;
   switch (a)
   {
	   case 10:
		   cout << "经典" << endl;
		   break; //没有break会一直往下执行
	   case 9:
		   cout << "经典" << endl;
		   break;
	   case 7:
		   cout << "很好" << endl;
		   break;
	   case 6:
		   cout << "很好"  << endl;
		   break;
	   default:
		   cout << "烂" << endl;
		   break;
   }

while循环

   int a = 0;
   while (a < 10)
   {
	   a = a++ ;
	   cout << a << endl;
   }
   //案例 猜数字
   srand(time(NULL)); //更新种子,需要在头文件里包含#include<time.h>
   int num = rand() % 100; //生成0~99之间的真随机数
   //cout << num << endl;
   int a = 0;
   while (a != num)
   {
	   cout << "玩家猜测数字:" << endl;
	   cin >> a;
	   if (a > num)
	   {
		   cout << "玩家猜测的数字过大" << endl;
	   }
	   else if (a < num)
	   {
		   cout << "玩家猜测的数字过小" << endl;
	   }
	   else
	   {
		   cout << "恭喜玩家猜对了数字:" << a << endl;
		   break;
	   }
   }

do…while 循环

   int a = 0;
   do
   {
	   cout << a << endl;
	   a++;
   }
   while (a < 10);

for循环

   int a;
   for (a = 0; a < 10; a++)
   {
	   cout << a  << endl;
   }
   //案例 逢7过 含有7或者7的倍数,打印敲桌子,其余直接打印输出
   int a = 1;
   for (a = 1; a < 101; a++)
   {
	   if (a % 7 == 0|| a % 10==7 || a / 10 % 10==7 )
	   {
		   cout  << "过" << endl;
	   }
	   else
	   {
		   cout << a << endl;
	   }
   }

break、continue

break: 执行到此退出循环
continue: 执行到此就不再执行,执行下一次循环

   //break
   for (int b = 0; b < 10; b++)
   {
	   if (b == 8)
	  {
		   break;
	   }
	   cout << b << endl;
   }
   //continue
   int a;
   for (a = 0; a < 101; a++)
   {
	   if (a % 2 == 0)
	   {
		   continue; //输出奇数
	   }
	   cout << a << endl;
   }

goto

无条件跳转到标记的位置,容易使程序混乱,不建议使用。

   cout << "1.xxxx" << endl;
   cout << "2.xxxx" << endl;
   goto FLAG;
   cout << "3.xxxx" << endl;
   cout << "4.xxxx" << endl;
   FLAG:
   cout << "5.xxxx" << endl;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值