JAVA Foundation-Chapter 3 Process Control Statement

Today's content

if else判断语句
switch选择语句
for循环语句
while循环语句
do while循环语句
跳出语句break,continue

teaching objectives

理解if语句的格式和执行流程
理解if...else语句的格式和执行流程
理解if...else if语句的格式和执行流程
了解if语句和三元运算符互换
理解switch选择语句的格式和执行流程
了解switch选择语句接收的数据类型
了解case的穿透性
理解while语句的格式和执行流程
理解for语句的格式和执行流程
理解do...while语句的格式和执行流程
了解do...while循环的特点
了解跳出语句break,continue的意义
理解嵌套for循环的执行流程

Chapter One Process Control

1. 1 Overview

During the execution of a program, the execution order of each statement has a direct impact on the result of the program. In other words, the flow of the program is

Has a direct impact. Therefore, we must be clear about the execution flow of each statement. Moreover, many times we have to control the execution order of statements to achieve

The function we want to complete.

1. 2 Sequence structure

Chapter Two Judgment Statement

2. 1 Judgment statement 1--if

if语句第一种格式: if

Implementation process

首先判断关系表达式看其结果是true还是false
如果是true就执行语句体
如果是false就不执行语句体
public static void main(String[] args){
//顺序执行,根据编写的顺序,从上到下运行
System.out.println( 1 );
System.out.println( 2 );
System.out.println( 3 );
}
if(关系表达式){
语句体;
}
public static void main(String[] args){
System.out.println("开始");
// 定义两个变量

2. 2 Judgment statement 2--if...else

if语句第二种格式: if...else

Implementation process

首先判断关系表达式看其结果是true还是false
如果是true就执行语句体 1
如果是false就执行语句体 2
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(关系表达式) {
语句体 1 ;
}else {
语句体 2 ;
}

2. 3 Judgment statement 3--if…else if…else

if语句第三种格式: if...else if ...else

Implementation process

首先判断关系表达式 1 看其结果是true还是false
如果是true就执行语句体 1
如果是false就继续判断关系表达式 2 看其结果是true还是false
如果是true就执行语句体 2
如果是false就继续判断关系表达式...看其结果是true还是false
...
如果没有任何关系表达式为true,就执行语句体n+ 1 。
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 (判断条件 1 ) {
执行语句 1 ;
} else if (判断条件 2 ) {
执行语句 2 ;
}
...
}else if (判断条件n) {
执行语句n;
} else {
执行语句n+ 1 ;
}

2. 4 sentence practice

public static void main(String[] args) {
// x和y的关系满足如下:
// x>= 3  y =  2 x +  1 ;
//‐ 1 <=x< 3  y =  2 x;
// x<=‐ 1  y =  2 x –  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);
}

Specify test scores to determine student level

90-100 excellent

80-89 good

70 --79 good

60-69 pass

Fail below 60

2. 5 interchange of if statement and ternary operator

In some simple applications, the if statement can be used interchangeably with the ternary operator.

Chapter 3 Select Statement

3. 1 Selection statement-switch

switch语句格式:
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("你的成绩属于不及格");
}
}}
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;
}

Implementation process

First calculate the value of the expression

其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结
束。
最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。

switch(expression) { case constant value 1: statement body 1; break; case constant value 2: statement body 2; break; default: statement body n + 1; break; }










public static void main(String[] args) { //Define the variable and determine the day of the week int weekday = 6; //switch statement implementation selection switch(weekday) { case 1: System.out.println("Monday") ; break; case 2: System.out.println("Tuesday"); break; case 3:










In the switch statement, the data type of the expression can be byte, short, int, char, enum (enumeration). Strings can be received after JDK 7.

3. 2 case penetration

In the switch statement, if break is not written after the case, a penetration phenomenon will occur, that is, it will not judge the value of the next case, and run directly
until it encounters a break or the overall switch ends.

In the above program, after case 5 is executed, since there is no break statement, the program will always go backwards. It will not judge the case or pay attention to the break, and
run the complete switch directly .

Because of the penetrating nature of case, beginners must write break when writing switch statements.

Chapter 4 Loop Statement

4. 1 Cycle overview

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;
}
}
public static void main(String[] args) {
int i =  5 ;
switch (i){
case  0 :
System.out.println("执行case 0 ");
break;
case  5 :
System.out.println("执行case 5 ");
case  10 :
System.out.println("执行case 10 ");
default:
System.out.println("执行default");
}
}

A loop statement can execute a certain piece of code repeatedly when the loop conditions are met. The code that is repeatedly executed is called a loop body statement.

When executing this loop body, it is necessary to modify the loop judgment condition to false when appropriate to end the loop, otherwise the loop will continue to execute,
forming an endless loop.

4. 2 loop statement 1--for

for循环语句格式:

Implementation process

Execution sequence: ①②③④>②③④>②③④…②Unsatisfied.

①Responsible for completing the initialization of loop variables

②Responsible for judging whether the cycle conditions are met, and if they don’t, they will jump out

③The specific execution statement

④After the loop, the changes of the variables involved in the loop conditions

for(初始化表达式①; 布尔表达式②; 步进表达式④){
循环体③
}
public static void main(String[] args) {
//控制台输出 10 次HelloWorld,不使用循环
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");

Loop exercise: Use loops to calculate the sum of even numbers between 1-100

4. 3 loop statement 2--while

while循环语句格式:

Implementation process

Execution sequence: ①②③④>②③④>②③④…②Unsatisfied.

①Responsible for completing the initialization of loop variables.

②Responsible for judging whether the cycle conditions are met, and if they are not met, they will exit the cycle.

③The specific execution statement.

④After the loop, the change of loop variables.

System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐");
//用循环改进,循环 10 次
//定义变量从 0 开始,循环条件为< 10
for(int x =  0 ; x <  10 ; x++) {
System.out.println("HelloWorld"+x);
}
}
public static void main(String[] args) {
// 1 .定义一个初始化变量,记录累加求和,初始值为 0
int sum =  0 ;
// 2 .利用for循环获取 1 ‐ 100 之间的数字
for (int i =  1 ; i <=  100 ; i++) {
// 3 .判断获取的数组是奇数还是偶数
if(i %  2 == 0 ){
// 4 .如果是偶数就累加求和
sum += i;
}
}
// 5 .循环结束之后,打印累加结果
System.out.println("sum:"+sum);
}

Initialization expression①

while(布尔表达式②){
循环体③
步进表达式④
}

The while loop outputs HelloWorld 10 times

The while loop calculates the sum between 1-100

public static void main(String[] args) {
//while循环实现打印 10 次HelloWorld
//定义初始化变量
int i =  1 ;
//循环条件<= 10
while(i<= 10 ){
System.out.println("HelloWorld");
//步进
i++;
}
}
public static void main(String[] args) {
//使用while循环实现
//定义一个变量,记录累加求和
int sum =  0 ;
//定义初始化表达式
int i =  1 ;
//使用while循环让初始化表达式的值变化
while(i<= 100 ){
//累加求和
sum += i ;
//步进表达式改变变量的值
i++;

4. 4 Loop statement 3--do…while

do...while循环格式

Implementation process

Execution sequence: ①③④>②③④>②③④…②Unsatisfied.

①Responsible for completing the initialization of loop variables.

②Responsible for judging whether the cycle conditions are met, and if they are not met, they will exit the cycle.

③The specific execution statement

④After the loop, the change of loop variables

}

//Print the summed variable

System.out.println(" 1 ‐ 100 的和是:"+sum);
}

Initialization expression①

do{
循环体③
步进表达式④
}while(布尔表达式②);

Print HelloWorld 10 times

The characteristics of the do...while loop: unconditionally execute the loop body once, even if we write the loop condition directly as false, it will still loop once. Such a
loop has a certain risk, so beginners are not recommended to use the do...while loop.

4. 5 The difference between loop statements

public static void main(String[] args) {
int x= 1 ;
do {
System.out.println("HelloWorld");
x++;
}while(x<= 10 );
}
public static void main(String[] args){
do{
System.out.println("无条件执行一次");
}while(false);
}

4. 5 The difference between loop statements

for 和 while 的小区别:
控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继
续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消
失,能够提高内存的使用效率。
在已知循环次数的时候使用推荐使用for,循环次数未知的时推荐使用while。

4. 6 Jump out of the sentence

break

使用场景:终止switch或者循环
在选择结构switch语句中
在循环语句中
离开使用场景的存在是没有意义的

continue

Usage scenario: End this cycle and continue to the next cycle

Chapter 5 Expanding Knowledge Points

5. 1 Endless loop

死循环:也就是循环中的条件永远为true,死循环的是永不结束的循环。例如:while(true){}。
public static void main(String[] args) {
for (int i =  1 ; i<= 10 ; i++) {
//需求:打印完两次HelloWorld之后结束循环
if(i ==  3 ){
break;
}
System.out.println("HelloWorld"+i);
}
}
public static void main(String[] args) {
for (int i =  1 ; i <=  10 ; i++) {
//需求:不打印第三次HelloWorld
if(i ==  3 ){
continue;
}
System.out.println("HelloWorld"+i);
}
}

In the later development, there will be scenes that use endless loops. For example, we need to read the input input by the user, but how much data the user enters, we do not

I don’t know, I can only use the infinite loop. When the user does not want to input data, he can end the loop. How to end an infinite loop?

It's time to jump out of the sentence.

5.2 Nested loop

所谓嵌套循环,是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。总
共的循环次数=外循环次数*内循环次数
嵌套循环格式:

Nested loop execution process:

Execution sequence: ①②③④⑤⑥>④⑤⑥>⑦②③④⑤⑥>④⑤⑥

The outer loop is once and the inner loop is many times.

For example, skip rope: there are 5 groups of jumps, 10 jumps in each group. Five groups are outer loops, and 10 are inner loops.

Exercise: Use nested loops to print a 5 * 8 rectangle

for(初始化表达式①; 循环条件②; 步进表达式⑦) {
for(初始化表达式③; 循环条件④; 步进表达式⑥) {
执行语句⑤;
}
}
public static void main(String[] args) {
// 5 * 8 的矩形,打印 5 行*号,每行 8 个
//外循环 5 次,内循环 8 次
for(int i =  0 ; i <  5 ; i++){
for(int j =  0 ; j <  8 ; j++){
//不换行打印星号
System.out.print("*");
}
//内循环打印 8 个星号后,需要一次换行
System.out.println();
}
}

Guess you like

Origin blog.csdn.net/weixin_43419256/article/details/108198246