- If...else ... Statement
1 Public classIfdemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 intX=20;9 intY=12;TenSYSTEM.OUT.PRINTLN ("****** compare start ******"); One if(x<y) { ASystem.out.println ("x is smaller than Y!") "); -}Else if(x>y) { -System.out.println ("x is bigger than Y!") "); the}Else{ -System.out.println ("x and y are equal! "); - } -SYSTEM.OUT.PRINTLN ("****** comparison complete ******"); +}
The results are as follows:
Compare start ******x greater than Y! ****** comparison completed ******
- Swich statements
-
1 Public classSwitchdemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 intX=3;9 intY=6;Ten CharOper= ' + '; One Switch(oper) { A Case+:{ -System.out.println ("x+y=" + (x+y)); - Break;//Exit Switch the } - Case‘-‘:{ -System.out.println ("x-y=" + (X-y)); - Break; + } - Case‘*‘:{ +System.out.println ("x*y=" + (x*y)); A Break; at } - Case‘/‘:{ -System.out.println ("x/y=" + (x/y)); - Break; - } - default:{ inSystem.out.println ("Unknown operation! "); - Break; to } + } - } the *}
If the break statement is not set, the program does not jump out of the switch
- While
1 Public classWhiledemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 intX=1;9 intSum=0;Ten while(x<=10){ Onesum+=x; AX + +; - } -SYSTEM.OUT.PRINTLN ("Cumulative sum of 1 to 10 for:" +sum); the } - -}
While is a looping statement and a conditional judgment statement. When the implementation does not know how many times the loop executes, it uses the while loop.
- Do...while
1 Public classDowhiledemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 intX=1;9 intSum=0;Ten Do{ Onesum+=x; AX + +; -} while(x<=10); -System.out.println ("1 to 10 and for:" +sum); the } - -}
Do...while ... The operation of the loop and the while loop is the same, but it is executed at least once.
- For
1 Public classFordemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 intSum=0;9 for(intx=1;x<=10;x++){Tensum+=x; One } ASystem.out.println ("1 to 10 of the and for:"); - } - the}
(initial value of assignment; Judgment condition; assignment increment or decrement)
- Nesting of loops
1 Public classFornesteddemo {2 3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8 for(inti=1;i<=9;i++){9 for(intj=1;j<=i;j++){TenSystem.out.print (i+ "*" +j+ "=" + (i*j) + "\ T"); One } ASystem.out.print ("\ n"); - } - } the -}
The results are as follows:
1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 4*1=4 4*2=8 4*3=12 4*4=16 5 *1=5 5*2=10 5*3=15 5*4=20 5*5=25 6*1=6 6*2=12 6*3=18 6*4=24 6*5= 6*6=36 7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
- Interruption of the Loop
-
- The break statement can force the program to interrupt the loop, and the program will leave the loop when execution breaks.
- The continue statement can force the program to jump to the beginning of the loop;
Getting Started with Java-selection and looping statements