Java control Statements--branching, looping, jumping

Source: Internet
Author: User
Tags switch loop

    • Branch statement (if statement, switch statement);
    • Loop statement (For,while,do...while);
    • Jump Statement (Break,continue,return);
Branch statement (if statement, switch statement)

If Judgment statement format:

Format 1: Suitable for use in one case.

if (judging condition) {The    code that conforms to the conditional execution;      }

Format 2: Suitable for use in both cases.

if (judging condition) {    The code that conforms to the conditional execution}else{    code that does not conform to the conditional execution;}

The if-else is very similar to the ternary operator:

三元运算符的优点: 结构比较简洁。三元运算符的缺点: 符合条件必须要返回一个结果,不能执行语句。

Format 3: Suitable for use in a variety of situations.

if (condition 1) {    The code that conforms to condition 1 executes}else if (judging condition 2) {    conforms to condition 2 executes code}else if (judging condition 3) {    conforms to the condition 3 executes the code}......else{    does not conform to the above Code for the Condition execution}
 

Switch's English is interpreted as a toggle, as it is interpreted, the switch loop features a switch, and the jump to a condition results in a certain result.

Writing:

switch (the variable that needs to be cycled) {The variable value that needs to be    cycled: The statement that needs to be        executed;        break;//indicates that the variable satisfies this condition, and after executing the corresponding statement, it jumps out of the loop. Otherwise the code will continue to execute downward, knowing that a break jumps out of the loop, otherwise the program will be executed. ......    Default:        The statement to be executed;        

如果在条件语句中不加break的话,程序会继续向下执行,直到遇到循环控制语句break跳出循环,否则程序继续向下执行。

Loop statement (For,while,do...while)

The FOR Loop statement is the most efficient and flexible loop structure in the Java language, typically used in cases where the number of cycles is known.

First, for loop syntax

for (initialization; Condition UPDATE) {            statements;    

Syntax Explanation: When the For statement executes, the initialization operation (initialization) is performed first, then the termination condition expression (condition) is satisfied and the loop exits if the termination condition is met. Otherwise executes the statement in the loop body, then executes the iteration part (update) to complete a loop. The next cycle starts with the judgment termination condition and the corresponding operation is performed according to the result.

Cases:

public class fordemo1{    piblic static void Main (string[] args) {for            (int. i=0;i<5;i++) {for                (int j=i;j<5 ; j + +) {                    System.out.print ("*");                }                System.out.println ();}}}         

Print as shape (positive triangle)

While loop statement usage is simpler than the for statement, and the format is simple;

while (judging condition) {     loop body}

There is a do while corresponding to the while, and the difference between while is that the format of the Do while is;

Do {     loop body}whil (judgment statement);

The Do while executes a loop-body statement and then makes a judgment statement, which means that the loop body statement is executed at least once, regardless of whether the judgment is true.

Jump Statement (Break,continue,return) Java supports 3 types of jump statements: Break,continue and return . These statements transfer control to other parts of the program. Each of these statements is discussed below.  

Note: In addition to the jump statements discussed here,Java also supports another way to change your program's execution process: by exception handling . Exception handling provides a structured approach that allows your program to capture and handle runtime errors. It is controlled by the following five keywords:try,catch,throw,throws, and finally . Essentially, the exception handling mechanism allows your program to complete a non-local branch jump .

In Java, the break statement has 3 functions.

    1. You have seen that in a switch statement, it is used to terminate a sequence of statements.
    2. It can be used to exit a loop.
    3. It can be used as an "advanced" goto statement.

Use Break to exit a loop
You can use the break statement to force exit loops directly, ignoring any other statements in the loop body and conditional tests for loops. When a break statement is encountered in a loop, the loop is terminated and program control starts over the statement following the loop.

Continue statements

The continue statement can only be applied in the For,while and Do...while loop statements, allowing the program to skip directly behind the statement for the next loop.

The return statement is used to explicitly return from a method.

That is, thereturn statement causes the program control to return to the method that called it. Therefore, it is categorized as a jump statement. Here is a brief introduction to it.

At any time in a method, the return statement can be used to return the executing branch program to the method that called it. The following example illustrates this point. In the following example, because the Java runtime calls main (), the return statement causes the program execution to return to the Java runtime System.

Demonstrate return. Class Return {public     static void Main (String args[]) {         Boolean t = true;         System.out.println ("before Thereturn.");        if (t) return; Return to caller        System.out.println ("This won ' Texecute.");}     }

The results of the program are as follows:


As you can see, the last println () statement is not executed. Once the return statement is executed, the program control passes to its caller.

The last point: in the above program, the IF (t) statement is necessary. Without it, the Java compiler will mark an "unreachable code" error because the compiler knows that the last println () statement will never be executed. To prevent this error, for this example to execute, use the IF statement here to "deceive" the compiler.

Java control Statements--branching, looping, jumping

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.