English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

while and do in C Language-Introduction and Precautions of while

1. While and do-Introduction to while

 1while statement

 Syntax:

 while(expression) {

 Loop body;

 }

The loop process:

 1.First, execute the loop body, then jump to2, otherwise jump to3

 2.First, judge whether the expression is true, if it is true, jump to1

 3.Exit the loop

2). do-.Execute the loop body, then jump to

Syntax:

 do{

 Loop body;

  }; while(expression);

 Note:The parentheses after this .while must be followed by a semicolon

The loop process:

 1.First execute the loop body, then jump to2

 2.Check the result of the expression, if it is true, jump to1, otherwise jump to3

 3.Exit the loop

3). do-.The biggest difference between .while and .do

    do-.At least one execution of .while1The loop body, but .while may not execute at all

Second, things to note about .while

    1.Avoid making the loop condition always true or always false, otherwise it may be meaningless

    2.Never add a semicolon after .while

    3.The curly braces after the .while loop can be omitted. If omitted, it can only affect the nearest statement, and this statement cannot be a variable declaration

    4.Variables defined inside the .while statement block cannot be accessed outside

tips:Here is an example of code

#include <stdio.h>
int main(int argc, const char * argv[]) {
    //while
  int i = 1,sum = 0;
  while(i <= 100)
  {
    sum += i;
    i++;
  }
printf("From1Add to10The sum of 0 is:%d\n",sum);
  // do-while
  char answer;
  do{
    printf("Performance\n");
    printf("Satisfied?"63;\n");
    scanf("%c",&answer);
  }; while(answer != 'y');
  return 0;
}

Summary

That's all for this article. I hope the content of this article can be helpful to everyone's learning or work. If you have any questions, you can leave a message for communication.

You May Also Like