"Java" JAVA_14 loop structure

Source: Internet
Author: User

Loop Structure

when type: when P When the condition is set (T), repeat A until p is " false cycle is not stopped .

until type: Execute First A, then Judge p, if it is T, then execute A, so repeat until p to F.

1.While Cycle

the basic format and flowchart of the while loop:

while ( boolean expression ) {

circulation body;

}

The while loop is detailed:

at the beginning of the loop, the value of the boolean expression is computed once, and if the condition is true, the loop body is executed. And for each additional cycle, it will be recalculated before the start.

there should be statements that make loops tend to end, or an infinite loop-a " dead " loop

While loop code:

/*** Test While Loop*/ Packagecom.oliver.test; Public classTestwhile { Public Static voidMain (string[] args) {//Print 1 to        intA = 1;  while(a<=100) {System.out.println (a); A++; }        //ask for 1 to 100 of the and        intb = 1 ; intsum = 0;  while(b<=100) {sum+=b; b++;    } System.out.println (sum); }}
2.do-while Cycle do-while The basic format of the loop:

do {

circulation body;

} while ( Boolean expression );

Do-while Cycle instructions and flowchart:

1. perform the loop body first

2. post-judgment Boolean expression

3. The loop body is executed at least once

Do-while Loop Code:

 Package com.oliver.test;  Public class Dowhile {    publicstaticvoid  main (string[] args) {        int i = 1;          Do {            = i+1;            I++        ;        }  while (i<=10);            System.out.println ("I's value is" +i);}            }

The difference between while and do-while

while first judgment, after execution

Dowhile Execute first, then Judge

3. For Loop

A for Loop statement is a common structure that supports iterations and is the most efficient and flexible loop structure

Grammatical form

For ( an initial expression ; Boolean expression ; Step in ) {

circulation body;

}

the For loop is initialized before the first iteration. It then tests the condition and, at each iteration, carries out some form of "stepping".

    1. Initialization section sets the initial value of the loop variable
    2. The conditional judgment part is any Boolean expression
    3. Iterative factors control the increment and decrease of cyclic variables

"Attention" The For Loop executes the program section after the condition test, and then Execution Step in .

For Loop code:

/*** * package com.oliver.test;  Public class testfor {    publicstaticvoid  main (string[] args) {          for (int i=1;i<=10;i++) {            System.out.println (i);     }}}

"Java" JAVA_14 loop structure

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.