Mu Network-android engineer first form -4-9 Java Loop statement for

Source: Internet
Author: User

Source: http://www.imooc.com/code/1425

In the Java loop structure, there is a for loop in addition to the while and Do...while, and three loops can be replaced with each other.

Grammar:

Execution process:

<1>, executes the loop variable initialization section, sets the initial state of the loop, which executes only once in the entire loop

<2>, the judgment of the cyclic condition, if the condition is true, the loop body code is executed, and if False, the loop is exited directly

<3>, execute cyclic variable change part, change the value of the loop variable, in order to make the next condition judgment

<4>, re-execute < 2 >, < 3 >, < 4, until Exit loop

Features: simpler and easier to read than the while and Do...while statement structure

For example, output 1000 times "I adore the net", use for the implementation code is:

Some small details to keep in mind:

1. The three expressions in parentheses after the For keyword must be separated by ";", and three expressions can be omitted, but ";" cannot be omitted.

A. Omit "loop variable initialization", which can be initialized by an assignment statement before the for statement, such as:

B. Omitting "cyclic conditions" may cause the cycle to continue, which is what we often call a "dead loop" phenomenon, such as:

Avoid the appearance of a "dead loop" during programming, so for the above code you can use break to force out loops in the loop body (the usage of break is described later).

C. Omit "Cyclic variable change", you can change the loop variable in the loop body, such as:

2. For loop variable initialization and cyclic variable changes, you can use "," to initialize or change the values of multiple loop variables, such as:

In the code, the initialization variable part simultaneously assigns the initial value to two variables I and J, and the change part of the cyclic variable also changes the two variables, running the result:

3, the loop condition part can use the combination of logical operators expression, to express complex judgment conditions, but must pay attention to the priority of the operation, such as:

The code must satisfy both the variable i is less than 10, and I do not wait until 5 o'clock to loop, the value of the output variable i.

Task

Have you mastered the use of the For loop, my little friends? Let's check it out.

Implementation function: Calculates the sum of numbers between 1 and 100 that cannot be divisible by 3

Implementation idea: Define a variable sum to hold the data and define a variable i represents the data between 1--100, the value starting from 1. The For loop determines if I is a multiple of 3, and the data that meets the requirements is saved in sum.

In line 6th of the editor, add the For statement to complete, running the result:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         intsum = 0;//Save the sum of the numbers that cannot be divisible by 34         5         //loop variable I initial value is 1, each execution of the variable plus 1, as long as less than or equal to 100 repeats the loop6          for(inti = 1;         ; ) {7             8             //The variable i and 3 are modulo (take the remainder), if not equal to 0, it means that it cannot be divisible by 39             if(i% 3! = 0) { Tensum = sum + i;//Accumulate sum One             } A         } -          -System.out.println (the sum of the numbers that cannot be divisible by 3 between 1 and 100: "+sum); the     } -}

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         intsum = 0;//Save the sum of the numbers that cannot be divisible by 34         5         //loop variable I initial value is 1, each execution of the variable plus 1, as long as less than or equal to 100 repeats the loop6          for(inti = 1; I <= 100; i++) {7             //The variable i and 3 are modulo (take the remainder), if not equal to 0, it means that it cannot be divisible by 38             if(i% 3! = 0) { 9sum = sum + i;//Accumulate sumTen             } One         } A          -System.out.println (the sum of the numbers that cannot be divisible by 3 between 1 and 100: "+sum); -     } the}

Mu Network-android engineer first form -4-9 Java Loop statement for

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.