And Java1 to 100 - loop structure

Loop structure

Java loop control statements have while loop, do-while loop, for loop. Features loop structure is at a timing loop condition is satisfied, repeatedly perform certain procedures, until the loop condition does not hold.

One cycle is composed of four parts, namely a variable loop, loop condition, loop, change the value of the loop variable.

1 . Loop variable, the loop variable for controlling the number of cycles.

2 . Cycling conditions cycling conditions used to determine whether to continue the cycle.

3. The body of the loop, the loop is the loop condition is true code segment when to be executed.

4. change the value of the loop variable, only the value of the loop variable can change, only the end of the cycle, otherwise it is an endless loop.


1. the while loop structure

while loop syntax rules are as follows:

Loop variable initialization

while ( loop condition ) {

         Loop

}

Syntax parsing rules:

1. Keyword while the contents of the parentheses after the condition is cycling

2. The cycling conditions is a Boolean expression that is true iteration of the loop, it is the false terminated cycles.

3. braces statement is the loop body.

4. the while loop is to determine whether the conditions established, and then decide whether to perform

Examples

public static void main(String[] args) {
    int i = 1, sum = 0;
    while (i <= 100) {
        sum += i;
        i++;
    }
    System.out.println("1+2+3+...+100的和是" + sum);
}

Code analysis

1. I is a loop variable, SUM store the data summation.

2. i <= 100 is the loop condition, when i <= 100 , the loop body is executed.

. 3 . SUM = I + is the cumulative sum.

. 4 . I ++ to change the value of the loop variable, so the opportunity to terminate the cycle.


2. do the while-loop structure

do-while loop syntax rules are as follows:

Loop variable initialization

do{

      Loop

} While ( loop condition );

Syntax parsing rules:

1. do the while- loop to do begin.

2. A pair of braces are loop.

3. the while keyword and the parentheses following is cycling conditions.

4. do the while- loop is the first pass through the loop, then determine the cycle condition is established, even if the loop condition is not satisfied, then performing at least also the loop again.

Examples

public static void main(String[] args) {
    int i = 1, sum = 0;
    do {
        sum += i;
        i++;
    } while (i <= 100);
    System.out.println("1+2+3+...+100的和是" + sum);
}

Code analysis

1. I is a loop variable, SUM store the data summation.

2 . I <= 100 is the loop condition, when i <= 100 , the loop body is executed.

3. SUM = I + is the cumulative sum.

4. I ++ to change the value of the loop variable, so the opportunity to terminate the cycle.


3. for loop structure

for loop syntax rules are as follows:

for ( Expression 1; Expression 2; Expression 3) {

      Loop

}

More intuitive and can be expressed as:

for ( loop variable initialization ; loop condition ; change the value of the loop variable ) {

      Loop

}

Syntax parsing rules:

1. for circulating the keyword for the beginning.

2. A pair of braces are loop.

3. Expression 1 is variable initialization loop, the expression 2 is a cyclic conditions, expression 3 is to change the value of the loop variable.

4. No matter how many times the loop is executed, the expression 1 is performed only once.

Examples
public static void main(String[] args) {
    int sum = 0;
    for (int i = 0; i < 101; i++) {
        sum += i;
    }
    System.out.println("1+2+3+...+100的和是" + sum);
}

Code analysis

1. I . A loop variable, SUM store the data summation.

2. i <= 100 is the loop condition, when i <= 100 , the loop body is executed.

. 3 . SUM = I + is the cumulative sum.

. 4 . I ++ to change the value of the loop variable, so the opportunity to terminate the cycle.


4. Multi-cycle

Multiple loop refers to a loop in the loop again contains a loop statement, also known as a nested loop. Is included loop statement called an inner loop that contains other loop is referred to as the outer loop.

Multi-loop syntax is as follows:

while ( loop condition ) {

         Loops 1

         for(;;){

              Loop 2

         }

}

1. the while loop an outer loop, for loop within the loop.

2. The outer loop once per cycle, from start to finish to complete the cycle within the cycle again.

3. the while loop, do the while- loop, for loop can be nested within each other.

 

Published 35 original articles · won praise 5 · Views 874

Guess you like

Origin blog.csdn.net/m0_43443133/article/details/104331575