What are the while Loop in C Language ??? with Free Notes

What are the while Loop ???

while Loop is the fundamental repetitive control structure used in C. while Loop is suited for the problems where it is not known in advance that how many times a statement or a set of statements i.e. statement block will executed.

Let’s understand the while Loop with it’s Syntax,

Syntax

while (condition) {
    // code block to execute while condition is true
}

Here, “condition” is an expression that evaluates to either true or false. Now Let’s understand this Syntax and Concept with the example,

Note :

The statement block is executed repeatedly till the condition evaluates to a non-zero value otherwise if it becomes zero, the execution of while statement will terminate and control will be passed to the statement following while loop.

Sequence of Operations in a while Loop as follows:

  1. Test the Condition
  2. If the condition is true then execute the statement block and repeat step 1.
  3. If the Condition is false, leave the loop and go on with the rest of the program.
C Language Notes

Example

Here we write a program for while Loop,

#include <stdio.h>

int main() {
    int num = 1;

    // While loop example
    while (num <= 5) {
        printf("%d ", num);
        num++;
    }
    printf("\n");

    return 0;
}

Explanation about Example

Here we Write an example of while Loop, with this we print a numbers 1 to 5. And it will continually printed, but when the “num” is matched, equal to the 5 or less than 5 means the condition becomes true, then it will stop automatically.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top