JAVA基础篇(09):分支控制结构之---if分支结构

Java分支结构

① If分支结构

if分支结构有如下四种情形(case为布尔表达式,结果为true或false):

一、

if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}

二、

if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else if(条件case2){
	操作option2。//如果满足条件case2,则执行option2
}

三、

if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else{
	操作option2。//如果不满足条件case1,则执行option2
}

四、

if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else if(条件case2){
	操作option2。//如果满足条件case2,则执行option2
}
else {
	操作option3。//如果不满足以上所有条件,则执行option3
}

直接按顺序上栗子:
前情提要:我们现在有两个小老弟,一个叫spring(春),一个叫autumn(秋)。年龄分别是15岁和18岁。
我们考虑如下几种情形:
场景1:我们执行一个判断,如果spring年龄比autumn的大,那我们就输出“spring是autumn的大老哥”,则代码应为:

/*
 * if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
*/
public static void main(String[] args) {//定义方法
		int ageSpring=18,ageAutumn=15;//定义两个变量并赋值
		if(ageSpring> ageAutumn){//条件case1
			System.out.println("spring是autumn的大老哥");//操作option1
		}
	}

上面的代码执行后会输出"spring是autumn的大老哥"。

场景2:我们把条件改为,如果spring年龄比autumn的大,那我们就输出“spring是autumn的大老哥”;如果spring的年龄比autumn的小,就输出“spring是autumn的小老弟”:

/*if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else if(条件case2){
	操作option2。//如果满足条件case2,则执行option2
}*/ 

public static void main(String[] args) {//定义方法
	int ageSpring=18,ageAutumn=15;//定义两个变量并赋值
	if(ageSpring> ageAutumn){//条件case1
		System.out.println("spring是autumn的大老哥");//操作option1
	}
	else if(ageSpring< ageAutumn) {//条件case2
		System.out.println("spring是autumn的小老弟");//操作option2
	}
}

此时执行代码会输出"spring是autumn的大老哥";如果此时我们将ageSpring的值改成比ageAutumn小的数,就会输出"spring是autumn的小老弟"。

场景3:我们把条件改为,如果spring年龄比autumn的大,那我们就输出“spring是autumn的大老哥”;如果不符合上述条件,就输出“spring不比autumn的年龄大”:

/*if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else{
	操作option2。//如果不满足条件case1,则执行option2
}*/

public static void main(String[] args) {//定义方法
		int ageSpring=18,ageAutumn=15;//定义两个变量并赋值
		
		if(ageSpring> ageAutumn){//条件case1
			System.out.println("spring是autumn的大老哥");//操作option1
		}
		else {
			System.out.println("spring不比autumn的年龄大");//如果不满足条件case1(即spring比autumn大),则执行option2		}
	}

此时执行代码会输出"spring是autumn的大老哥";如果此时我们将ageSpring的值改成比ageAutumn小或与之相等的数,就会输出"spring不比autumn的年龄大"。

场景4:我们把条件改为,如果spring年龄比autumn的大,那我们就输出“spring是autumn的大老哥”; 如果spring的年龄比autumn的小,那我们就输出“spring是autumn的小老弟”;以上两种都不符合,则输出“spring和autumn一样大”:

/*if(条件case1){
	操作option1。//如果满足条件case1,则执行操作option1
}
else if(条件case2){
	操作option2。//如果满足条件case2,则执行option2
}
else {
	操作option3。//如果不满足以上所有条件,则执行option3
}*/

public static void main(String[] args) {//定义方法
	int ageSpring=18,ageAutumn=15;//定义两个变量并赋值
	
	if(ageSpring> ageAutumn){//条件case1
		System.out.println("spring是autumn的大老哥");//操作option1
	}
	else if(ageSpring> ageAutumn){//条件case2
		System.out.println("spring是autumn的小老弟");//操作option2
	}
	else {
		System.out.println("spring和autumn一样大");//如果不满足以上所有条件,则执行option3
	}
}

上一篇:JAVA基础篇(08):循环控制结构

下一篇:JAVA基础篇(09):分支控制结构之—if分支结构


软件测试工程师一只,也在不断的学习阶段,平时的小经验不定期分享。
博主经验有限,若有不足,欢迎交流,共同改进~
有意可加Q群 908417285 交流学习。
乾坤未定,你我皆是黑马
原创文章 84 获赞 86 访问量 14万+

猜你喜欢

转载自blog.csdn.net/qq_36396763/article/details/104855731