1.条件控制语句
1)if语句
public class Example3_3{
public static void main(String args[]){
int a=9,b=5,c=7,t;
if(a>b){
t=a;a=b;b=t;
}
if(a>c){
t=a;a=c;c=t;
}
if(b>c){
t=b;b=c;c=t;
}
System.out.println("a="+a+",b="+b+",c="+c);
}
}
复合语句如果只有一个语句,可以省略{}
2)if-else语句
3)if语句的扩充形式
if(表达式1)
语句1
else if(表达式2)
语句2
...
else if(表达式n)
语句n
2.switch开关语句
switch(表达式){
case 常量值1:
功能代码1;
break;
case 常量值2:
功能代码2;
break;
……
case 常量值n:
功能代码n;
break;
default:
功能代码;
}
其中break语句是可选的
例:
public class SwitchExample{
public static void main(String args[]){
int x=2,y=1;
switch(x+y){
case 1:
System.out.println(x+y);
break;
case 3:
System.out.println(x+y);
break;
case 0:
System.out.println(x+y);
break;
default:
System.out.println("没有般配的"+(x+y));
}
}
}