catalogue
4.while loop and do while loop
4.1 while loop (judge first and then execute)
1. Branch structure
1.1 general
Although the program with sequential structure can solve the problems of calculation and output
But you can't judge and choose. For the problem of making judgment before selection, we should use the branch structure
1.2 structural form
Single branch structure:
If (condition requiring judgment){
Execute the code when the condition is satisfied
};
Multi branch structure:
If (judgment condition){
Conditional execution code
}else{
If the condition fails, execute the code immediately
};
Nested branch structure:
If (judgment condition){
Conditional execution code
}Else if (judgment condition){
If the first condition is not true, execute the code
}else{
If neither of the two conditions holds, execute this code
}
Single branch structure:
package com.cy; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int a = 3; System.out.println(a); if(a>2){ System.out.println("I'll eat apples later"); }; } }
Multi branch structure:
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int a = 3; if(a>6) { System.out.println("Eat meat at night"); }else { System.out.println("Only potatoes tonight"); } } }
Nested branch structure:
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int a = 3; if(a<3) { System.out.println("Eat apples"); }else if(a>3){ System.out.println("Eat potatoes"); }else { System.out.println("Today, the whole Manchu and Han banquet"); } }
2.switch structure
The "switch case" statement is used to judge whether a variable is equal to a value in a series of values. Each value is called a branch.
When a case is established, it penetrates all cases backward from this case, including default, until the program ends or encounters a break
Default: when none of the above case s is true, default is output
The above condition 6 is true, and a break is not added after the statement, resulting in penetration, and case 6 and default are output
Case 1 holds, because a break is added later, the statement cannot penetrate, and only case 1 is entered;
a=9. If the above case statement is not satisfied, enter default
3. Circulation structure
3.1 for loop overview
Loop structure refers to a program structure that needs to execute a function repeatedly in the program.
It determines whether to continue to perform a function or exit the loop according to the conditions in the loop body.
According to the judgment conditions, the loop structure can be subdivided into the loop structure of judgment before execution and the loop structure of execution before judgment.
3.2 for loop structure
For (start condition, loop condition, change condition){
Cycle code;
};
3.3 for loop code
If the start condition is "1", less than 100 and meets the judgment conditions, 1 will be printed. The self increasing value of i is the value of the next cycle, and the start condition of the second cycle is "2". If the conditions are met, 2 will be printed. Cycle in turn until the cycle ends if the conditions are not met.
3.4 nested for loop code
There are at least two layers of for loops. First, judge whether the outer loop is established. If it is established, judge whether the inner loop can be executed. If it can be executed, continue to judge whether the outer loop can be executed after the inner loop is executed until the conditions are not established and cannot be executed, and end the loop.
4.while loop and do while loop
4.1 while loop (judge first and then execute)
public class Test { public static void main(String[] args) { int a = 0; while (a<10) { a++; System.out.println(a); } int random = new Random().nextInt(100); System.out.println("Please enter 0-100 Integer of"); while (true) { int b=new Scanner(System.in).nextInt();//Only integers can be entered if (b>random) { System.out.println("Guess big, smaller"); }else if (b<random) { System.out.println("Guess smaller, bigger"); }else { System.out.println("Congratulations, you guessed right~"); break; } } } }
4.2 do while loop (execute first and then judge, and the loop body code shall be executed at least once)
5. Difference between cycles
- for: know the number of cycles
- While / do while: when the number of cycles is uncertain
- while: judge first. If the rules are not met, the code will not be executed
- do while: execute the code at least once, and then judge later. If the rules are not met, the code will not be executed