Difference and usage of for loop and while loop

Source: Internet
Author: User
A For loop and a while loop can be substituted for each other, such as:

for (int i = 0; i<10; i++) {...//INSERT statement}

Using while to write is
int i = 0;
while (I<10) {
...//INSERT statement
i++;
}

The For loop is more concise and more common than the while, especially when using an array.

But if you want to do an infinite loop, the while is more convenient, for example
int i = 1;
while (i = = 1) {...}

There is also a do. While, similar to while, Do...while executes the command once in any case, even if I does not meet the set conditions,
do{
...
}while (I<10);

Most of these loops are interchangeable. On different occasions, some loops are more convenient.

Difference and usage of for loop and while 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.