Pupil's Hand (01) "For Loop"

Source: Internet
Author: User

---restore content starts---

Cough and cough! The first time to write this kind of thing, if there is a shortage of places, please forgive me!!! And it feels like a little swim in this. Always good at low-key me, in the ' forced helpless ' under, to arrogant a bit ... (500 words omitted here)

The words come back to the story ...

Before we all learned the while loop,do-while loops . Now to be ' good study, day up ' on the console print 1000 times, then the while loop is this:

Pubilc class whiledemo{

public static void Main (String [] args) {

int i=1;

    While (i<=1000) {

System.out.printIn ("+i+") to study hard and make progress every day! ”);

i++;

}

}

}

The Do-while is this:

public class Whiledemo {

public static void Main (string[] args) {

int i=1;

    Do {

System.out.println ("+i+") study hard, make Progress day by day! ");

i++;

    } while (i<=1000);

}

}

Through the code is not difficult to find, here the number of cycles ' 1000 ' has been fixed, for this case we can also choose the for loop structure to achieve:


public class Whiledemo {
public static void Main (string[] args) {
for (int i = 0; i <; i++) {
System.out.println ("+i+") to study hard and make progress every day! ");
}
}
}

So what is a for loop structure?

The main function of a looping statement is to execute a piece of code repeatedly, knowing that a certain condition is met. To summarize, the loop structure can be divided into 4 parts:

A. Initial part: Sets the initial state of the loop, such as setting the variable I for the number of cycles to record 0.

B. Loop body: Repeated execution of the code, that is, the output "study hard, day up!" ”。

C. Iteration part: the part to be executed before the next loop starts, as part of the loop body in the while loop structure, such as using "i++;" To accumulate the number of cycles.

D. Cycle conditions: Determine whether to continue the cycle of conditions, such as the use of "i<1000" to determine whether the number of cycles has reached 1000.

Grammar:

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

Loop body

}

Knowing the syntax of the for loop structure, what is her execution process? Understanding since the combination:

(1) Execute the initial part (int i=0;).

(2) The cyclic condition is judged (i<100;).

(3) Judging the result according to the cyclic condition.

A. If true, the loop body is executed.

B. If false, exits the loop, neither step (4), (5) is executed.

(4) Execute the Iteration section and change the value of the loop variable (i++).

(5) Repeat steps (2) ~ (4) sequentially until you exit the for loop structure.

As can be seen, in the For loop structure, the initial part of expression 1 is executed only 1 times.

Note: For the back of the parentheses, two semicolons can not be less, semicolon partition of the three parts, the first part and the third part can be omitted.

Pupil's Hand (01) "For Loop"

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.