鱼C论坛

 找回密码
 立即注册
查看: 1538|回复: 2

[技术交流] C++旅程第三站——逃不出来的循环

[复制链接]
发表于 2020-4-22 21:13:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 这是她 于 2020-4-22 21:11 编辑

When everything seems to be going against you,remember that the airplane takes off against the wind,not with it.

循环来喽!!!!!!!!!!!




  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. int main()
  5. {
  6.         int bus;//可以在外面先定义变量
  7.         int track,trolley = 10,coach = 20;
  8.         string station;
  9.        
  10.         for (int bus = 0; bus < 10; bus++)//看这里,也可以在for循环这里定义变量;当条件不满足第二个表达式的时候结束循环;更新用于测试的值。
  11.         {
  12.                 cout << "Look" << endl;
  13.                 cout << "The quantity of bus is : " << bus << endl;
  14.         }//对于for循环,如果是循环体是一条语句-->可以不使用大括号括起来;否则-->要用大括号将循环体括起来
  15.         //花括号括起来的代码块将被视为一条语句
  16.        
  17.         int i = for (int a = 10; a < 0; a--)
  18.                   cout << a;//一定要注意一下不能把for循环赋值给变量
  19.        
  20.         cout << "Please enter the track : " << endl;
  21.         cin >> track;
  22.         for (int railway = 2; railway < 20 ; railway += track)
  23.                 cout << railway << endl;//track是步长,按照步长来进行循环计数的增减
  24.        
  25.         cout << "Enter a station :";
  26.         cin >> station;
  27.        
  28.         for (int b = station.size()-1;b >= 0;b--)//使用for循环访问字符串
  29.              cout << station[b];
  30.         cout << "\nBye.\n";
  31.        
  32.         int taxi[4][5] =
  33.         {
  34.                 {45,75,22,75,84},
  35.                 {36,63,26,17,72},
  36.                 {37,83,85,13,75},
  37.                 {53,35,77,56,67}
  38.         };//初始化二维数组:使用逗号分隔的一维数组初始化用花括号括起来组成
  39.        
  40.         for(int x=0;x < 4;x++)
  41.         {
  42.                 for(int y = 0;y < 5;y++)
  43.                    cout << taxi[x][y] << "\t";
  44.                 cout << endl;
  45.         }//使用嵌套循环-->就是在一个循环里在装上一个循环
  46.        
  47.         while(trolley > 5)//while循环:若表达式为0或false时,跳出循环 ——先判断在执行
  48.         {
  49.                 cout << "The quantity of trolley is : " << trolley << endl;
  50.                 trolley -= 1;
  51.         }
  52.        
  53.         do//先执行循环体,在判断表达式是否满足条件;false——>循环中止
  54.         {
  55.                 cout << coach << endl;
  56.                 coach += 2;
  57.         }while(coach < 32);
  58.        
  59.         return 0;
  60. }
复制代码

渣渣一个,大佬们手下留情
        留个建议,消除霉运

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-23 11:49:14 | 显示全部楼层
“int i = for (int a = 10; a < 0; a--)”这句用的少些,谢谢分享code。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-23 19:25:11 | 显示全部楼层
SugarCane88 发表于 2020-4-23 11:49
“int i = for (int a = 10; a < 0; a--)”这句用的少些,谢谢分享code。

感谢您的建议  但我这个是把for循环赋值给一个变量,下面还有一个cout语句-----这样应该是不可行的吧,望指教
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表