Java_ Select and loop statements
1. Select statement – switch
(1) Statement format:
switch (expression) { case Constant value 1: Statement body 1; break; case Constant value 2: Statement body 2; break; .... default: Statement body n+1; break; }
(2) Flow chart:
(3) Code implementation:
public class test{ public static void main(String[] args){ int num=10; //Data exception!!! switch(num){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Data exception!!!"); break; } } }
(4) Precautions:
- The value after multiple case s cannot be repeated
- There can only be (byte,short,char,int,String,enum) these data types in the parentheses after switch
- The format of switch statement can be flexible: the sequence can be reversed, and the break statement can be omitted (if the break is omitted, continue to execute downward until a break or the end of the whole is encountered)
public class test{ public static void main(String[] args){ int num=3; //Wednesday, Thursday, Friday, Saturday switch(num){ case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); // break; case 4: System.out.println("Thursday"); // break; case 5: System.out.println("Friday"); // break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Data exception!!!"); break; } } }
2. Circular statement
(1) Components of the cycle structure:
- Initialization statement
- Conditional judgment
- Circulatory body
- Step statement
2.1 loop statement – for
(1) Statement format:
for(Initialization expression;Boolean expression;Step expression){ Circulatory body; }
(2) Flow chart:
(3) Code implementation:
//Print (Hello) 100 times public class test{ public static void main(String[] args){ for(int i=0;i<100;i++){ System.out.println("Hello "+i); } System.out.println("Program stop"); } }
2.2 circular statement – the standard format of while
(1) Statement format:
while(Conditional judgment){ Circulatory body }
(2) Flow chart:
2.3 loop statement - extended format of while
(1) Statement format:
Initialization statement; while(Conditional judgment){ Circulatory body; Step statement; }
(2) Flow chart:
(3) Code implementation:
//Print (Hello) 100 times public class test{ public static void main(String[] args){ int i=0; while(i<10){ System.out.println("Hello "+i); i++; } } }
2.4 circular statement – standard format of do... while
(1) Statement format:
do{ Circulatory body }while(Conditional judgment);
2.5 loop statement – extended format of do... while
(1) Statement format:
Initialization statement: do{ Circulatory body Step statement }while(Boolean expression);
(2) Flow chart:
(3) Code implementation:
public class test{ public static void main(String[] args){ int i=0; do{ System.out.println("Hello "+i); i++; }while(i<10); } }
(4) Print even and between 1-100
//Find the even sum between 1 and 100 //(1) for loop public class test{ public static void main(String[] args){ int sum=0; //accumulator for(int i=1;i<=100;i++){ if(i%2==0){ //even numbers sum+=i; } } System.out.println("The result is: "+sum); } } // (2) while loop public class test{ public static void main(String[] args){ int sum=0; //accumulator int i=1; while(i<100){ if(i%2==0){ sum+=i; } i++; } System.out.println("The result is: "+sum); } } //do..while Loop public class test{ public static void main(String[] args){ int sum=0; //accumulator int i=1; do{ if(i%2==0){ sum+=i; } i++; }while(i<100); System.out.println("The result is: "+sum); } }
2.6 differences between the three cycles
(1) If the condition judgment is not satisfied, the for loop and the while loop will execute 0 times, but the do... While loop will execute at least once.
(2) The variables of the for loop are defined in parentheses and can only be used inside the loop; The while and do while loop initialization statements are already outside, so they can continue to be used when the loop is out.
3. Cycle control_ break
(1) Usage of break keyword:
- Used in a switch statement. Once executed, the entire switch statement ends immediately
- Used in loop statements. Once executed, the whole loop statement ends immediately and interrupts the loop
//Execute the printout statement only three times public class test{ public static void main(String[] args){ for(int i=1;i<10;i++){ if(i==4){ break; } System.out.println("hello "+i); } } } // hello 1 // hello 2 // hello 3
(2) Usage of continue keyword:
- Once executed, immediately skip the remaining contents of the current cycle and start the next cycle immediately
//The fourth printout statement is not executed, and other normal printouts are not executed public class test{ public static void main(String[] args){ for(int i=1;i<=6;i++){ if(i==4){ continue; } System.out.println("hello "+i); } } } // hello 1 // hello 2 // hello 3 // hello 5 // hello 6
4. Dead cycle: never stop
(1) Statement format:
while(true){ Circulatory body }
(2) Code implementation:
public class test{ public static void main(String[] args){ while(true){ System.out.println("hello"); } } }
5. Loop nesting
public class test{ public static void main(String[] args){ for(int hour=0;hour<24;hour++){ for(int minute=0;minute<60;minute++){ System.out.println(hour+"spot"+minute+"branch"); } } } }