Java execution statement:
Classification:
1. Sequential statement: the code in the method is executed from top to bottom
2. Branch statements: perform different functions according to different conditions
2.1 if branch
2.2 switch branch
3. Circular statement: when the condition is established, a function will be executed repeatedly
3.1 for loop
3.2 while loop
3.3 do while cycle
4. Special process control statement
4.1 break
4.2 continue
4.3 return
4.4 lable
1, if branch
1. Simple if branch
1.1 grammatical structure:
If (expression){
... Code block
}
1.2. understand:
The expression result of the if branch must be of type boolean
true - execute code block
false - jump out of if branch
1.3 experiment:
if(false){ System.out.println(...Code within a code block...); } System.out.println(...if Code outside branch....):
1.4 cases:
import java.util.Scanner; //Case: if Rong Xi's Java test score is greater than 98, Rong Xi can get a Ferrari as a reward public class Test01{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); System.out.println("Please enter Rongxi Java Achievement:"); double score = scan.nextDouble(); if(score>98){ System.out.println("Rong Xi can get a Ferrari as a reward"); } } }
2.1 complex if branches
2.1.1 cases
import java.util.Scanner; /**Case 1: the health value of Rongxi is in the range of 20-25 (Health value algorithm: weight (Kg) / height (M2)*/ public class Test01{ public static void main(String[] args){ //establish Scanner scan = new Scanner(System.in); //1. Enter weight and height System.out.println("Please enter Rongxi's weight( Kg): "); double weigth = scan.nextDouble(); System.out.println("Please enter Rong Xi's height (m)): "); double heigth = scan.nextDouble(); //2. Calculate health value double health =weigth/(heigth*heigth); //3. Judgment if(health>20 && health<25){ System.out.println("The little nurse told Rong Xi:\"Good health, please keep it\""); } /**Case 2: if Rong Xi's score is more than 98 and his music score is more than 80, the teacher rewards him; Or if the Java score is equal to 100 points and the music score is greater than 70 points, the teacher can also reward him. */ Scanner input = new Scanner(System.in); System.out.println("Please enter Rongxi Java Achievement:"); int javaScore = input.nextInt(); System.out.println("Please enter Rongxi music score:"); int musicScore = input.nextInt(); if((javaScore>98 && musicScore>80) || (javaScore==100 && musicScore>70)){ System.out.println("Reward Rong 11 to take paid leave for one month"); } } }
3.if and else
3.1 grammatical structure
If (expression){
... Code block
}else{
... else code block
}
3.2 understanding
The result of the expression must be boolean
true - execute code block
false - execute else code block
3.3 experiment
if(false){
System.out.println("... Code block...");
}else{
System.out.println("...else code block...");
}
System. out. Println ("code other than if branch");
3.4 cases
import java.util.Scanner; /**Case 1: Rong Xi's Java test score is greater than 98 points, Reward him a Rolls Royce, or punish him*/ public class Test01{ public static void main(String[] args){ //establish Scanner scan = new Scanner(System.in); System.out.println("Please enter Java Achievement:"); double score = scan.nextDouble(); if(score > 98){ System.out.println("Reward Rongxi a Rolls Royce"); }else{ System.out.println("Punish Rongxi to write the code for a week and sort out the data"); } } }
4. if and else
4.1 grammatical structure
If (expression 1){
... Code block 1
} else if (expression 2){
... Code block 2
} else if (expression n){
... Code block n
}else{
... else code block
}
4.2 understanding
The result of the expression must be boolean
Judge the expression from top to bottom. Which expression is true first, execute the corresponding code block
If all expressions are not true, else code blocks are executed
Note: else{} optional
4.3 experiment
if(false){
System.out.println("... Code block 1...");
}else if(false){
System.out.println("... Code block 2...");
}else if(false){
System.out.println("... Code block n...");
}else{
System.out.println("...else code block...");
}
System. out. Println ("code other than if branch");
4.4 cases
import java.util.Scanner; /**Case 1: Rong Xi's health value is thin in the range of 15-20 20-25 The value within is healthy 25-30 The value of is fat (Health value algorithm: weight (Kg) / height (M2)*/ public class Test01{ public static void main(String[] args){ //establish Scanner scan = new Scanner(System.in); //1. Enter weight and height System.out.println("Please enter Rongxi's weight( Kg): "); double weigth = scan.nextDouble(); System.out.println("Please enter Rong Xi's height (m)): "); double heigth = scan.nextDouble(); //2. Calculate health value double health =weigth/(heigth*heigth); //3. Judgment if(health > 15 && health<=20){ System.out.println("The little nurse told Rong Xi:\"The body is thin. Eat more foods that supplement fat and protein\""); }else if(health > 20 && health <= 25){ System.out.println("The little nurse told Rong Xi:\"Good health, please keep it\""); }else if(health > 25 && health <= 30 ){ System.out.println("The little nurse told Rong Xi:\"Fat body, usually more exercise\""); }else{ System.out.println("Said the little nurse:\"The health value is abnormal. Please go to the hospital for detailed examination\""); } /**Enter student grades through the keyboard ? If it is between 90-100, print "A", ? Otherwise, if it is between 80-90, print "B", ? Otherwise, if it is between 70-80, print "C" Otherwise, if it is between 60-70, print "D" Otherwise, if it is between 0 and 60, "E" is printed Otherwise, print "abnormal score" */ System.out.print("Please enter the student's grade:"); double score=scan.nextDouble(); if(score>=90 && score<=100) { System.out.println("A"); }else if(score>=80 && score<90) { System.out.println("B"); }else if(score>=70 && score<80) { System.out.println("C"); }else if(score>=60 && score<70) { System.out.println("D"); }else if(score>0 && score<60){ System.out.println("E"); }else { System.out.println("The result entered is abnormal!"); } } }
5. Nested if branches
5.1 cases
import java.util.Scanner; public class Test05 { /**Case: In the sports meeting, students who run in 16 seconds in the 100 meter race are eligible to enter the finals, They were divided into men's group and women's group according to gender.*/ //Nested if branch public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.print("Please enter the result of your 100 meter race (seconds):"); double second=scan.nextDouble(); if(second>0 && second<16) { System.out.println("Congratulations, you are qualified to enter the finals"); System.out.print("Please enter your gender:"); String sex=scan.next();//Input string if(sex.equals("male")) {//Judge whether the contents of sex and "male" are equal System.out.println("Congratulations on entering the men's final"); }else if(sex.equals("female")) {/judge sex and"female"Are the contents of the two strings equal System.out.println("Congratulations on entering the women's group final"); }else { System.out.println("Abnormal gender!!"); } }else if(second>=16) { System.out.println("I'm sorry, your 100 meter result didn't reach the finals, but it doesn't matter. It's important to participate!"); }else { System.out.println("Abnormal performance!!!"); } } }
6. Summary of if branch
If. 1
2.if can judge complex conditions
3. Hump naming method of variables: except the first word, the first letters of other words are capitalized
4. Consider using if else...
5. Consider using if else if...
6. Grammatical differences:
if() {}: the simplest if
if...else...: either-or
if...else if...: Choose one more
if can be nested in infinite layers
7. Application scenario:
Individual values can be judged
Can judge the interval
Complex conditions can be judged
2, Switch branch
1. Basic switch branch
1.1 grammatical structure:
Switch (expression){
case value 1:
... Code block 1
break;
case value 2:
... Code block 2
break;
case value n:
... Code block n
break;
default:
... default code block
break;
}
1.2 understanding
The result of the expression can be: byte, short, int, enumeration (JDK1.5), String(JDK1.7)
Compare with value 1, value 2 and value n respectively, and execute the corresponding code block if which is equal
break: jump out of switch branch statement
The default code block is similar to else {}. It means other situations. It can be written or not according to the requirements
1.3 experiment
switch(50){
case 10:
System.out.println("10");
break;
case 50:
System.out.println("50");
break;
case 100:
System.out.println("100");
break;
default:
System.out.println("...default code block...");
break;
}
System. out. Println ("code outside the switch branch");
1.4 cases
import java.util.Scanner; public class Test06 { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.print("Please enter your ranking in the computer programming competition (e.g. first):"); String st=scan.next(); switch(st){ case "the first": System.out.println("Reward 1 month Maldives Tourism"); break; case "proxime accessit": System.out.println("Reward apple Pro A laptop"); break; case "third": System.out.println("Reward one mobile hard disk"); break; default: System.out.println("Knock the code for one day and sort out all learning materials for review and consolidation"); break; } } }
2. Go deep into the switch branch
2.1 interview questions
import java.util.Scanner; public class Test07{ /** Knowledge points: go deep into the switch branch Interview questions: case Can the values of be the same? may not default Can it be omitted? sure break Can it be omitted? sure default Do you have to be last? not always case Can the types of subsequent values be different? It can be different, but it must be compatible What can be the type of expression value? byte, short, int, enumeration (JDK1.5), String(JDK1.7) */ public static void main(String[] args){ switch(97){ case 10: System.out.println("10"); break; case 50: System.out.println("50"); break; case 'a': System.out.println("97"); break; default: System.out.println("...default Code block..."); break; } System.out.println("switch Code outside branch"); } }
2.2 cases
import java.util.Scanner; public class Test08{ /** Knowledge points: go deep into the switch branch Demand: enter the year and month, and output the days of the current month Or a leap year that cannot be divided by 4 */ public static void main(String[] args){ //Enter year and month Scanner scan = new Scanner(System.in); System.out.println("Please enter year:"); int year = scan.nextInt(); System.out.println("Please enter month:"); int month = scan.nextInt(); int day = 0;//Days of the month //judge switch(month){ case 1:case 3:case 5:case 7:case 8:case 10:case 12: day = 31; break; case 4:case 6:case 9:case 11: day = 30; break; case 2: //Determine whether it is a leap year if(year%4 == 0 && year%100 != 0 || year%400 == 0){//leap year day = 29; }else{//Ordinary year day = 28; } break; } //output System.out.println(year+"year"+month+"Monthly:" + day + "day"); } }
3. switch summary
Why are the types of switch expressions only byte, short, int, enumeration (JDK1.5) and String(JDK1.7)?
The result of the switch expression accepts only int at the bottom
byte is automatically converted to int
short is automatically converted to int
The enumerated object system will assign it an int value
String is the ASCII code obtained
III. if branch VS switch branch
1. Differences in grammatical structure:
if expression: boolean
Expressions for switch: byte, short, int, enumeration (JDK1.5), String(JDK1.7)
2. Differences between application scenarios:
if: judge single value, interval and complex condition
switch: judge a single value
4, for loop branch
1. Meaning: condition processing will be executed according to conditions
2. Benefits: reduce code redundancy (reduce repetitive code)
3. Grammatical structure:
For (expression 1; expression 2; expression 3){
... Code block
}
4. Understand:
Expression 1: initializing variables
Expression 2: judgment condition
Expression 3: updating variables
5. Execution process:
1. Initialize variables
2. Judgment condition: the result of judgment condition must be boolean
2.1 true - execute the code block, update the variables, and repeat step 2
2.2 false - jump out of the whole loop statement
6. Experiment:
for(int i = 1;i<=5;i++){
System.out.println("Rongxi in-situ programming");
}
7.for cycle deformation record:
The scope of a variable declared in a loop can only be within a loop
There is no difference between i + + and + + i in recycling
int i = 1;
for(;i<=5;){
System.out.println("education with conscience");
//i++;
++i;
}
System.out.println(i);
8. Case:
Understanding case 1: output 1-9 Number of for(int i = 1;i<=9;i++){ System.out.println(i); } Understanding case 2: output 0-9 Number of for(int i = 0;i<10;i++){ System.out.println(i); } Understanding case 3: output 1~9 Odd numbers in numbers //Solution 1 for(int i = 1;i<10;i+=2){ System.out.println(i); } //Solution 2 for(int i = 1;i<10;i++){ if(i%2!=0){ System.out.println(i); } } Understanding case 4: output 9~1 Number of for(int i = 9;i>0;i--){ System.out.println(i); }
9. Strengthening cases
Case 1: cycle input for 5 times int Type number, output sum Scanner scan = new Scanner(System.in); int sum = 0;//the sum for(int i = 1;i<=5;i++){ System.out.println("Please enter page" + i + "Number:"); int num = scan.nextInt(); sum += num;//accumulation } System.out.println("The sum is:" + sum); Case 2: input the scores of five courses of Rongxi students in a circular manner and calculate the average score Scanner scan = new Scanner(System.in); double sum = 0;//Total score for(int i = 1;i<=5;i++){ System.out.println("Please enter page" + i + "Door score:"); double score = scan.nextDouble(); sum += score;//accumulation } System.out.println("The average score is:" + (sum/5)); Case 3: cycle input for 5 times int Type of number, output maximum Scanner scan = new Scanner(System.in); System.out.println("Please enter the first number:"); int max = scan.nextInt();//Assume that the first entered number is the maximum value for(int i = 2;i<=5;i++){ System.out.println("Please enter page" + i + "Number:"); int num = scan.nextInt(); if(max < num){ max = num; } } System.out.println("The maximum value is:" + max);
10. Summary:
Understand the case: i can start from 1 or 0, and the update variable can be incremented or decremented
Dead cycle: (should be avoided)
for(;;){
System.out.println("Rongxi in-situ programming");
}
Pseudo loop: (should be avoided)
for(int i = 0;i>=0;i++){
System.out.println("Rongxi in-situ programming");
}
5, for loop nesting
1. Grammatical structure:
For (expression 1; expression 2; expression 3;) {
... Code block
For (expression 1; expression 2; expression 3){
.... Code block
}
}
2. Cases
Print graphics **** **** **** for(int i = 0;i<3;i++){ for(int j = 0;j<4;j++){ System.out.print("*"); } System.out.println();