The while of the 1203.3--Loop statement

Source: Internet
Author: User

While loop

The general form of a while loop is:
while (expression) {
Statement block
}
Where an expression is called a loop condition, a statement block is called a loop body.

The while statement means that the value of the expression is evaluated first, when the values are true (not 0), the loop body statement is executed, the loop body statement is executed, the value of the expression is evaluated again, and if true, the loop body is resumed ... This process repeats until the value of the expression is False (0), exits the loop, and executes the subsequent statement. The execution process is as follows:


Use the While statement to calculate the value from 1 to 100:

#include <stdio.h>int main (void) {    int i=1, sum=0;    while (i<=100) {        sum+=i;        i++;    }    printf ("%d\n", sum);    return 0;}

Operation Result:
5050

Code Analysis:
1) When the program runs to the while statement, the loop body is executed because the value of i=1,i<=100 is true, and the value of I is changed to 2,sum after execution ends to 1.

2) Next will continue to determine whether I<=100 is established, because at this time i=2,i<=100 is established, so continue to execute the loop body; the value of I becomes 3,sum after execution is changed to 3.

3) Repeat step 2).

4) When the loop goes to the 100th time, the value of I changes to 101,sum to 5050, because at this point the i<=100 no longer holds, so it exits the loop, no longer executes the loop body, and executes the code that follows the while loop instead.

Another example is to count the number of characters entered from the keyboard:

#include <stdio.h>int main () {    int n=0;    printf ("Input A string:");    while (GetChar ()! = ' \ n ') n++;    printf ("Number of characters:%d\n", n);    return 0;}

Operation Result:
Input a String:cnblogs
Number of Characters:7

In this case, the loop condition getchar()!=‘\n‘ is, the meaning is, as long as the character entered from the keyboard is not a carriage return to continue the loop. The loop body n++; completes counting the number of input characters.

Do-while Cycle

In addition to the while loop, there is a do-while loop in the C language.

The general form of the Do-while cycle is:
do{
Statement block
}while (expression);

The Do-while loop differs from the while loop in that it executes the loop body first, then determines whether the expression is true, resumes the loop if true, and terminates the loop if it is false. Therefore, the Do-while loop executes at least one loop body. The execution process can be expressed as:


Use Do-while to calculate the value of 1 plus to 100:

#include <stdio.h>int main () {    int i=1, sum=0;    do{        sum+=i;        i++;    } while (i<=100);    printf ("%d\n", sum);    return 0;}

Operation Result:
5050

Note the while(i<=100); final semicolon ; , which must be there.

While loop and do-while each have the characteristics, we can choose properly, the actual programming using while loop more.

The while of the 1203.3--Loop statement

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.