Mu Network-android engineer first form -4-7 Java Loop statement while

Source: Internet
Author: User

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

In life, there are times when you need to repeat certain actions in order to accomplish a task. If you participate in a 10000-metre long distance running, you need to run 25 laps around the 400-metre track repeatedly. When implementing functionality in Java, it is often necessary to execute some code repeatedly, for example, in order to express "strong love", we want to output 1000 lines "I adore class net!" ”。 Obviously, at this time repeatedly knocked 1000 times output statement is not reliable drop!! So, there is a good way to solve the wood? There, looping statements !

3 types of loops commonly used in Java: While , Do...while, for

We're going to talk about while in this section.

Grammar:

Execution process:

< 1 >, determine if the condition behind the while is established (True/false)

< 2 >, when the condition is established, execute the operation code within the loop, then repeat < 1 >, < 2, until the loop condition is not established

Features: first judgment, after execution

Such as:

Task

Let's feel the charm of the while!

Implementation function: Sequential output 1 ~ 5 of these 5 digits.

Implementation ideas: Use a variable i represents a number between 1-5, the value starts at 1, the value of I is output each time the loop, and the value of I plus 1, as long as I is less than the number 5 to execute the loop

In line 7th of the editor, add the condition of the while statement to complete, and the results of the program run:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         inti = 1;//represents a number between 1-55         6         //Loop is executed when the variable is less than 5 o'clock7          while (       ) {8             9             //The value of the output variable, and add 1 to the variable for the next loop condition to be judgedTen System.out.println (i); Onei++; A         } -     } -}

Answer:

1  Public classHelloWorld {2      Public Static voidMain (string[] args) {3         4         inti = 1;//represents a number between 1-55         6         //Loop is executed when the variable is less than 5 o'clock7          while(I <= 5) {8             9             //The value of the output variable, and add 1 to the variable for the next loop condition to be judgedTen System.out.println (i); Onei++; A         } -     } -}

Mu Network-android engineer first form -4-7 Java Loop statement while

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.