About some loops in Java

Source: Internet
Author: User

1:switch Statements
(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.
The final recommendation is not to omit
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.

(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: Loop statement
(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

Case:
g: Number of daffodils printed on the console

(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;
}
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
(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
(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) {
//Code completion
}
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.

About some loops in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.