WHCSRL 技术网

C++基础入门4——程序流程结构

  1. int main()
  2. {
  3. //打印10*10正方形点阵
  4. for (int i = 0; i < 10; i++)
  5. {
  6. for (int i = 0; i < 10; i++)
  7. {
  8. cout << "*";
  9. }
  10. cout << endl;
  11. }
  12. return 0;
  13. }

 

1. 选择结构

1)if语句

 实例:

  1. //多条if语句形式
  2. int main()
  3. {
  4. int pig1 = 0;
  5. int pig2 = 0;
  6. int pig3 = 0;
  7. int max = 0;
  8. cout << "请输入三只小猪的体重:" << endl;
  9. cout << "pig1:" << endl;
  10. cin >> pig1;
  11. cout << "pig2:" << endl;
  12. cin >> pig2;
  13. cout << "pig3:" << endl;
  14. cin >> pig3;
  15. if (pig1 >= pig2 && pig1 >= pig3)
  16. {
  17. cout << "体重最重的小猪是小猪1" << endl;
  18. }
  19. else if (pig1 >= pig2 && pig1<= pig3)
  20. {
  21. cout << "体重最重的小猪是小猪3" << endl;
  22. }
  23. else if (pig1 <= pig2 && pig2 <= pig3)
  24. {
  25. cout << "体重最重的小猪是小猪3" << endl;
  26. }
  27. else if (pig1 <= pig2 && pig2 >= pig3)
  28. {
  29. cout << "体重最重的小猪是小猪2" << endl;
  30. }
  31. return 0;
  32. }
  1. //嵌套if语句形式
  2. int main()
  3. {
  4. int pig1 = 0;
  5. int pig2 = 0;
  6. int pig3 = 0;
  7. int max = 0;
  8. cout << "请输入三只小猪的体重:" << endl;
  9. cout << "pig1:" << endl;
  10. cin >> pig1;
  11. cout << "pig2:" << endl;
  12. cin >> pig2;
  13. cout << "pig3:" << endl;
  14. cin >> pig3;
  15. if (pig1 > pig2)
  16. {
  17. if (pig1 > pig3)
  18. {
  19. cout << "小猪1最重" << endl;
  20. }
  21. else
  22. {
  23. cout << "小猪3最重" << endl;
  24. }
  25. }
  26. else
  27. {
  28. if (pig2 > pig3)
  29. {
  30. cout << "小猪2最重" << endl;
  31. }
  32. else
  33. {
  34. cout << "小猪3最重" << endl;
  35. }
  36. }
  37. return 0;
  38. }

2)三目运算符

3)switch语句 

 

  1. //switch语句只能判断整型或者字符型,不能对区间进行判定
  2. int main()
  3. {
  4. int score = 0;
  5. cout << "请输入您的评分:" << endl;
  6. cin >> score;
  7. switch (score)
  8. {
  9. case 5:
  10. cout << "您对电影的评级为优秀" << endl;
  11. break;
  12. case 4:
  13. cout << "您对电影的评级为良好" << endl;
  14. break;
  15. case 3:
  16. cout << "您对电影的评级为不错" << endl;
  17. break;
  18. case 2:
  19. cout << "您对电影的评级为还行" << endl;
  20. break;
  21. case 1:
  22. cout << "您对电影的评级为烂片" << endl;
  23. break;
  24. }
  25. return 0;
  26. }

2. 循环结构

1)while循环语句

 

  1. int main()
  2. {
  3. int RandNumber = rand() %% 100 + 1;//生成一个1到100之间的随机数
  4. cout << "系统给出的随机数字是:" << RandNumber << endl;
  5. int GuessNumber = 0;
  6. cout << "请输入您猜想的数字:" << endl;
  7. cin >> GuessNumber;
  8. while (GuessNumber != RandNumber)
  9. {
  10. if (GuessNumber > RandNumber)
  11. {
  12. cout << "抱歉,您猜测的数字过大。请继续!" << endl;
  13. cout << "请输入您猜想的数字:" << endl;
  14. }
  15. else
  16. {
  17. cout << "抱歉,您猜测的数字过小。请继续!" << endl;
  18. cout << "请输入您猜想的数字:" << endl;
  19. }
  20. cin >> GuessNumber;
  21. }
  22. cout << "恭喜,您猜对了。" << endl;
  23. return 0;
  24. }

2)do...while循环语句

  1. int main()
  2. {
  3. //输出0-9之间的数字
  4. int num = 0;
  5. do
  6. {
  7. cout << num << endl;
  8. num++;
  9. } while (num < 10);
  10. return 0;
  11. }

 

  1. int main()
  2. {
  3. //输出所有的水仙花数
  4. int num = 100;
  5. int num100 = 0;
  6. int num10 = 0;
  7. int num1 = 0;
  8. do
  9. {
  10. num100 = num / 100;
  11. num10 = (num - num100*100)/10;
  12. num1 = num %% 10;
  13. if (num == pow(num100,3) + pow(num10, 3) + pow(num1, 3))
  14. {
  15. cout << num << endl;
  16. }
  17. num++;
  18. } while (num < 1000);
  19. return 0;
  20. }

 

3)for循环语句

 

 

  1. int main()
  2. {
  3. //敲桌子问题
  4. for (int num = 1; num <= 100; num++)
  5. {
  6. if (num %% 10 == 7)
  7. {
  8. cout << "敲桌子" << endl;
  9. }
  10. else if (num / 10 == 7)
  11. {
  12. cout << "敲桌子" << endl;
  13. }
  14. else if (num %% 7 == 0)
  15. {
  16. cout << "敲桌子" << endl;
  17. }
  18. else
  19. {
  20. cout << num << endl;
  21. }
  22. }
  23. return 0;
  24. }

4)嵌套循环

 

 

 

  1. int main()
  2. {
  3. //打印九九乘法口诀表
  4. for (int i = 1; i < 10; i++)
  5. {
  6. for (int j = 1; j <= i; j++)
  7. {
  8. cout << j << "x" << i << "=" << i * j << " ";
  9. }
  10. cout << endl;
  11. }
  12. return 0;
  13. }

 

3. 跳转语句

1)break语句

 2)continue语句

在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环

 

 

3)goto语句

 

推荐阅读