Java process control 02 -- sequence structure and selection structure
Sequential structure
-
The basic mechanism of Java is the sequential structure. Unless otherwise specified, it will be executed sentence by sentence in order;
-
Sequential structure is the simplest algorithm structure;
-
Between statements and between boxes, it is carried out in the order from top to bottom. It is composed of several processing steps executed in turn. It is a basic algorithm structure that any algorithm is inseparable from.
package com.studyhao1999.struct; public class ShunXuDemo { public static void main(String[] args) { System.out.println("hello1"); System.out.println("hello2"); System.out.println("hello3"); System.out.println("hello4"); System.out.println("hello5"); System.out.println("hello6"); } }
Select structure
- if single selection structure
- if double selection structure
- if multiple selection structure
- Nested if structure
- switch multiple selection structure
if single selection structure
- We often need to judge whether something is feasible before we execute it. Such a process is represented by an if statement in the program
- Syntax:
if(Boolean expression){ //The statement that will be executed if the Boolean expression is true }
- For example: enter a string to see if it is the same as Hello. If it is the same, Hello will be output. If it is not equal, it will not be output
package com.studyhao1999.struct; import java.util.Scanner; public class IfDemo01 { public static void main(String[] args) { Scanner scanner =new Scanner(System.in);//The scanner variable is waiting for input from the system System.out.println("Please enter a content"); String s = scanner.nextLine();//scanner adopts the input mode of nextline and assigns the input content to s //equals: determines whether the strings are equal if(s.equals("Hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); } }
if double selection structure
-
Now there is a demand. The company wants to buy a software. If it succeeds, it will pay 1 million yuan. If it fails, it will find someone to develop it by itself. Such a requirement cannot be met with an if. We need to have two judgments and a double selection structure, so we have an if else structure
-
Syntax:
if(Boolean expression){ //If the Boolean expression has a value of true }else{ //If the value of the Boolean expression is false }
-
For example: if the test score is greater than 60, you will pass, and if it is less than 60, you will fail
package com.studyhao1999.struct; import java.util.Scanner; public class IfDemo02 { public static void main(String[] args) { //If the test score is greater than 60, you will pass, and if it is less than 60, you will fail Scanner scanner =new Scanner(System.in); System.out.println("Please enter grade"); int score =scanner.nextInt(); if (score>60){ System.out.println("pass"); }else{ System.out.println("fail,"); } scanner.close(); } }
if multiple selection structure
- We found that the code just now does not conform to the actual situation. In the real situation, there may be ABCD, and there are interval multi-level judgments. For example, 90-100 is A, 80-90 is B... Etc. in life, we often have more than two choices, so we need A multi-choice structure to deal with such problems!
- Syntax:
if(Boolean expression 1){ //If the value of Boolean expression 1 is true, execute the code }else if(Boolean expression 2){ //If the value of Boolean expression 2 is true, execute the code }else if(Boolean expression 3){ //If the value of Boolean expression 3 is true, execute the code }else { //If none of the above Boolean expressions is true, execute the code }
-
For example: divide the test scores into five grades of ABCD failure, and judge the entered scores
package com.studyhao1999.struct; import java.util.Scanner; /* if Statement can have at most one else statement, which is after all else if statements if Statement can have several else if statements, which must precede the else statement Once one else if statement is detected as true, the other else if and else statements will escape execution */ public class IfDemo03 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter grade"); int score = scanner.nextInt(); if (score == 100) { System.out.println("Congratulations, full marks"); } else if(score<100&&score>=90) { System.out.println("A level"); }else if(score<90&&score>=80) { System.out.println("B level"); }else if(score<80&&score>=70) { System.out.println("C level"); }else if(score<70&&score>=60) { System.out.println("D level"); }else if(score<60) { System.out.println("fail,"); } else { System.out.println("The result is illegal"); } scanner.close(); } }
- An IF statement can have at most one else statement, which follows all else if statements
- An IF statement can have several else if statements, which must precede the else statement
- Once one else if statement is detected as true, the other else if and else statements will escape execution
Nested if structure
-
It is legal to use nested if ··· else statements. That is, you can use an if or else if statement in another if or else if statement. You can nest else like an IF statement
-
Syntax:
if(Boolean expression 1){ //If the value of Boolean expression 1 is true, execute the code if(Boolean expression 2){ //If the value of Boolean expression 2 is true, execute the code } }