Java Foundation chapter II (Control statement, loop statement)

Source: Internet
Author: User

First, the control statement

1.if

Format

if (Boolean) {

Method body; Boolean is true to execute method body, false to not execute

}

2.if Else

Format

if (Boolean) {

Method body; Boolean true to execute method body

}else{

Method body; Boolean is false to execute method body

}

Note: Due to the large program developed later, the source code has many if else, else matches its nearest if.

If the parentheses are not enlarged, then it controls only the first statement that is close to itself, if any at the end.

Example: if (1 > 2)

SYSTEM.OUT.PRINTLN (1); The statement belongs to if, but the Boolean value is False so do not execute

SYSTEM.OUT.PRINTLN (2); --Change the statement to not belong to the IF

Output 2

if (1 > 2); The end means that the statement is over.

SYSTEM.OUT.PRINTLN (1); --The statement does not belong to the IF, so execute.

SYSTEM.OUT.PRINTLN (2); --The statement does not belong to the IF, so execute.

Output 1

2

3.if else If ... else

if (Boolean) {

Method body; Boolean true to execute method body

}else if (Boolean) {//else if can have n

Method body; Boolean true to execute method body

}else if (Boolean) {

Method body; Boolean true to execute method body

}else{

Method body; The other is not satisfied with the execution method body

}

Second, the circular statement

1.for Cycle

Format

for (condition one; condition two; condition three) {

Method body;

}

Execution order: Condition One-->t condition two see if is true if True--> method body--condition three-->t condition two see if is true if True--> method body ...

These conditions can be omitted, but there must be, at this time a dead loop.

Condition one can be defined outside, but must be within the defined domain.

Example: for (i = 1;i < 5; i++) {

System.out.println ("Hello World");

}

Note: First + +: Perform other actions after the + + operation.

After + +: Executes the operator close to + +, and then + +.

2.while Cycle

Format

while (Boolean) {//--> Boolean is the true execution method body, a false exit loop.

Method body;

}

You can set forced exit: break;.

3.do while loop

Format

do{

Method body; The Boolean value is not judged until the method body is executed first.

}while (Boolean)

4.switch Cycle

Format

switch (int or char) {

case int or char: Method body; break;

case int or char: Method body; break;

case int or char: Method body; break;

Default: Method body; break;

}

Note: If a switch has a branch, it enters the corresponding branch and touches the break.

If there is no break, all the case is executed until the default is executed.

If there is no default, then the last case is executed, and there is no match.

Java Foundation chapter II (Control statement, loop statement)

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.