一、
与 y=(x>0?1:x<0?-1:0);的功能相同的if语句是:
if (x>0) y=1;
else if(x<0) y=-1;
else y=0;
这道题有一定的迷惑性,有可能有的人会把第二行的else忘记。
二、
若从键盘上输入58,则输出的结果为:585858
#include<iostream.h>
void main()
{ int a;
cin>>a;
if(a>50) cout<<a;
if(a>40) cout<<a;
if(a>30) cout<<a;
}
三、以下程序输出结果是:
#include<iostream.h>
void main()
{
int m=5;
if(m++>5)cout<<m;
else cout<<m--;
}
注意:自增自减是一个类型。
四、
#include<iostream.h>
void main()
{ int x=1,a=0,b=0;
switch(x){
case 0: b++;
case 1: a++;
case 2: a++,b++;
}
cout<<"a="<<a<<",b="<<b;}
原因是因为case后没有添加break,所以程序不会跳出,而是一直循环到最后一行。