C++选择结构和循环结构、嵌套的控制结构和其他控制语句(if、switch、while、do-while、for、自定义)

**

选择结构:

**

if语句

If语句的语法形式
if (表达式) 语句
例:if (x > y) cout << x;

if (表达式) 语句1 else 语句2
例:

if (x > y) cout << x;
else cout << y;

if (表达式1) 语句1
else if (表达式2) 语句2
else if (表达式3) 语句3

else 语句 n

例2-2输入一个年份,判断是否闰年

#include <iostream>

using namespace std;

int main() {

          int year;

          bool isLeapYear;

          cout << "Enter the year: ";

          cin >> year;

          isLeapYear = ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

          if (isLeapYear)

                   cout << year << " is a leap year" << endl;

          else

                   cout << year << " is not a leap year" << endl;

          return 0;

}
  • 嵌套的if结构
    语法形式
if(   )

   if(   ) 语句 1

   else 语句 2

else

   if(   ) 语句 3

   else 语句 4
  • 注意

    语句 1、2、3、4 可以是复合语句;
    每层的 if 与 else 配对,或用 { } 来确定层次关系。

例2-3:输入两个整数,比较两个数的大小

#include<iostream>

using namespace std;

int main() {

          int x, y;

          cout << "Enter x and y:";

          cin >> x >> y;

          if (x != y)

                   if (x > y)

                             cout << "x > y" << endl;

                   else

                             cout << "x < y" << endl;

          else

                   cout << "x = y" << endl;

          return 0;

}

switch语句


  • 语法形式
   switch  (表达式)

     {  case    常量表达式 1:语句1

        case   常量表达式 2:语句2case   常量表达式 n:语句n

        default :             语句n+1

     }
  • 执行顺序

    以case中的常量表达式值为入口标号,由此开始顺序执行。因此,每个case分支最后应该加break语句。

  • 注意

    case分支可包含多个语句,且不用{ }。
    表达式、判断值都是int型或char型。
    如果若干分支执行内容相同可共用一组语句。

例2-4:输入一个0~6的整数,转换成星期输出

#include <iostream>

using namespace std;

int main() {

     int day;

     cin >> day;

     switch (day) {

     case 0: cout << "Sunday" << endl; break;

     case 1: cout << "Monday" << endl; break;

     case 2: cout << "Tuesday" << endl; break;

     case 3: cout << "Wednesday" << endl; break;

     case 4: cout << "Thursday" << endl; break;

     case 5: cout << "Friday" << endl; break;

     case 6: cout << "Saturday" << endl; break;

     default:

        cout<<"Day out of range Sunday .. Saturday"<< endl;   break;

     }

     return 0;

}

**

循环结构:

**

while语句

  • 语法形式
    while (表达式) 语句

14.png

  • 执行顺序
    先判断表达式的值,若为 true 时,执行语句。

例2-5 求自然数1~10之和

#include <iostream>

using namespace std;

int main() {

  int i = 1, sum = 0;

  while (i <= 10) {

      sum += i;  //相当于sum = sum + i;

      i++;

  }

  cout << "sum = " << sum << endl;

          return 0;

}

do-while

批注 2020-03-03 200928.png

例2-6:输入一个数,将各位数字翻转后输出

#include <iostream>

using namespace std;

int main() {

      int n, right_digit, newnum = 0;

      cout << "Enter the number: ";

      cin >> n;

      cout << "The number in reverse order is  ";

      do {

           right_digit = n % 10;

           cout << right_digit;

           n /= 10;  /*相当于n=n/10*/

      } while (n != 0);

    cout << endl;

      return 0;

}

例2-7用do-while语句编程,求自然数1~10之和

#include <iostream>

using namespace std;

int main() {

      int i = 1, sum = 0;

      do {

           sum += i;

           i++;

      } while (i <= 10);

      cout << "sum = " << sum << endl;

      return 0;

}

对比下面的程序

程序1:

#include <iostream>

using namespace std;

int main() {

  int i, sum = 0;

  cin >> i;

  while (i <= 10) {

    sum += i;

    i++;

  }

  cout<< "sum= " << sum

       << endl;

  return 0;

}

程序2:

#include <iostream>

using namespace std;

int main() {

  int i, sum = 0;

  cin >> i;

  do {

    sum += i;

    i++;

  } while (i <= 10);

  cout << "sum=" << sum

       << endl;

  return 0;

}

for语句

批注 2020-03-03 201858.png 在这里插入图片描述

例2-8:输入一个整数,求出它的所有因子

#include <iostream>

using namespace std;

int main() {

      int n;

      cout << "Enter a positive integer: ";

      cin >> n;

      cout << "Number  " << n << "   Factors  ";

      for (int k = 1; k <= n; k++)

        if (n % k == 0)

          cout << k << "  ";

  cout << endl;

  return 0;

}

运行结果1:

Enter a positive integer: 36

Number  36  Factors  1  2  3  4  6  9  12  18  36 

运行结果2:

Enter a positive integer: 7

Number  7   Factors  1  7  

**

嵌套的控制结构、其他控制语句

**

例2-10 输入一系列整数,统计出正整数个数i和负整数个数j,读入0则结束。

#include <iostream>

using namespace std;

 

int main() {

      int i = 0, j = 0, n;

      cout <<"Enter some integers please (enter 0 to quit):" << endl;

      cin >> n;

      while (n != 0) {

        if (n > 0) i += 1;

        if (n < 0) j += 1;

        cin >> n;

      }

      cout << "Count of positive integers: " << i << endl;

      cout << "Count of negative integers: " << j << endl;

      return 0;

}

其他控制语句
其他控制语句

**

自定义类型

**
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值