Look Back, Java restudying (ix): Java Basic Flow control statement

Source: Internet
Author: User

Process Control statements are divided into conditional statements, loop statements, and interrupt statements.

  

The interrupt statement consists of break and continue, the difference being that it jumps out of the loop, while continue jumps out of the loop, and then continues the next loop.

There are two kinds of conditional statements, the most used is if...else, which is not difficult, literally walk, if the expression in parentheses is true then go after the statement, if False then go to the statement in else, where only if () {...} section is required, in the following format:

if (Boolean expression 1) {    //-expression 1 is true when the Operation}else if (Boolean expression 2) {    //Expression 2 is true when the operation}else{//The    above expression is false when the action}

The second is the switch switching statement, which is a bit cumbersome to use if statements, and is mainly used to replace multiple if...else if ... Loops, it is important to note that each case expression except the last case needs to be followed by a break, otherwise the expression will be executed to the first break or end of the statement, and the other literal types include:

1.char, Byte, short, int numbers, and their wrapper classes

2. Enumeration constants

3. String (starting from Java7.0)

The format is as follows:

Switch (variable) {case    literal 1:        //Operation break       ;    Case literal 2:        //Operation break       ;    Case literal 3:        //Operation break       ;    Default:        //actions that do not meet the above conditions}

  

The loop statement includes the For loop, while loop, Do...while loop, and foreach Loop.

    • foreach is the Java5.0 join loop, mainly for the array collection class object, which is characterized by no subscript, the use of the process does not have to develop a small standard, the same because there is no subscript so it is not suitable for the need to operate according to the subscript. Its format is
For (object obj:list) {    //Perform action on object}
    • The difference between the do....while and the while loop is that the former will let the code block execute once, while the latter may not execute at once, depending on this feature you can choose one of the two to use, do...while use less, while loops are often used together with iterators, both in the form of the following:
      while (Boolean expression) {     //is true when the action} do{     //action, this operation will execute once}while (Boolean expression) regardless of the expression result

        

    • The traditional for loop is the most used loop statement, which applies to all cases, depending on the loop condition being developed, in the following format:
for (initialize counter; loop condition; update counter) {    //loop operation}//Common template for (int i = 0; i < x; i++) {    //action}

  

Look Back, Java restudying (ix): Java Basic Flow control 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.