Sequential structure, determining if statement, the switch statement select

Process Control

Outline

During a program execution, the order of execution of the statement of the program is the result of a direct impact. That is, the flow of the program have a direct impact on operating results. So, we have to understand the flow of execution for each statement. And, many times we want to achieve our functions to be performed by the execution order control statements.

Sequence Structure

public static void main(String[] args){
//顺序执行,根据编写的顺序,从上到下运行
	System.out.println(1);
	System.out.println(2);
	System.out.println(3);
}

Judgment statement

if (relational expression) {
statements thereof;
}

  • Implementation process
    • First, determine the relationship between the expression to see the result is true or false
    • If the statement is true on the implementation of the body
    • If false statement is not executed body
public static void main(String[] args){
	System.out.println("开始");
	// 定义两个变量
	int a = 10;
	int b = 20;
	//变量使用if判断
	if (a == b){
		System.out.println("a等于b");
	}
	int c = 10;
	if(a == c){
		System.out.println("a等于c");
	}
	System.out.println("结束");

if (relational expression) {
statement body. 1;
} {the else
sentence 2;
}

  • Implementation process
    • First, determine the relationship between the expression to see the result is true or false
    • If the statement is true on the implementation of 1
    • If false statement on the implementation of 2
public static void main(String[] args){
	// 判断给定的数据是奇数还是偶数
	// 定义变量
	int a = 1;
	if(a % 2 == 0) {
		System.out.println("a是偶数");
	} else{
		System.out.println("a是奇数");
	}
	System.out.println("结束");
}

if (judgment condition 1) {
execute statement 1;
} the else if (judgment condition 2) {
execute statement 2;
}
...
} the else if (n-determination condition) {
execute statement n-;
} {the else
executing statements 1 + n-;
}

  • Implementation process
    • First, determine the relationship between the expression 1 to see the result is true or false
    • If the statement is true on the implementation of 1
    • If it is false to continue to determine the relationship between the expression 2 to see the result is true or false
    • If the statement is true on the implementation of 2
    • If it is false to continue to determine the relationship between the expression ... see the result is true or false
    • If there is no relationship between the expression is true, then the statement is executed body n + 1.
public static void main(String[] args) {
	// x和y的关系满足如下:
	// x>=3 y = 2x + 1;
	//‐1<=x<3 y = 2x;
	// x<=‐1 y = 2x – 1;
	// 根据给定的x的值,计算出y的值并输出。
	// 定义变量
	int x = 5;
	int y;
	if (x>= 3) {
		y = 2 * x + 1;
	} else if (x >=1 && x < 3) {
		y = 2 * x;
	} else {
		y = 2 * x ‐ 1;
	}
	System.out.println("y的值是:"+y);
}

Statement Exercise:

Specifies the test scores to determine student grades
90-100 Excellent
80-89 Good
70-79 Good
60-69 pass
60 or less fail

public static void main(String[] args) {
	int score = 100;
	if(score<0 || score>100){
		System.out.println("你的成绩是错误的");
	}else if(score>=90 && score<=100){
		System.out.println("你的成绩属于优秀");
	}else if(score>=80 && score<90){
		System.out.println("你的成绩属于好");
	}else if(score>=70 && score<80){
		System.out.println("你的成绩属于良");
	}else if(score>=60 && score<70){
		System.out.println("你的成绩属于及格");
	}else {
		System.out.println("你的成绩属于不及格");
	}
}}

if语句和三元运算符的互换

public static void main(String[] args) {
	int a = 10;
	int b = 20;
	//定义变量,保存a和b的较大值
	int c;
	if(a > b) {
		c = a;
	} else {
		c = b;
	}
	//可以上述功能改写为三元运算符形式
	c = a > b ? a:b;
}

选择语句

switch(表达式) {
	case 常量值1:
		语句体1;
		break;
	case 常量值2:
		语句体2;
		break;
		...
	default:
		语句体n+1;
		break;
}
  • 执行流程
    • 首先计算出表达式的值
    • 其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束。
    • 最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。
public static void main(String[] args) {
	//定义变量,判断是星期几
	int weekday = 6;
	//switch语句实现选择
	switch(weekday) {
		case 1:
			System.out.println("星期一");
			break;
		case 2:
			System.out.println("星期二");
			break;
		case 3:
			System.out.println("星期三");
			break;
		case 4:
			System.out.println("星期四");
			break;
		case 5:
			System.out.println("星期五");
			break;
		case 6:
			System.out.println("星期六");
			break;
		case 7:
			System.out.println("星期日");
			break;
		default:
			System.out.println("你输入的数字有误");
			break;
	}
}

switch语句中,表达式的数据类型,可以是byte,short,int,char,enum(枚举),JDK7后可以接收字符串。

  • case的穿透性
    在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。
public static void main(String[] args) {
	int i = 5;
	switch (i){
		case 0:
			System.out.println("执行case0");
			break;
		case 5:
			System.out.println("执行case5");
		case 10:
			System.out.println("执行case10");
		default:
			System.out.println("执行default");
		}
}

上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接运行完整体switch。
由于case存在穿透性,因此初学者在编写switch语句时,必须要写上break。

发布了16 篇原创文章 · 获赞 11 · 访问量 289

Guess you like

Origin blog.csdn.net/weixin_43649997/article/details/103947954