Organizing Java fundamentals-Looping structures

Source: Internet
Author: User

There are three types of looping structures in Java:
1.while Cycle
The basic expression of the while loop:
while (Boolean expression) {
Looping content
}
As long as the Boolean expression ==true, the loop will always execute
E.G1:

class TestLoop{    public static void main(String args[]){        int i = 10;        while(i < 20){            System.out.print("value of x:" + i);            i++;            System.out.print("\n");        }    }}/*输出结果:value of x:10value of x:11value of x:12value of x:13value of x:14value of x:15value of x:16value of x:17value of x:18value of x:19*/

2.do...while Cycle
While loops, which cannot be cycled if conditions are not met; sometimes we need to not meet the conditions, and at least once
The Do...while loop and while are similar, but the Do...while loop executes at least once
Do...while the way the loop is expressed:

do{//循环内容}while(布尔表达式);

Note:
The Boolean expression is behind the loop body, so it is executed once before the Boolean expression is instrumented.
If the expression is always T, the loop content after do is executed until the expression ==f
E.G2:

class TestLoop{    public static void main(String args[]){        int i = 10;        do{            System.out.print("value of x:" + i);            i++;            System.out.print("\n");        }while(i < 20);    } }//输出内容和e.g1一样

3.for Cycle
A For loop can make some loop structures simpler
The number of times the For loop executes is the exact format that was determined before execution:

for(初始化A;布尔表达式B;更新C){ D表达式;//循环内容}//执行顺序:A>>B(满足)>>D>>C>>B(不满足跳出for循环)

E.G3:

class TestLoop{    public static void main(String args[]){                for(int i = 10;i < 20;++i){            System.out.print("value of x:" + i);            System.out.print("\n");        }    } }//输出内容和e.g1一样

Array for loop usage:

for(声明语句 : 表达式){   //代码句子}

Declaration statement: Declares a new local variable that must have a type that matches the type of the array element. Its scope is scoped to the Loop statement block, whose value is equal to the value of the array element at this time.

Expression: The expression is the name of the array to access, or is the method that returns the value to the group.
E.G4:

class TestArrLoop{    public static void main(String args[]){        int [] ages = {10,20,30,40,50};                for(int age:ages){            System.out.print(age);            System.out.print(",");        }        System.out.print("\n");        String [] names = {"Lili","Lucy","HanMeiMei","LiLei","Jim"};        for(String name:names){            System.out.print(name);            System.out.print(",");        }    }}/*输出结果:10,20,30,40,50,Lili,Lucy,HanMeiMei,LiLei,Jim,*/

Break & Continue keywords (use same as C + +)

1.class TestLoop{    public static void main(String args[]){        int [] ages = {10,20,30,40,50};                for(int age : ages){            if(age == 30){                break;            }                   System.out.print(age);                      System.out.print("\n");        }    }   }/*输出结果:1020*/2.class TestLoop{    public static void main(String args[]){        int [] ages = {10,20,30,40,50};                for(int age : ages){            if(age == 30){                continue;            }                   System.out.print(age);                      System.out.print("\n");        }    }   }/***输出结果:跳出==30直接执行下一个**10204050*/

Organizing Java fundamentals-Looping structures

Related Article

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.