JAVA Daily
Department: **Big Data Development Department 6
Name: Qianmo Feixu
Date: 2020.10.24
Note: partly transferred from Baidu and CSDN, invaded and deleted
Outline
1. Control structure
2. Loop structure
Study Daily-Control, Circulation Structure
1. Control structure
1. If statement
if(条件){
执行语句1
}else{
执行语句2
}
if(条件){
执行语句
}else if(条件){
执行语句
}else{
执行语句
}
After the first execution judgment, when the conditions are not satisfied, stop running
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
if (a>b){
System.out.println("a大");
}else if (a == b){
System.out.println("a=b");
}else {
System.out.println("b大");
2、switch
switch(数据类型){
case 结果1:
执行语句
break;
case 结果2:
执行语句
break;
...
default:
执行语句
break;
}
Case has penetration, we use break to suppress penetration
If there is no break, it will run to the next code block
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a的值:");
int a = scanner.nextInt();
System.out.println("请输入b的值:");
int b = scanner.nextInt();
System.out.println("请问求1.a+b\n2.a-b\n请输入对于编号:");
int i = scanner.nextInt();
switch (i){
case 1 :
System.out.println("a+b="+(a + b));
break;
case 2 :
System.out.println("a-b="+(a - b));
break;
}
2. Loop structure
1. The while statement
While is the most basic loop, its structure is:
while( 布尔表达式 ) {
//循环内容
}
Scanner scanner =new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
while (a>b){
System.out.println(a+b);
b++;
}
2、do…while
For the while statement, if the condition is not met, it cannot enter the loop. But sometimes we need to execute at least once even if the conditions are not met.
The do...while loop is similar to the while loop, except that the do...while loop will be executed at least once.
do {
//代码语句
}while(布尔表达式);
Scanner scanner =new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
do {
b++;
System.out.println(a+b);
}while (a>b);
Note : The Boolean expression is after the loop body, so the statement block has been executed before the Boolean expression is detected. If the value of the Boolean expression is true , the statement block is executed until the value of the Boolean expression is false .
3. The for statement
The number of times the for loop is executed is determined before it is executed.
for(初始化; 布尔表达式; 更新) {
//代码语句
}
About the for loop has the following description :
- Perform the initialization step first . One type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement.
- Then, check the value of the Boolean expression. If true, the loop body is executed. If it is false, the loop is terminated and the statement following the loop body is executed.
- After the loop is executed once, the loop control variable is updated.
- Check the Boolean expression again. Repeat the above process.
3.1 Right triangle
for (int i = 1;i <= 4;i++){
for (int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println(" ");
}
3.2 Parallelogram
for (int i = 1; i <= 5;i++){
for (int j = 1;j <= 6-i;j++){
System.out.print(" ");
}
for (int j = 1;j <= 5;j++){
System.out.print("*");
}
System.out.println(" ");
}
3.3 Isosceles (Equilateral) Triangle
for (int i = 1;i <= 4;i++){
for (int j = 1;j <= 5-i;j++){
System.out.print(" ");
}
for (int k = 1;k <= 2*i-1;k++){
System.out.print("*");
}
System.out.println(" ");
}
3.4 Diamond
for (int i = 1;i <= 4;i++){
for (int j = 1;j <= 5-i;j++){
System.out.print(" ");
}
for (int k = 1;k <= 2*i-1;k++){
System.out.print("*");
}
System.out.println(" ");
}
for (int i = 3;i >= 1;i--){
for (int j = 1;j <= 5-i;j++){
System.out.print(" ");
}
for (int k = 5-i ;k <= 3+i;k++){
System.out.print("*");
}
System.out.println(" ");
}
3.5 Nine-Nine Multiplication Table
for (int i = 1;i <= 9;i++){
for (int j = 1;j <= i;j++){
System.out.print(j+"*"+i+"="+(i*j)+"\t" );
}
System.out.println(" ");
4. Break keyword
Break is mainly used in loop statements or switch statements to jump out of the entire statement block.
break Jump out of the innermost loop and continue to execute the statements below the loop.
break;
5. continue keyword
continue applies to any loop control structure. The effect is to make the program immediately jump to the next iteration of the loop.
In the for loop, the continue statement causes the program to immediately jump to the update statement.
In the while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
continue;