content
I. Introduction
- This chapter mainly learns the basic sequence structure and branch structure in the Java flow control statement.
- Learning Objectives: Proficiency in applying the three if branch formats and switch structures
2. Sequential structure
1. Definition
- The sequence structure is the simplest basic flow control in the program, which is executed in sequence according to the code sequence.
2. Examples
- As shown in the above figure, a basic sequence structure executes the statement, and the output order should be: start->A->B->C->end
Coding implementation :
public static void main(String[] args)
{
System.out.println("开始");
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("结束");
}
Output result :
开始
A
B
C
结束
Three, if format 1
1. Definition
if(条件表达式)
{
语句体;
}
- First evaluate the conditional expression
- true to execute the statement, false to not execute the statement
- Continue to execute the following statement content
2. Examples
- Given the values of data a, b, and c, determine whether a and b, a and c are the same?
Coding implementation :
//定义三个变量
int a=10;
int b=20;
int c=10;
if(a==b)
{
System.out.println("a等于b");
}
if(a==c)
{
System.out.println("a等于c");
}
System.out.println("结束");
Output result :
a等于c
结束
Fourth, if format 2
1. Definition
if (条件)
{
语句体1;
}
else
{
语句体2;
}
...
- First, first determine the value of the if condition
- If true, execute statement body 1
- Otherwise false, execute statement 2
2. Example 1
- Given the values of data a and b, determine whether a is greater than b?
Coding implementation :
//定义两个变量
int a=10;
int b=20;
if(a>b)
{
System.out.println("a大于b");
}
else
{
System.out.println("a不大于b");
}
System.out.println("结束");
Output result :
a不大于b
结束
3. Example 2
- Given an arbitrary number, determine whether it is odd or even?
Coding implementation :
Scanner sc=new Scanner(System.in);//从键盘读入数据
int n=sc.nextInt();
if(n%2==0)
{
System.out.println("数字"+n+"是偶数");
}
else
{
System.out.println("数字"+n+"是奇数");
}
Output result :
10
数字10是偶数
5
数字5是奇数
4. Example 3
- Requirements: Enter the user password on the keyboard, if the password is 666666, the program output password is correct, otherwise the output password is wrong
Coding implementation :
Scanner sc=new Scanner(System.in);//从键盘读入数据
int n=sc.nextInt();
if(n==666666)
{
System.out.println("密码正确,欢迎登录");
}
else
{
System.out.println("密码错误,请重新输入");
}
Output result :
111111
密码错误,请重新输入
666666
密码正确,欢迎登录
Five, if format 3
1. Definition
if (条件1)
{
语句体1;
}
else if (条件2)
{
语句体2;
}
else if (条件3)
{
语句体4;
}
. . .
else
{
语句体n+1;
}
First judge the value of condition 1, if it is true, execute statement body 1, and the branch ends If false, evaluate the value of condition 2 If the value is true , the statement body 2 is executed , and the branch ends If false, evaluate the value of condition 3 ........ If none of the conditions are true , execute the statement body n+1 of the else branch
2. Examples
- The test scores are entered on the keyboard, and the program prints out different reward mechanisms according to the range of the scores.
Coding implementation :
Scanner sc=new Scanner(System.in);//从键盘读入数据
int score=sc.nextInt();
if(score<0 || score>100)//分数区间确定,防止出错
{
System.out.println("输入的成绩有误,请重新输入");
}
else if(score>=95 && score<=100)
{
System.out.println("奖励自行车一辆");
}
else if(score>=90 && score<=94)
{
System.out.println("奖励游乐园游玩");
}
else if(score>=80 && score<=89)
{
System.out.println("奖励变形金刚一个");
}
else
{
System.out.println("奖励胖揍一顿");
}
Output result :
101
输入的成绩有误,请重新输入
98
奖励自行车一辆
92
奖励游乐园游玩
88
奖励变形金刚一个
66
奖励胖揍一顿
Six, switch statement
1. Definition
switch(表达式)
{
case 值1:
执行代码...;
break;
case 值2:
执行代码...;
break;
…
case 值n-1:
执行代码...;
break;
default:
执行代码n;
}
The value of the expression is executed first, and the result after execution matches the switch value When the value of matching case is true , execution starts , and when break is encountered, it jumps out of the switch branch . If none of the values after the case match, execute the default code
2. Example 1
- Input a week number (1~7), output the corresponding week?
Coding implementation :
Scanner sc=new Scanner(System.in);//从键盘读入数据
int week=sc.nextInt();
switch (week)
{
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;
}
Output result :
5
星期五
10
输入的数据有误
3. Example 2
- There are 12 months in a year, which belong to spring, summer, autumn and winter. Enter a month on the keyboard, and use the program to realize which season the month belongs to?
Coding implementation :
Scanner sc=new Scanner(System.in);//从键盘读入数据
int month=sc.nextInt();
switch (month)
{
case 1:
case 2:
case 12:
System.out.println("冬季");
break;
case 3:
case 4:
case 5:
System.out.println("春季");
break;
case 6:
case 7:
case 8:
System.out.println("夏季");
break;
case 9:
case 10:
case 11:
System.out.println("秋季");
break;
default:
System.out.println("输入的月份有误");
break;
}
Output result :
5
春季
8
夏季
10
秋季
12
冬季
13
输入的月份有误
4. Example 3
- The following code input 1, what is the output? What about input 2?
Scanner sc=new Scanner(System.in);//从键盘读入数据 int n=sc.nextInt(); switch (n) { case 1: System.out.println("输出1"); case 2: System.out.println("输出2"); break; case 3: System.out.println("输出3"); break; default: System.out.println("输入的月份有误"); break; }
Output result :
1
输出1
输出2
2
输出2
5. Precautions
- The expression type can only be byte, short, int, char, String, double, float, long are not supported
- The value given by case cannot be repeated and cannot be a variable.
- Remember to write break, otherwise the result of Example 3 will appear