Java loop statement while usage

Source: Internet
Author: User
Tags arrays

In life, some actions need to be repeated to complete the task. If you want to participate in the 10000-meter long-distance race, you need to repeatedly run 25 laps around the 400-meter track. When implementing functions in Java, you often need to repeat some code. For example, in order to express "strong love", we hope to output 1000 lines of "I Love course network !". Obviously, it is unreliable to repeat the output statement 1000 times !! How can we solve this problem? Yes,Loop statement!


Three common Java loops:While, do... While,


Let's talk about this section.While


Syntax:


 

Execution process:


<1> determine whether the condition after while is true/false)

 

<2> when the condition is set, execute the operation code in the loop and repeat <1> and <2> until the loop condition is invalid.


Features:Judge first, and then execute


For example:

 

The while loop is actually called a true loop. The content in the while loop will be executed only when the condition is true.

The basic structure of the for statement is as follows:

For (initialization expression; judgment expression; increment (decrease) expression ){
Execute a statement; // A piece of code
}

Initialization expression: it indicates the value of the variable before the loop is defined. If this parameter is not set, the starting value of the loop is unknown.
Judgment expression: it specifies the end point of a loop. If there is no judgment expression, this loop becomes an endless loop.
Increment (decrease) expression: specifies the number of increments or decreases in the variable for each execution of a program.


Listing 1: traditional methods of traversing arrays

/* Create an array */
Int [] integers = {1, 2, 3, 4 };
/* Start traversal */
For (int j = 0; j <integers. length; j ++ ){
Int I = integers [j];
System. out. println (I );
 }

For traversing Collection objects, this loop usually adopts the following form:

Listing 2: traditional methods for traversing Collection objects

/* Create a Collection */
String [] strings = {"A", "B", "C", "D "};
Collection stringList = java. util. Arrays. asList (strings );
/* Start traversal */
For (Iterator itr = stringList. iterator (); itr. hasNext ();){
Object str = itr. next ();
System. out. println (str );
 }

Do/while statement

Before learning the do/while statement, you must first understand how the while statement works. The while statement first performs condition judgment and then executes the loop body in braces.

The do/while statement is different from the while Statement. It first executes the loop body in braces and then judges the condition. If the condition is not met, it will not execute the loop body next time. That is to say, the loop body in braces has been executed before the condition is determined.
Example: calculate the result of 1 + 2 + 3 + 4 ...... + 100.
Public class control5 {
Public static void main (String [] args ){
Int a = 1, result = 0;
Do {
Result + = a ++;
} While (a <= 100 );
System. out. println (result );
    }
}
Output result:
5050

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.