c++选择语句

<一> if语句

1.一个分支的if语句

语句形式为:if(表达式) 语句;

例:if(x>y)

cout<<x<<endl;

2.两个分支的if-else语句

语句形式为:if(表达式) 语句1;

else语句2;

例:if(x>y) max=x;

else
max=y;

3.if语句的嵌套

语句形式为:if(表达式)
语句1;

           if(表达式) 语句2;

           else语句3;

else 语句4;

例:if(x>0) y=x;

else { if(x<0) y=-x;

    else y=0;

  }

T1 判断三角形

#include

using namespace std;

int main()

{

int a,b,c;

cin>>a>>b>>c;

if(a+b>c&&a+c>b&&b+c>a)

{

if(ab&&bc&&a==c) cout<<“等边三角形”;

if(ab&&a!=c||ac&&a!=b||b==c&&b!=a)
cout<<“等腰三角形”;

if(a!=b&&a!=c&&b!=c) cout<<“一般三角形”;

}

else cout<<“不能形成三角形”;

}

<二>switch语句

语句形式为:switch(表达式)

{case 常量表达式1:语句1;

          case 常量表达式2:语句2;

          ······

          case 常量表达式n:语句n;

          [default:语句n+1]

          }

T2 判断奇偶性

#include

using namespace std;

int main()

{

int a,b;

cin>>a;

b=a%2

switch(b)

{

case 0:cout<<“a为偶数”;

case 1:cout<<“a为奇数”;

}

}

猜你喜欢

转载自blog.csdn.net/qq_43345290/article/details/84927646