Java Basics---java loop for (24)

Source: Internet
Author: User

The Java Loop statement for

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.

Syntax: 650) this.width=650; "src=" http://img.mukewang.com/536af1e60001f83604160073.jpg "/>

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 class net", use for the implementation code: 650) this.width=650; "src=" http://img.mukewang.com/536aea30000190eb04790088.jpg "/ >

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 "cyclic variable initialization", which can be initialized by an assignment statement before the for statement, such as: 650) this.width=650; "Src=" http://img.mukewang.com/ 536c6cc80001c00505490117.jpg "/>

B. Omitting "cyclic conditions" may cause the cycle to continue, which is what we often call a "dead cycle" phenomenon, such as: 650) this.width=650; "Src=" http://img.mukewang.com/ 536c6d060001fa4405310092.jpg "/>

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: 650) this.width=650; "Src=" http://img.mukewang.com/53704a7b0001217905270113.jpg " />

2, the For loop variable initialization and the change of the loop variable part, can be used "," simultaneously initialize or change the value of multiple cyclic variables, such as: 650) this.width=650; "Src=" http://img.mukewang.com/ 536c8b620001eb1005560086.jpg "/> Code, the initialization of the variable part of the two variables I and J at the same time, the change part of the cyclic variable also changes the two variables, running results: 650) this.width=650;" Src= " Http://img.mukewang.com/536c8dc50001b60e00730112.jpg "/>

3, the cyclic 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: 650) this.width=650; "Src=" http://img.mukewang.com/ 536c8e120001306804240071.jpg "/> Code, must satisfy the variable i is less than 10, and I do not wait until 5 o'clock to loop, output variable i value.



Code:

public class Helloworld {
public static void Main (string[] args) {
int sum = 0; Save the sum of the numbers that cannot be divisible by 3

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 loop
for (int i = 1; i<=100;i++) {

The variable i and 3 are modulo (take the remainder), if not equal to 0, it means that it cannot be divisible by 3
if (i% 3! = 0) {
sum = sum + i; Accumulate sum
}
}

System.out.println (the sum of the numbers that cannot be divisible by 3 between 1 and 100: "+ sum);
}
}

Operation Result:

The sum of the numbers between 1 and 100 that cannot be divisible by 3 is: 3367

This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1547765

Java Basics---java loop for (24)

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.