2.JAVA loop structure-FOR statement, while statement detailed

Source: Internet
Author: User

A cyclic statement is introduced in Java programming. There are three common forms of circular statements:For statement, while statement, Do/while statement. The following will be described in detail on a case by case basis. The basic structure of a For statement for statement is as follows:
For (initialize an expression; Judge an expression; increment (decrement) an expression) {
Execute the statement; A section of code
}
    • Initialization expression: The meaning of the initialization expression is to define the value of the variable before the loop is defined, and if it does not, it does not know which value to start the loop from.
    • Judgment expression: The function of judging an expression is to specify the end point of the loop. If the expression is not judged, then the loop becomes a dead loop.
    • Increment (decrement) expression: This specifies how much increment or decrement the variable will be in each execution of the program.

Example: Calculate the result of a 1+2+3+4......+100.
public class control5{
public static void Main (string[] args) {
int result=0;
for (int i=1;i<=100;i++) {
Result+=i;
}
SYSTEM.OUT.PRINTLN (result);
}
}
Output Result:
50,502. While loop in English the word "while" means "when", and in Java programming, it can also be understood as "when", its grammatical structure is:
While (condition) {
Purpose A section of code
}
When the condition is true, enter the loop.

Example: Calculate the result of a 1+2+3+4......+100.
public class control5{
public static void Main (string[] args) {
int a=1,result=0;
while (a<=100) {
result+=a++;
}
SYSTEM.OUT.PRINTLN (result);
}
}
Output Result:
50,503. Do/while statement before learning the Do/while statement, first understand how the while statement works. The while statement performs a conditional judgment before executing the loop body inside the curly braces.

The Do/while statement differs from the while statement by executing the loop body inside the curly braces and then judging the condition if the condition is not satisfied and the loop body is not executed the next time. In other words, the loop body within the curly braces is executed before the condition is judged.
Example: Calculate the result of a 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

Note: In fact, in the actual program development, Do/while loop statements are not often used. Because this statement is the first to perform the cyclic body re-detection conditions, so there will be some dangerous data without detection, will be executed. We recommend that you use a while statement or a For loop statement to write code.

2.JAVA loop structure-FOR statement, while statement detailed

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.