Java Programming Fundamentals (iii) Process Control statements

Source: Internet
Author: User
Tags switch case

Process Control statements 1. Sequential statements:

Statement: A semicolon-delimited code is used as a statement.

Note: There is no code, only one semicolon is also a statement.

Sequential statements are statements executed sequentially in order from top to bottom

2. If Judgment statement

The IF statement is divided into three syntax formats, each of which has the characteristics of each format.

If there is only one statement in the curly braces, then the curly braces can be omitted, and beginners are not recommended to omit them.

Format 1 If statement is used to judge a situation

If (Judging condition)

{

If the code that satisfies the condition executes;

}

For example:

The Format 2 If-else statement is used to judge two cases

If (Judging condition)

{

The code executed to satisfy the condition;

}

else{

If another code is executed without satisfying the IF condition;

}

For example:

Format 3 if-else if–else statement, used to judge a variety of conditions

If (Judging condition 1)

{

The code executed to satisfy the condition;

}

else if (judging condition 2) {

Another code executed if the IF condition satisfies the condition in else-if;

}

else{

Neither of the above conditions satisfies the code executed in the case.

}

For example:

3. Switch statement

Switch and if statements are a common choice structure statement. However, unlike the IF statement, the value of an expression is judged, while the IF statement can use a comparison operator to determine the value of a range, but the number of values to be judged is less, two can be used, and the number of a value must be judged using the IF statement.

Format:

switch (expression)

{

Case takes value 1:

Execute the statement;

Break

Case takes value 2:

Execute the statement;

Break

Default

Execute the statement;

Break

}

Switch statement features:

There are only four types of 1,switch statement choices: Byte,short,int, Char.

remark: JDK7.0 can start using switch to use string-type data.

There is no order between 2,case and default. Judge all the case first, no matching case execution

Default

The condition for the 3,switch statement to stop is that it encounters the break keyword or the curly brace that ends the switch statement.

4, if the matching case or default does not have a corresponding break, then the program will continue to execute

The line can execute the statement until the break is encountered or the end of the switch ends.

The value in the 5,switch case must have the same data type as the value of the switch expression. And the value of the case followed must be constant, not with the variable.

For example:

case, you must add a break statement, or the program will execute until the next break or the curly brace of the switch.

4. While and Do-while loop statements:

The while loop statement is somewhat similar to the IF statement, which determines whether execution statements within parentheses are executed according to the conditional judgment. The difference is that the while statement repeats the conditional judgment as long as the condition is true, and the execution of the {} is executed until the condition is not established and the while loop ends. The while loop syntax format is as follows:

while (loop condition)

{

Execute the statement;

}

The Do-while statement and the function of the while statement, are based on the condition of the judgment is repeated to determine whether the statement inside the parentheses is executed, the difference is that the statement in the Do-while executes one side of the code, after the condition is judged whether to execute the statement inside the parentheses, at least once. The syntax structure of Do-while is as follows:

do{

Execute statement (at least once)

}while (conditional judgment statement)

Let's look at the example below:

1. Guess the number game:

Change him to the do-while structure:

5. For loop statements

1. Format: for (initialization expression; cyclic conditional expression; post-loop action expression)

{

Execute the statement;

}

Infinite loop for (,,) equals while (true)

Exercise: Print the 1~100 and:

Nesting of loops:

Is the loop statement inside there are loop statements, so the loop between the statements can be nested between each other.

Practice:

Print 99 multiplication table:

6. Break and Continue keywords

Break keyword: The break statement is used to terminate the nearest enclosing loop or the switch statement it is in, and the statements after the keyword cannot be executed

Applicable: For loop, switch two kinds of loop statements.

Use of break:

    1. Used alone.
    2. Used with labels. (tag: a name that satisfies the criteria for an identifier).

Use the details: After the break statement, write other statements, never execute, compile error.

Continue keyword: The statement passes control to the next iteration of the enclosing iteration statement where it is located. (Jump out of this loop and perform the Next loop).

Applies to: While, do, and for loop statements

Usage Details:

1. If continue appears at the end of the loop (the last statement), you can omit it.

2. If the continue appears in the first statement of the loop, then the subsequent statements cannot be executed, so compile an error.

3. Can be used in conjunction with the tag.

Java Programming Fundamentals (iii) Process 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.