c++语句switch语句_C中的switch语句–第3部分

c++语句switch语句

Read: switch statement in C – Part 2

In the last tutorial I told you about the practical use of switch keyword. As I said earlier a programmer generally use this keyword for menu driven programs. Today I will tell you about the tips and traps while using switch keyword. I will also compare switch with if-else ladder.

So lets start it one by one.

switch statement in C

1. It is not compulsory to make switch-case statement for only integer expression. We can also make it for characters too.


Output


2. We are not bound to write only one statement inside each case. And we can also put multiple conditions inside each case.


Output


In the above program we have excluded the default column. As I said earlier its completely optional. You can also write multiple statements inside each case without worrying about the braces.

3. In case if you write a instruction inside switch case but it does not belong to any case. Then the compiler will skip that instruction at run time.

4. Switch statement can also be used to check the result of particular integer expression. E.g. switch(1*5*0+8*0)

5. You can also write nested switch statement but in practice it is used very rarely.

switch vs if-else

Advantages of using switch statement

1. It is a far better way of writing a program and it also gives well structured way to programs.

2. Switch statements works faster than if-else ladder because while the compilation of program, compiler generally generates a jump table by which it can easily check the answer instead of checking each condition.

Disadvantages of using switch

1. It doesn’t allow to write conditions with operators.

2. It doesn’t allow to write even floating point expression. e.g. switch (0.5) is not allowed.

3. A programmer cannot write multiple cases which will give same result.

case 8:
x=y;
break;

case 7+1:
k=2;
break;

Above code will give an error. Because two cases will give the same result i.e. 8.

翻译自: https://www.thecrazyprogrammer.com/2015/01/switch-statement-in-c-part-3.html

c++语句switch语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 首先,让我们来看一下如何用 C++switch 语句实现猜拳游戏。 ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int player, computer; srand(time(NULL)); cout << "请出拳:1.石头 2.剪刀 3.布" << endl; cin >> player; computer = rand() % 3 + 1; switch(player) { case 1: switch(computer) { case 1: cout << "平局" << endl; break; case 2: cout << "你赢了" << endl; break; case 3: cout << "电脑赢了" << endl; break; } break; case 2: switch(computer) { case 1: cout << "电脑赢了" << endl; break; case 2: cout << "平局" << endl; break; case 3: cout << "你赢了" << endl; break; } break; case 3: switch(computer) { case 1: cout << "你赢了" << endl; break; case 2: cout << "电脑赢了" << endl; break; case 3: cout << "平局" << endl; break; } break; default: cout << "输入错误!" << endl; break; } return 0; } ``` 以上是一个简单的猜拳游戏实现,使用了嵌套的 switch 语句来处理不同的情况。 接下来,让我们看一下如何使用 C++switch 语句实现菜单程序。 ```c++ #include <iostream> using namespace std; int main() { int choice; cout << "请选择您要进行的操作:" << endl; cout << "1.添加新的记录" << endl; cout << "2.修改现有记录" << endl; cout << "3.删除现有记录" << endl; cout << "4.查看所有记录" << endl; cout << "5.退出程序" << endl; cin >> choice; switch(choice) { case 1: cout << "您选择了添加新的记录。" << endl; break; case 2: cout << "您选择了修改现有记录。" << endl; break; case 3: cout << "您选择了删除现有记录。" << endl; break; case 4: cout << "您选择了查看所有记录。" << endl; break; case 5: cout << "您选择了退出程序。" << endl; break; default: cout << "输入错误,请重新选择。" << endl; break; } return 0; } ``` 以上是一个简单的菜单程序实现,使用了 switch 语句来处理用户的不同选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值