The Wayback Machine - https://web.archive.org/web/20240717024721/https://www.geeksforgeeks.org/c-while-loop/
Open In App

while loop in C

Last Updated : 07 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The while Loop is an entry-controlled loop in C programming language. This loop can be used to iterate a part of code while the given condition remains true.

Syntax

The while loop syntax is as follows:

while (test expression)
{
   // body consisting of multiple statements
}

Example

The below example shows how to use a while loop in a C program

C




// C program to demonstrate while loop
#include <stdio.h>
 
int main()
{
    // Initialization of loop variable
    int i = 0;
 
    // setting test expression as (i < 5), means the loop
    // will execute till i is less than 5
    while (i < 5) {
 
        // loop statements
        printf("GeeksforGeeks\n");
 
        // updating the loop variable
        i++;
    }
    return 0;
}


Output

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

while Loop Structure

The while loop works by following a very structured top-down approach that can be divided into the following parts:

  1. Initialization: In this step, we initialize the loop variable to some initial value. Initialization is not part of while loop syntax but it is essential when we are using some variable in the test expression
     
  2.  Conditional Statement: This is one of the most crucial steps as it decides whether the block in the while loop code will execute. The while loop body will be executed if and only the test condition defined in the conditional statement is true.
     
  3. Body: It is the actual set of statements that will be executed till the specified condition is true. It is generally enclosed inside { } braces.
     
  4. Updation: It is an expression that updates the value of the loop variable in each iteration. It is also not part of the syntax but we have to define it explicitly in the body of the loop.

Flowchart of while loop in C

C While Loop

 

Working of while Loop

We can understand the working of the while loop by looking at the above flowchart:

  1. STEP 1: When the program first comes to the loop, the test condition will be evaluated.
     
  2. STEP 2A: If the test condition is false, the body of the loop will be skipped program will continue.
     
  3. STEP 2B: If the expression evaluates to true, the body of the loop will be executed.
     
  4. STEP 3:  After executing the body, the program control will go to STEP 1. This process will continue till the test expression is true.

Infinite while loop

An infinite while loop is created when the given condition is always true. It is encountered by programmers in when:

  • The test condition is incorrect.
  • Updation statement not present.

Example

C




// C program to demonstrate an infinite while loop
#include <stdio.h>
 
int main()
{
    // Initialization
    int gfg1 = 1;
    int gfg2 = 1;
 
    // 'gfg1' is the Check/Test statement, which means that
    // the while loop will iterate till the conditions
    // satiate
    while (gfg1 < 10) {
 
        // 'gfg2' is the body statements
        gfg2 = gfg2 + 1;
        printf("GeeksforGeeks to Infinity");
    }
    // Return statement to tell that everything executed
    // safely
    return 0;
}


Output

GeeksforGeeks to Infinity
GeeksforGeeks to Infinity
GeeksforGeeks to Infinity
.........................

As seen in the above example, the loop will continue till infinite because the loop variable will always remain the same resulting in the condition that is always true.

Important Points

  • It is an entry-controlled loop.
  • It runs the block of statements till the conditions are satiated, once the conditions are not satisfied it will terminate.
  • Its workflow is firstly it checks the condition and then executes the body. Hence, a type of pre-tested loop.
  • This loop is generally preferred over for loop when the number of iterations is unknown.


Previous Article
Next Article

Similar Reads

Difference between while and do-while loop in C, C++, Java
while loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. Syntax : while (boolean condition) { loop statements... } Flowchart: Example: C/C++ Code #include &lt;stdio.h&gt; int main() { int i = 5; while (i &lt; 10)
2 min read
Which will be faster while(1) or while(2)?
In most Programming Languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) & while(2) It is an infinite loop that will run till a break statement is issued explicitly. Neither while(1) nor while(2) but any integer whic
2 min read
Difference between while(1) and while(0) in C language
Prerequisite: while loop in C/C++ In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The boolean condition is either true or false. while(1) It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
3 min read
do...while Loop in C
Loops in C language are the control flow statements that are used to repeat some part of the code till the given condition is satisfied. The do-while loop is one of the three loop statements in C, the others being while loop and for loop. It is mainly used to traverse arrays, vectors, and other data structures. What is do...while Loop in C? The do.
7 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
For Versus While
Question: Is there any example for which the following two loops will not work the same way? [GFGTABS] C /*Program 1 --&gt; For loop*/ for (&lt;init - stmnt&gt;;&lt;boolean - expr&gt;;&lt;incr - stmnt&gt;) { &lt;body-statements&gt; } /*Program 2 --&gt; While loop*/ &lt;init - stmnt&gt;; while (&lt;boolean - expr&gt;) { &lt;body-statements&gt; &lt;i
1 min read
A nested loop puzzle
Which of the following two code segments is faster? Assume that compiler makes no optimizations. C/C++ Code /* FIRST */ for(i = 0; i &lt; 10; i++) for(j = 0; j &lt; 100; j++) //do something // This code is contributed by Shubham Singh C/C++ Code /* FIRST */ for(i=0;i&lt;10;i++) for(j=0;j&lt;100;j++) //do something Java Code /* First */ for(i = 0; i
7 min read
Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the goto statement: declare variable i of value 0decla
5 min read
Why are elementwise additions much faster in separate loops than in a combined loop?
When adding more than 2 arrays, separate loops for adding elements step by step prove to give a better performance than adding the elements in a single loop. To Test the above statement, two code blocks add the 4 array elements namely array a with b and c with d. The first code block uses a combined loop to add them while the second block uses two
3 min read
Difference between Sentinel and Counter Controlled Loop in C
Sentinel Controlled Loop A sentinel controlled loop is also called an indefinite repetition loop because the number of iterations is not known before the loop starts executing. In a sentinel controlled loop, a special value called sentinel value is used to change the loop control expression from true to false in order to determine whether to execut
3 min read
Article Tags :
three90RightbarBannerImg