The usage of C language while statement _c language

Source: Internet
Author: User

In the C language, there are three commonly used program structures:

Sequential Structure : The code executes in the past, without any "beating around the bush";

Select the structure : Also called the branching structure, the emphasis is to master if else, switch, and conditional operators;

Loop structure : Repeats the same piece of code.

The sequential structure and selection structure are explained earlier, and this section begins to explain the looping structure. Loop, which is repeated execution of the same piece of code, for example, to calculate the value of 1+2+3+......+99+100, it is necessary to repeat the 99 addition operation.

While loop

The general form of the while loop is:
while (expression) {
Statement block
}

Where the expression is called a cyclic condition, the statement block is called the loop body.

The while statement means that the value of the expression is evaluated first, and when it is 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 continues to execute ... This process repeats until the value of the expression is False (0), exiting the loop, and executing the following statement. Its execution process is shown in the following illustration:

Use the while statement to calculate a value of 1 plus 100:

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

Run Result:

5050

Code Analysis:

1 when the program runs to the while statement, the execution loop body is executed because the value of the i=1,i<=100 is true, and the value of I to 2,sum after the execution ends becomes 1.

2) will continue to determine whether i<=100 is set up, because at this time I=2,I<=100 was established, so continue to execute the loop body, after the execution of the value of I to 3,sum 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, stops executing the loop body, and executes the code following the while loop.

Take another example to count the number of line 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;
}

Run Result:

Input a string:c.biancheng.net
Number of characters:15

The loop condition in this example program is GetChar ()!= ' \ n ', which means that as long as the character entered from the keyboard is not a carriage return, the loop continues. The n++ of the loop body completes the count of 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 difference between a do-while loop and a while loop is that it executes the loop body first, then it determines whether the expression is true, if it is true, the loop continues, and if it is false, the loop is terminated. Therefore, the Do-while loop must perform at least one loop body at a time. Its execution process can be represented by the following figure:

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

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

Run Result:

5050

Note while (i<=100); the last semicolon;

While loops and do-while each have their own characteristics, you can choose the appropriate, practical programming uses while loop more.

The above is the C language while statement of the use of detailed introduction, the need for students can refer to.

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.