c++学习笔记——Switch语句

// switch.cpp -- using the switch statement
#include <iostream>
using namespace std;
void showmenu();   // function prototypes
void report();
void comfort();
int main()
{
    showmenu();
    int choice;
    cin >> choice;
    while (choice != 5)
    {
        switch(choice)
        //括号中必须是一个结果是整数的表达式(整数表达式),因为执行Switch语句时,程序将跳到括号中整数值的那一行
        {
            case 1  :   cout << "\a\n";
            //所以每个标签(case labeli)都必须是整数常量表达式。最常见的标签是int或者char常量,也可以是枚举常量。
                        break;
                        //break可以确保只执行Switch语句中的特定部分;break用来终止Switch循环,不再执行下面的case语句。
            case 2  :   report();
                        break;
            case 3  :   cout << "The boss was in all day.\n";
                        break;
            case 4  :   comfort();
                        break;
            default :   cout << "That's not a choice.\n";
            //如果整数表达式不与任何标签匹配,那么程序将跳到标签是default的这一行。
            //default是可选的,如果没有,并且也没有匹配的标签,程序则跳到Switch后面的语句执行
        }
        showmenu();
        cin >> choice;
    }
    cout << "Bye!\n";
    // cin.get();
    // cin.get();
    return 0;
}

void showmenu()
{
    cout << "Please enter 1, 2, 3, 4, or 5:\n"
            "1) alarm           2) report\n"
            "3) alibi           4) comfort\n"
            "5) quit\n";
}
void report()
{
    cout << "It's been an excellent week for business.\n"
        "Sales are up 120%. Expenses are down 35%.\n";
}
void comfort()
{
    cout << "Your employees think you are the finest CEO\n"
        "in the industry. The board of directors think\n"
        "you are the finest CEO in the industry.\n";
}
Please enter 1, 2, 3, 4, or 5:
1) alarm           2) report
3) alibi           4) comfort
5) quit
3
The boss was in all day.
Please enter 1, 2, 3, 4, or 5:
1) alarm           2) report
3) alibi           4) comfort
5) quit
4
Your employees think you are the finest CEO
in the industry. The board of directors think
you are the finest CEO in the industry.
Please enter 1, 2, 3, 4, or 5:
1) alarm           2) report
3) alibi           4) comfort
5) quit
5
Bye!

将枚举常量用作标签

#include <iostream>
enum {red, orange, yellow, green, blue, violet, indigp};

int main()
{
    using namespace std;
    cout << "Enter color for (0-6)" ;
    int code;
    cin >> code;
    //cin无法识别枚举类型,因此这里定义一个整型,
    //当Switch语句将int值和枚举量进行比较时,枚举量将被自动提升为int
    //在while循环测试条件中,也会将枚举量提升为int类型。
    while (code >= red && code <= indigp)
    
    {
        switch (code)
        {
        case red : cout << "Her lips were red.\n";
            break;
        case orange : cout << "Her hair was orange.\n";
            break;
        case yellow : cout << "Her shoes were yellow.\n";
            break;
        case blue : cout << "Her nails were green.\n";
            break;
        case violet : cout << "Her sweatsuit was blue.\n";
            break;
        case indigp : cout << "Her mood was indigo.\n";
            break;
        }
        cout << "Enter color code (0-6): ";
        cin >> code;
    }
    cout << "Bye.\n";
    return 0;

}
Enter color for (0-6)5
Her sweatsuit was blus.

Switch和 if else

switch和if else
Switch 并不是为了处理取值范围而设计的,Switch语句中的每一个case标签必须是一个单独的值。并且这个值必须是整数(包括char),因此Switch无法处理浮点测试。
如果所以的选项都可以用整数常量来标识,这可以使用Switch和if else语句。如果选项超过两个,就代码长度和执行速度而言,Switch语句的效率更高。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值