//java中的控制语句有三类:分支语句,循环语句,跳转语句
一、分支语句中的if-else语句
/*
* 没有else的if语句语法
* if(条件表达式){
* 语句或块
* }
* 如果条件表达式为true,则执行语句块,如果为false直接跳出
* 语句块中如果只有一个语句,{}可以省略,但为了语法的严谨性和程序的可读性,最好不要省略
*/
int a = 4,b = 3,c;
if (a>b) {
c = a;
a = b;
b = c;
}
System.out.println("a="+a+",b="+b);
/*
* 带有else的if语句语法
* if(条件表达式){
* 语句块1
* }else{
* 语句块3
* }
*如果条件表达式为true,则执行语句块1,如果则执行语句块2
*/
int score = 77;
int score2 = 20;
if (score>=60) {
System.out.println("考试及格了!");
} else {
System.out.println("考试不及格,还需努力!!!");
}
if (score2>=60) {
System.out.println("考试及格了!");
} else {
System.out.println("考试不及格,还需努力!!!");
}
//if语句是可以嵌套使用的
if (x1>x2) {
System.out.println("x1>x2");
} else {
if (x1<x2) {
System.out.println("x1<x2");
} else {
System.out.println("x1=x2");
}
}