Java Learning Note Five-process Control

Source: Internet
Author: User
Tags case statement

Branching structure

Java provides two common branch control structures: The IF statement and the switch statement.

If statement

The IF statement uses a Boolean or boolean expression (the result of the expression is a Boolean value), and the IF statement has 3 forms:

The first form of:

        if (5 > 3) {            System.out.println ("5 > 3");        }

The second form of:

        if (5 > 3) {            System.out.println ("5 > 3");        }         Else {            System.out.println ("5 < 3");        }

The third form of:

        if (5 > 3) {            System.out.println ("5 > 3");        }         Else if (5 > 4) {            System.out.println ("5 > 4");        }         Else {            System.out.println ("5 < 3 and 5 < 4");        }

Let's take a look at the following example:

        int age =;         if (age <=) {            System.out.println ("young Man");        }         Else if (Age > && age < Max) {            System.out.println ("Youth");        }         Else if (Age >= && Age <) {            System.out.println ("middle-aged");        }         Else {            System.out.println ("old person");        }

This is the application of the IF statement, the result is of course young people, no problem.

However, if you have a lot of branches, it can be cumbersome to write, so the following switch statement appears.

Switch statement

A switch statement consists of a control expression and multiple case labels. The data type of the control expression following the switch statement can be:byte, short, char, int four basic types, enum type and java.lang.String type (from Java7 only) . Total 6 kinds.

Example:

String season = "Summer"; Switch(season) { CaseSpring: System.out.println ("The Spring Rain");  Break;  CaseSummer: System.out.println ("Hot Summer");  Break;  CaseAutumn: System.out.println ("Autumn Wind Howling");  Break;  CaseWinter: System.out.println ("East Snow capped");  Break; default: System.out.println ("All Seasons are not divided");  Break; }

Note: There is a break after each case statement, and the default branch is entered when the value of the control expression is not equal to the value that follows any of these.

Of course, there are cases where a break is not required after a case. Look at the following example:

        CharScore = ' B '; Switch(score) { CaseA:         CaseB: System.out.println (Failed);  Break;  CaseC:         CaseD: System.out.println ("Failed");  Break; default: System.out.println ("Score input Error");  Break; }

When score is a or b, they pass, and when they are C or D, they fail.

Loop structure while statement

The structure is as follows:

         while (expression) {            statement;        }

Where the result of expression must be a Boolean value. The while loop is evaluated for expression before each execution of the loop body (statement), and if the value is true, the loop body is entered, otherwise the while loop ends.

Example:

        int count = 0;          while (Count <=) {            System.out.println (count);            Count+ +;        }        System.out.println ("Count has been closed greater than 10, now 11");

The program prints 11 times after count, executes the last sentence.

Do and loop

The difference between a do and while loop is that the while loop line determines the loop condition, and if the condition is true, the loop body is executed. The Do While loop executes the loop body before judging the loop condition, and if the loop body is true, then the loop body is executed, so the reciprocating.

The structure is as follows:

         Do {            statement;          while (condition);

Rewrite The example above:

        int count = 0;          Do {            System.out.println (count);            Count++        ;  while (Count <=);        System.out.println ("Count has been closed greater than 10, now 11");
For statement

The structure is as follows:

         for (statement 1; Statement 2; statement 3) {            loop body        }

The order in which they are executed:

    1. Executes statement 1 First and executes only once
    2. EXECUTE Statement 2, Statement 2 is a conditional control statement, the result is a Boolean value, if False, the loop ends, the statement 3 and the loop body will not be executed.
    3. If the result of statement 2 is true, the loop body is executed
    4. After executing the loop body, the statement 3 is executed
    5. Execute Statement 2 again, repeat 2345.

Example:

        int c = 0;          for (int i = 0; i < i++,c++) {            System.out.println (// printed 10 times         }         // Ten

The following statement is legal, but a dead loop:

         for (;;) {            System.out.println ("I am the cycle of Death");        }

That is, statement 1, Statement 2, Statement 3 is not required, this can be determined according to the situation.

Control Loop Statement break statement

The break statement functions by jumping out of the current loop, as follows:

        int c = 0;          for (;; C + +) {            if(C >)                {break;            }            System.out.println ("I am not a dead circle");        }

Originally the for statement constitutes a dead loop, but now with a break, it is not a dead loop, and when c>10, it executes a break and jumps out of the for loop.

Break can be used in any loop statement and also in a switch statement.

Continue statements

The continue statement does not jump out of the current loop, just ignores the loop.

Example:

        int c = 0;          while (c <) {            C+ +;             if (c = = 5)                {continue;            }            System.out.println ("execution loop Body");        }

Originally printed 10 times "execution loop body", but when C equals 5 o'clock, ignoring this cycle, the following statement will not be executed, the next loop directly, so less print once, and finally only 9 times.

Java Learning Note Five-process Control

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.