1:switch Statement (Master)
(1) Format:
switch (expression) {
Case value 1:
Statement body 1;
Break
Case Value 2:
Statement body 2;
Break
...
Default
Statement body n+1;
Break
}
Description of format explanation:
switch: Indicates that this is a switch statement.
expression: can be Byte,short,int,char
JDK5 can be enumerated later
JDK7 can be a string later
Case: The value that follows is the value to compare to the expression
Break: Indicates that the program is interrupted here, jumping out of the switch statement
Default: If all cases do not match, execute here, equivalent to the else in the IF statement
(2) face test
Can a switch statement be an expression of byte? Can it be a long? Can it be a string?
Yes, no, JDK7 can be later.
(3) Execution process:
A: Calculate the value of an expression first
B: Match each case, and if there is one, execute the corresponding statement body and see the break end.
C: If there is no match, execute the default statement body n+1.
(4) Precautions:
A:case can only be constants, cannot be variables, and values after multiple case cannot appear the same
Can B:default be omitted?
It can be omitted, but it is not recommended, because it is used to prompt for incorrect conditions.
Special cases:
case, you can fix the value.
A,b,c,d
Can c:break be omitted?
can be omitted, but the result may not be what we want.
There is a phenomenon: case penetrating.
In the end, we recommend not omitting
D:default must be at the end?
No, it can be anywhere. But the suggestion at the end.
The end condition of the E:switch statement
A: It's over when you hit the break.
B: Execution ends at the end.
(5) Case:
A: Keyboard input a number (1-7), the output corresponds to the day of the week.
B: Single Choice questions
C: Keyboard Input A string problem
String s = sc.nextline ();
D: According to the given month, the corresponding season of the output
(6) If statement and switch statement respective scene
A:If
For Boolean-type judgments
For a range of judgments
Judging for a few constants
B:switch
Judging for a few constants
2: Circular statement (master)
(1) There are three kinds: for,while,do...while
(2) For Loop statement
A: Format
for (initialize statement; Judge conditional statement; Control condition statement) {
loop body statement;
}
Execution process:
A: Execute initialization statement
B: Execute judgment condition statement
If this is true, continue
If this is false, the loop will end.
C: Execute loop Body statement
D: Execute control condition statement
E: Back to B
B: Precautions
A: Judging conditional statements whether simple or complex, the result is a Boolean type
B: Loop body statement If it is one, you can omit the curly brace, but it is not recommended
C: There is a semicolon there is no left curly brace, there is no semicolon on the left curly brace
C: Case
A: output 10 times HelloWorld
B: Output 1-10 of data
C: Output 10-1 of data
D: Ask for 1-10 and
E: Ask for 1-100 and, ask 1-100 even and 1-100 odd and
F: Ask for factorial of 5
G: Number of daffodils printed on the console
H: Count Daffodils
I: Improved version of palindrome number
A five-digit number
digit = million digit
10 bits = thousands
Digit + 10 bit + thousand + thousand bit = Hundred
J: Statistics 1-1000 The number of data that satisfies the following conditions
x%3==2
X%5==3
x%7==2
(3) While loop
A: Basic Format
while (judging condition statement) {
loop body statement;
}
Extended format:
initialization statements;
while (judging condition statement) {
loop body statement;
Control condition statement;
}
By looking at this format, we know that the while loop can be converted to the For loop equivalent.
B:while's practice
Use while to improve the For statement practice
The difference between c:for and while
A: the difference in use
The control condition variable for the For statement cannot be used at the end of the loop.
While the while can continue to be used.
B: The difference in understanding
For a range of judgments
The while is suitable for ambiguous times
Example: Eating grapes
D: Case:
A: Mount Everest problem
B: Xiao Fang Save the problem (after break to do)
(4) Do...while cycle
A: Basic Format
do {
loop body statement;
}while (Judgment condition statement);
Extended format:
initialization statements;
do {
loop body statement;
Control condition statement;
}while (Judgment condition statement);
By looking at the format, we can see that in fact, the format of the three loops can be unified.
B: The difference between three cycles
A:do...while Loop performs at least one loop body
The b:for and while must first determine if the condition is true before deciding whether to execute the loop body
(5) Precautions for circular use (dead cycle)
A: Must pay attention to modify the control conditions, otherwise prone to die cycle.
B: The simplest form of dead loop
A:while (True) {...}
B:for (;;) {}
3: Control jump Statement (master)
(1) Break: Meaning of the Interruption
A: It is meaningless to leave this scenario in the loop and switch statements.
B: Role
A: Jump out of single-layer loops
B: Jump out of multi-layer loop, need the match of label statement
(2) Continue: Continue
A: It is meaningless to leave this scenario in the loop.
B: Role
A: Jump out of the single-layer loop once, you can continue the next time
C: Blank question
for (int x=1; x<=10; x + +) {
if (x%3 = = 0) {
The code is padded
}
System.out.println ("Java Basic class");
}
How to make the console output 2 times: Java Basic class
How to make the console output 7 times: Java Basic class
How to make the console output 13 times: Java Basic class
(3) Return: Back
A: Used to end the method, the following will continue to explain and use.
B: Once a return has been encountered, the program will not continue to execute in the future.
JAVA_DAY04 (on Java basis, select structure, loop structure, jump)