Java Chapter III Flow control statements

Source: Internet
Author: User

1. Conditional statements

  (1) If statement (single condition)

  if (expression) {

EXECUTE statement block

}

  (2) execution Process

·

 If statement

  A conditional expression can be any type of logical expression

If the expression value is true, the contents of the curly brace are executed before the following statement is executed

If the expressed value is false, the following statement is executed directly

If there is only one statement in the curly brace number, you can omit the curly braces (it is recommended that you do not

Omitted

  

  Variable = Boolean expression? Statement 1: Statement 2
If the value of the Boolean expression is true, the statement 1 is executed
If the value of the Boolean expression is false, the statement 2 is executed

If statement (multiple selection one)

if (expression 1)
{
Executing a statement block 1
}
else if (expression 2)
{
Executing a statement block 2
}

 Switch statement

  switch (expression)

{
Case value 1: statement block 1
...
Case Value N: statement block n
Default: statement block n+1
}

  Switch statement

The value of an expression can only accept int, Byte, char, short, and no other type of value
Duplicate case values are not allowed
Once the switch encounters the first case match, the program jumps to the tab position and starts executing all the program code in the future, regardless of whether the following case conditions match, until the break statement is reached

2. Circular statements

  While loop

    while (conditional expression) {

EXECUTE statement block
}

Do...while Cycle

    Do

{

EXECUTE statement block

} while (conditional expression);

  For loop

    for (initialization expression; cyclic conditional expression; post-loop action expression)

{
EXECUTE statement block
}

  Dead loop

     for (int x=1;; X + +) {

System.out.println ("x=" +x);
}

while (true) {
System.out.println ("x=" +x);
}

  Interruption of the Loop

   When you use a loop statement, you can end the loop only if the value of the loop condition expression is false. Sometimes we want to interrupt the loop prematurely, to do this, simply add a break or continue statement to the LOOP statement block

Break End a recent layer of loops

Continue; jump out of this cycle

Process Control Statements

    for (①;②;④) {

}

 Enhanced for Loop (array loop)

    for (int i:array) {

System.out.println (i);

}

Java Chapter III Flow control statements

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.