The control flow statement contains the following types of statements:
1. If-else statement
2. Switch statement
2. Loop statement: While and for
3. Break and Continue statements
If-else statement: (Judgment statement)
Format one: only applies to one situation to use.
if (judging condition) {
Code that conforms to the conditional execution;
}
Format two: Suitable for use in both cases
if (judging condition) {
Code that meets conditional execution
}else{
Code that does not meet the criteria for execution
}
Format three: Suitable for use in a variety of situations
if (judging condition 1) {
A statement conforming to the conditions of 1 execution;
}else if (judging condition 2) {
A statement conforming to the conditions of 2 execution;
}else if (judging condition 3) {
A statement conforming to the conditions of 3 execution;
}else if (judging condition 4) {
A statement conforming to the conditions of 4 execution;
}......else{
Code that does not meet the above criteria for execution ...
}
Switch statement
When you need to judge a variety of situations, it is more cumbersome to use if-else statements. The switch statement is appropriate for this purpose.
1. Format:
Switch (your choice) {
Case value 1:
Code that conforms to the value 1 execution
Break
Case Value 2:
Code that conforms to the value 2 execution
Break
Case Value 3:
Code that conforms to the value 3 execution
Break
Case Value 4:
Code that conforms to the value 4 execution
Break
......
Default
The code that executes when your selection conforms to the above options;
Break
}
2. Precautions
(1) The switch statement uses a variable that can only be a byte, char, short, int, string data type, and the string data type is supported from jdk7.0.
(2) The data followed by the case must be a constant.
(3) Switch stop condition:
Once the switch statement matches one of the case statements, it executes the statement code in the corresponding
When you encounter the break keyword or the curly brace that ends the switch statement, the switch statement is no longer judged and executed from top to bottom in the order of the Code
All the code. Until a break is encountered or the curly brace of the end Siwitch statement is reached. (Through effect)
(4) In a switch statement, regardless of the order of the Code, the case statement is always judged first, and then the default statement is executed without conformance.
3. Advantages and Disadvantages:
The advantages of the switch statement: The structure of the switch statement is clear.
Switch disadvantage: Using the switch operation is very troublesome if the condition of the judgment is an interval range.
4. code example:
Requirements: Accept the keyboard input a month, according to the corresponding month output corresponding season.
345 Spring
678 Summer
9 10 11 Fall
1 2 12 Winter
Requires a switch statement to implement it.
Import java.util.*;
Class Demo4
{
public static void Main (string[] args)
{
System.out.println ("Please enter a month:");
Create a scanner
Scanner Scanner = new Scanner (system.in);
Call the Nextint method of the scanner
int month = Scanner.nextint ();
Switch (month) {
Case 3:
Case 4:
Case 5:
System.out.println ("Spring");
Break
Case 6:
Case 7:
Case 8:
System.out.println ("Summer");
Break
Case 9:
Case 10:
Case 11:
System.out.println ("Autumn");
Break
Case 12:
Case 1:
Case 2:
System.out.println ("Winter");
Break
Default
System.out.println ("no corresponding season");
Break
}
}
}
Looping statements
One, while loop
1. Format:
while (condition of the loop) {
Loop statement;
}
2, while the Loop statement to note matters:
(1) While loop statements are typically the number of times a variable is controlled by its loop.
(2) The Loop body code of the WHILE loop statement if there is only one statement, you can omit the curly brace. But it is not recommended that you omit it.
(3) The judgment condition of the while loop statement cannot be followed by a semicolon, otherwise it will affect the effect of execution.
Second, Do-while cycle
Format:
do{
Loop statement;
}while (judging condition);
The difference between a while loop statement and a do-while loop statement:
While Loop statement is the first to judge after the execution of the loop statement;
Do-while loop statements are executed first and then judged. At least once if the condition is satisfied;
Third, for Loop
Format:
for (initialization statement; Judgment statement; post-loop statement) {
Loop statement;
}
Things to note for the FOR Loop statement:
1. for (;;) This notation is a dead loop statement, which is equivalent to while (true);
2. The initialization statement for a For Loop statement executes only once, only when the first loop is executed.
3. The loop body statement for the FOR Loop statement has only one sentence, you can omit the curly brace from writing. However, it is not recommended to omit.
Break and Continue statements
Break
Scope of application:
Can only be used in a switch or loop statement.
Role:
1. The effect of break for a switch statement is to end a switch statement.
2. The effect of break in a looping statement is to end the current loop statement.
Continue
Scope of application:
Continue can only be used for loop statements.
Role:
The function of the continue is to skip the loop body content. Continue the next time.
NOTES:
1. In one case, the continue cannot be followed by any other statements, because it is never executed.
2. Continue can also be used with markers.
Tag usage:
The format of the tag:
Tag name: statement
Break and continue can all be used in conjunction with tags. Examples are as follows:
Class Breakdemo
{
public static void Main (string[] args)
{
aaa:for (int j = 0; j<3; j + +) {//j=0 outer for loop
bbb:for (int i = 0; i< 2; i++) {//i=0 inner for Loop
System.out.println ("Hello World"); 1
Break AAA;
}
}
}
}
Class Continuedemo
{
public static void Main (string[] args)
{
aaa:for (int j = 0; j<3; j + +) {//j=0 outer for loop
bbb:for (int i = 0; i< 2; i++) {//i=0 inner for Loop
System.out.println ("Hello World"); 1
Continue AAA;
}
}
}
}
Control flow Statements