Skip to content

Instantly share code, notes, and snippets.

@yxjxx
Created March 8, 2015 12:44
Show Gist options
  • Save yxjxx/8f0f68d7751fd1037be5 to your computer and use it in GitHub Desktop.
Save yxjxx/8f0f68d7751fd1037be5 to your computer and use it in GitHub Desktop.
C语言Switch case语句用法
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a=0,i;
for(i=1;i<5;i++){
switch (i){
case 0:
case 3: //case 3匹配成功之后,因为没有break,其后的语句都会被执行
a += 2;
case 1:
case 2:
a += 3;
default:
a += 5;
}
}
cout << "a = " << a << endl;
return 0;
}
// result: a = 31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment