if分支结构
-单分支
-双分支
-多分支
if () { }
else if{ }
else{ }
输入整数计算
#include <iostream>
using namespace std;
int main()
{
int a,result;
cout << "The program get a numeber";
cout << "otherwise multiply it by 3 then plus 1\n";
cout << "The number:";
cin >> a;
result = a;
if (a%2 == 1)
result = a*3 +1;
else
result = a/2
cout << "The result is " << result <<"."<<endl;
return 0;
}
输出星期几
switch 分支
sw itch()
{
case 常数表达式 1 :语句序列;
.....
case 常数表达数n : 语句序列;
default : ;
}
嵌套的时候if和else会默认最短距离,需要设置时候可以有{}分层次