引用 | 编辑
1234561231
2012-01-07 19:51 |
楼主
▼ |
||
|
引用 | 编辑
kb041204
2012-01-07 22:02 |
1楼
▲ ▼ |
// 其实switch 好像 if...elseif...else
switch(变数) { //记紧开括号!! case 数值: //如果变数是数值时要做的指令 break; case 数值2: //如果变数是数值2时要做的指令 break; case 数值3: //如果变数是数值3时要做的指令 break; // 可以更多 default: //如果变数不是以上全部的数值便要做的指令 break; } //记紧关括号!! x1 |
引用 | 编辑
ebolaman
2012-01-07 23:46 |
2楼
▲ ▼ |
参考
http://caterpillar.onlyfun.net/Gossip/CppGossip/switchStatement.html Switch 就是看例如 switch (a) 括号内的 a 的值 是什么 然后就跑到底下 Case 中对应的数字,假如没有对应的 case 会跑到 default case N 很像是进入点,所以为什么要加 break ? 因为不加 break 程式会继续跑下去,底下的其他 case 中的东西也会被跑到,那就不是预期的结果了 我用两张图来表示 第一,没有在 case 最后加入 break,会发生什么事? 范例一: 复制程式 #include <stdio.h> #include <stdlib.h> int main() { int a = 5; switch (a) { case 5: printf("5 is here\n"); case 6: printf("6 is here\n"); default: printf("default!!\n"); } getchar(); } 就像溜滑梯一样,程式判断 a 是 5,然后就跑到 Case 5: 的进入点 接着就往下滑,case 6 和 default 都会跑到 第二,有加入 break; 范例二: 复制程式 #include <stdio.h> #include <stdlib.h> int main() { int a = 5; switch (a) { case 5: printf("5 is here\n"); break; case 6: printf("6 is here\n"); break; default: printf("default!!\n"); } getchar(); } 那么跑完成 printf("5 is here\n"); 遇到 break; 就会跳出 switch 的结构 不过要不要加 break; 还是要看你要设计什么 x1 |
引用 | 编辑
1234561231
2012-01-08 08:11 |
4楼
▲ ▼ |
了解.另外问大家一个问题按键要怎绑定啊
要用到哪个语法 x0 |
引用 | 编辑
LASER10227
2012-01-13 17:33 |
6楼
▲ |
补充:
若是你在switch statment中任一个case有做declare的话, 请记得每个case都要加上program block {} 例如... switch(n) { case 1: int i; ... break; 这样compiler不会给你过,这所为的cross initialization 因为你宣告的所以变数,在下一个case时lifetime还是存在的,所以要记得 加上大括号{} x0 |