鱼C论坛

 找回密码
 立即注册
查看: 1386|回复: 0

[技术交流] C++旅程第四站——如果有if

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

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

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

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

The desire to reach for the stars is ambitious.The desire to reach hearts is wise and most possible.




                if if if if if if 可惜没if
  1. #include <iostream>

  2. using namespace std;

  3. int main()
  4. {
  5.         int road = 1,house = 2,gulf,breek,desert;
  6.         char season;
  7.        
  8.         //if 表达式中  1-->true  0-->false
  9.         //第一种:只有一个if
  10.         if (road)
  11.         {
  12.                 cout << "road = 1" << endl;
  13.                 road -= 1;
  14.         }
  15.         //表达式中的结果为true-->执行循环体中的内容;否则-->直接跳过if语句
  16.        
  17.         //第二种:if else
  18.         if (house > 0)
  19.                 cout << "explore the paradise" << endl; //表达式为true执行
  20.         else
  21.             cout << "a trip to the paradise" << endl;//表达式为false执行
  22.                
  23.         //第三种:if else if else
  24.         cout << "Enter a word : ";
  25.         cin >> season;
  26.        
  27.         if (season == 'a')//①
  28.             cout << "a novel experience" << endl;//if ①的表达式为true,执行此代码
  29.         else
  30.                   if (season == 'b')//②-----if①的表达式为false,执行此代码
  31.                           cout << "a new perspective" << endl;// ②表达式值为true,执行此代码
  32.                   else
  33.                           cout << "the world city" << endl;//②表达式为false,执行此代码
  34.        
  35.         //条件运算符
  36.         cout << "Enter two integers : ";
  37.         cin >> gulf >> breek;
  38.        
  39.         cout << "The large of " << gulf << " and "  << breek ;
  40.         int rain = gulf > breek?gulf:breek;//rain的值:先判断gulf>breek  true-->执行gulf  false-->执行breek
  41.         cout << " is " << rain << endl;
  42.        
  43.         //switch语句
  44.         cout << "Enter a number(1-4) : ";
  45.         cin >> desert;
  46.        
  47.         switch (desert)//括号中相当于一个标签,会跳转到以下对应的代码;标签-->int char 枚举
  48.         {
  49.                 case 1 :
  50.                         cout << "kicking BeiJing!\n";//desert 为1-->执行此语句
  51.                         break;
  52.                 case 2 :
  53.                         cout << "the oriental pearl!\n";//desert 为2-->执行此语句
  54.                         break;
  55.                 case 3 :
  56.                 case 4 :
  57.                         cout << "intoxicated in Guizhou!\n";//desert 为 3/4-->执行此语句
  58.                         break;
  59.                 default :
  60.                         cout << "a true escape!";
  61.         }
  62.                        
  63.         return 0;
  64. }
复制代码




  渣渣一枚大佬们手下留情
               可惜没if但你有建议

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-15 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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