if语句是java中常用的判断语句,基本结构:
if (判断条件){
.... // 输出结果或者进行其他操作
}
一个 if 语句包含一个布尔表达式和一条或多条语句。
实例:
public class Demo{
public static void main(String []args){
int a = 6;
if(a > 0){
System.out.println("a比0大哦");
}
}
}
运行结果
if else 语句
语法:
if(布尔表达式){
//如果布尔表达式的值为true
}else{
//如果布尔表达式的值为false
}
使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。
实例:
public class Demo{
public static void main(String []args){
int a = 6;
if(a < 0){
System.out.println("a不小于0");
}else{
System.out.println("a比0大哦");
}
}
}
运行结果