Java three methods of cyclic summation, java three methods of summation

Source: Internet
Author: User

Java three methods of cyclic summation, java three methods of summation

Note: The sum of 100 is 5050.

Common for loop:

Public class HundredSum {public static void main (String [] args) {int x = 0; for (int I = 1; I <= 100; I ++) {x = x + I; // x + = I;} System. out. print (x );}}

While loop:

Public class HundredSum {public static void main (String [] args) {int x = 0; int I; while (I <= 100) {x = x + I; // x + = I; I ++;} System. out. print (x );}}

Do-while loop:

Public class HundredSum {public static void main (String [] args) {int I = 0, x = 0; do {x = x + I; // x + = I; I ++;} while (I <= 100); // loop the do statement block first, and then execute while. If the while condition is not met, the System jumps out of the loop. out. print (x );}}

The above are three common methods for summation. Thank you for your support for the help house.

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.