Set 18-19: Basic Usage of switch Statement, case Penetration, String Usage of switch in JDK7
Basic usage of switch statement
The case tag of a switch statement can be an int or a type that can be converted to an int (byte, char, short) [strings can be used in JDK 7.0]
-
Based on the value of the expression, a section of execution is selected from a series of codes. The format is as follows:
switch (expression){ case value 1: Statement sequence; [break]; case value 2: Statement sequence; [break]; ... ... ... ... ... [default: Default statement;] }
case Penetration Phenomenon
The switch statement is executed from the matching case tag to the break statement or the end of the switch statement based on the value of the expression. If it does not match any case value, enter the default statement (if any)
String usage of switch in JDK7
You can use strings as expression results and case values.
package test019; public class test019 { public static void main(String[] args){ String[] a = {"A sunny day","rain","Overcast"}; int index = (int)(3*Math.random()); System.out.println(a[index]); switch(a[index]){ case "A sunny day": System.out.println("It's a nice day."); break; case "rain": System.out.println("Bad weather"); break; default: System.out.println("The weather is normal."); break; } } }
Course sample code
package test018; public class test018 { public static void main(String[] args){ System.out.println("Testing multi-choice structure"); int e = (int)(7*Math.random()); System.out.println(e); switch(e){ case 6: System.out.println("Good luck."); break; case 5: System.out.println("Good luck."); break; case 4: System.out.println("Good luck."); break; default: System.out.println("Bad luck"); break; } System.out.println("test case Penetration Phenomenon"); switch(e){ case 6: case 5: case 4: System.out.println("Good luck"); break; default: System.out.println("Bad luck"); break; } } }
Episode 20-22: while Statement/dowhile Statement/for Statement/Comprehensive Exercise (Nine-Nine Multiplication Table)
the while statement
while (Boolean expression) { //Cyclic body; } package test020_022; public class test020_022_testWhile { public static void main(String[] args){ int i = 0; int sum = 0; while(i<=100){ sum += i; i++;// Without i++, the loop would not end. } System.out.println(sum); } }
do-while Statements
do { //Cyclic body; } while(Boolean expression) ; package test020_022; public class test020_022_testDoWhile { public static void main(String[] args){ int i = 0; int sum = 0; do{ sum += i; i++; }while(i<1); System.out.println(sum); } }
What is the difference between do-While and do-While?
Which judges first and then executes. Do while is execution before judgment!
Dowhile always guarantees that the loop body will be executed at least once! This is their main difference.
for statement
for (Initial expression;Boolean expression;Stepping) { //Cyclic body; } package test020_022; public class test020_022_testFor { public static void main(String[] args){ int a = 1; while(a<=100){ System.out.println(a); a+=2; } System.out.println("while End of cycle"); for(int i = 1;i<=100;i++){ System.out.println(i); } System.out.println("while The cycle is over!"); } }
package test020_022; //Calculate the sum of odd and even numbers between 0 and 100 public class Test020_022_TestWhileFor { public static void main(String[] args){ int i = 0; int j = 1; int oddSum = 0; int evenSum = 0; int totalSum = 0; //Even sum do{ evenSum+=i; i+=2; }while(i<=100); //Odd sum while(j<=100){ oddSum+=j; j+=2; } //Summation totalSum = oddSum + evenSum; System.out.println(evenSum); System.out.println(oddSum); System.out.println(totalSum); System.out.println("***********************"); //Use the while and for loops to output 5 divisible numbers between 1 and 1000, and output 3 for each line //My method-while /* int m = 1; int count = 0; while(m<=1000){ if(m%5==0){ System.out.print(m+" "); count++; if(count%3==0){ System.out.println(); } } m++; } */ //Teacher's Method for(int m = 1;m<=1000;m++){ if(m%5==0){ System.out.print(m+"\t"); } if(m%(5*3)==0){ System.out.println(); } } } }
Comprehensive Exercise (Nine-Nine Multiplication Table)
package test020_022; public class Test020_022_9x9 { public static void main(String[] args){ 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(); } } }
Episode 23: break and continue / tagged break and continue
break and continue
-
Break is used to force out of the loop without executing the remaining statements in the loop. (The break statement is also used in the switch statement)
package test_023; public class Test023_TestBreak { public static void main(String[] args){ //Output 1 to 88 /*for(int i = 0;i<100;i++){ System.out.println(i); if(i==88){ break; } }*/ //Cyclic output of random numbers until output 88 int total = 0; while(true){ int i = (int)(Math.random()*100); total++; if(i==88){ break; } } System.out.println(total); } }
-
Continue is used to terminate a loop, that is, to skip statements that have not yet been executed by the body of the loop, and then to continue the determination of whether to execute the loop next time.
package test_023; public class Test023_TestContinue { public static void main(String[] args){ //Output of the number between 100 and 150 that cannot be divisible by 3: for(int i = 100;i<=150;i++){ if(i%3==0){ continue; } System.out.println(i); } } }
Tagged break and continue
The goto keyword appeared very early in programming languages. Although goto is still a reserved word in Java, it is not formally used in the language; Java does not have goto. However, on the keywords break and continue, we can still see some shadow of goto - - tagged break and continue.
A label is an identifier followed by a colon, such as label:
The only place where tags are used for Java is before circular statements. The only reason to set the label before the loop is that we want to nest another loop in it, because break and continue keywords usually only interrupt the current loop, but if they are used with the label, they will interrupt to where the label exists.
-
Examples of tagged break s and continue s:
package test_023; public class Test023_TestContinueWithLabel { //Print all prime numbers between 101 and 150. public static void main(String[] args){ outer: for(int i = 101;i<=150;i++){ for(int j = 2;j<=(i/2);j++){ if(i%j==0){ continue outer; } } System.out.println(i); } } }