Since the choice of distance, then only the trials and hardships.
Process Control Statements
- If
- Switch
- While
- Do...while
- For
- Break
- Continue
If
package com.zhb.demo;publicclass Test1 { publicstaticvoidmain(String[] args) { int2; if20){ System.out.println("num 是 偶数"); }else{ System.out.println("num 不是 偶数"); } }}
Output Result:
Num is even
Suppose there is a problem is to determine whether a number is odd, if it is odd, the output is odd, not the output is not odd. Do you think the following code is correct?
PackageCom.zhb.demo;ImportJava.util.Scanner; Public class Test1 { Public Static void Main(string[] args) {intNum Scanner in =NewScanner (system.in); System.out.println ("Please enter a number"); num = In.nextint ();if(num%2==1) {System.out.println ("num is odd"); }Else{System.out.println ("num is not an odd number"); } }}
In fact, this code if the user input value is positive, really no problem. What if the user enters-1? -2? -3?
You know, one o'clock-1 is an odd number.
Switch
Switch to pay attention to the time
- The value of the expression in the parentheses behind the switch must be an integer or character type
- The value following the case can be a constant value or a constant expression, but it cannot be a variable or a
Expression of a variable
- After the case matches, execute the program code in the matching block, and if you do not meet the break will continue
Executes the contents of the next case block until the break statement is encountered or the switch statement ends
- A switch statement can contain a default branch, which must be the last branch of a switch.
Default is performed when there are no case statements with values and variables equal.
Public class Test1 { Public Static void Main(string[] args) {intnum =1;Switch(num) { Case 1: System.out.println ("Reward Apple6ps"); Case 2:; Case 3: System.out.println ("Reward pencil"); Case 4: System.out.println ("Reward Lollipop");default: System.out.println ("the hatchet."); Break; } }}
Run results
Reward Apple6ps
Reward Pencils
Reward Lollipop
Thugs
Let's make a change to the above procedure.
Public class Test1 { Public Static void Main(string[] args) {intnum =1;Switch(num) { Case 1: System.out.println ("Reward Apple6ps"); Case 2:; Case 3: System.out.println ("Reward pencil"); Break; Case 4: System.out.println ("Reward Lollipop");default: System.out.println ("the hatchet."); Break; } }}
What will be output at this time?
While
while(判断条件){ 循环操作}
Execution process
1. Determine if the condition after the while is true
2. When the condition is established, execute the operation code within the loop, and then repeat 1.2 until the loop condition is not established
Features:* First judgment, after execution *
Output 1 to 5
publicclass Test1 { publicstaticvoidmain(String[] args) { int i=1; while(i<=5){ System.out.print(i+" "); i++; } }}
Do...while
do{ 循环操作}while(判断条件)
Execution process:
1. Perform the cycle operation first and then determine if the loop condition is true
2. If the condition is true, continue to execute 1.2. Until the loop condition is not established.
The following execution results are 1
publicstaticvoidmain(String[] args) { int i=1; do{ System.out.println(i); }while(i>100); }
For
for(循环变量初始化; 循环条件; 循环变量变化){ 循环操作}
Execution process
1. Perform the loop variable initialization section to set the initial state of the loop, which executes only once
2. The cyclic condition is judged as ture, and the Loop body code is executed; if False, exit the loop directly
3. Perform the cyclic variable change section and change the value of the loop variable so that the next judgment can be
4. Execute 2.3.4 in turn until you exit the loop
publicstaticvoidmain(String[] args) { for (int0100; i++) { System.out.println("这是for循环"); } }
Let's see what this code output
Public class T2 { Static BooleanFooCharc) {System.out.print (c);return true; } Public Static void Main(string[] argv) {inti =0; for(Foo (' A '); Foo' B ') && (I <2); Foo' C ')) {i++; Foo' D '); } } }
The result is (ABDCBDCB)
Break
Jump out of the loop
publicstaticvoidmain(String[] args) { for (int110; i++) { if2 ==0){ break; } System.out.println(i); } }
Only 1 is output at this time
Continue
Let the program jump immediately to the next iteration of the loop
publicstaticvoidmain(String[] args) { for (int110; i++) { if2 ==0){ continue; } System.out.print(i); } }
Output results
13579
Introduction to Java first quarter _1.5_ Process Control statements