Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

loop
c
while
create

How to create a while loop in C

Educative Answers Team

As long as the given condition stands true, the While Loop can be used to execute a set of statements repetitively. The While loop depends on the following:

  • Condition: This controls the termination of while loop. If the condition becomes false, the while loop will terminate.
  • Execution statements: This completes the tasks assigned to the while loop.

Flowchart

svg viewer

How it works

The While loop will first check the value of the condition, and if it is true then the statement (or multiple statements) will be executed once.

Afterward, the condition will be evaluated again and if it stands true, the statements will be executed again, and so on. The While loop will only stop when the condition is found to be false.


Example

The following code adds “3” to the answer until the answer becomes 30.

#include <stdio.h>
int main () {

   int answer=0; 

   //while loop
   while( answer != 30 ) {
      answer=answer+3; //statements to be executed
   }
   printf("value of answer = %d\n", answer);
   return 0;
}

RELATED TAGS

loop
c
while
create
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring