while-loop

While Loop in C programming : Simple Tutorial

Hi guys. Welcome to the Digital Chalkie Educational Blog. Today I’m going to discuss “While Loop in  C programming”. This article belongs to our  ” C Programming Tutorial Series”. You can read other articles by click here.

Definition:- A loop is a method that uses to execute a block of statements repeatedly until a given condition returns false.

Syntax of While Loop

Here is the way that we can write a while loop in C program.

while (condition)
{
    Statement to be execute repeatedly
    increment (i++) or decrement(i--) operation
}

Flow of While loop

An example program
int main()
{
    int i=0;
    while (i<5)  // Condition
    {
        printf("%d \t",i); //statement to repeat 
        i++; //increment
    }
    printf("\n Example 01 is Over ");
    return 0;
}

In the above program, we try to print the integers below five. We use “i” as the counter variable¹.  Then the program prints the value of “i”. It is the repeated statement of this program.  The phrase” i++” means “i=i+1”.i++”  is a standard way to represent the increment of the counter variable.

Output of the above program

0       1       2       3       4
 Example 01 is Over

Infinite While Loop in C

 

Infinite while loops are the loops that will never end. This is caused when the counter variable unable to satisfy the condition in While loop. The following program is an example for it

#include <stdio.h>
#include <stdlib.h>

int main()
{
 int num = 6;
 while (num >=5)
 {
  printf("%d", num);
  num++;
 }
return 0;
}

In the above program the counter variable, num never satisfies the condition. So the program executes until user shut it off.

Using logical expressions in while loop

Not only the relational operations but also we can also use the logical expressions when using the for loops. The following scenarios are valid in the programming.

-Using AND (&&) operator, both conditions must be true

-Using AND (&&) operator, both conditions must be true
while ( num1<10 && num2<10 )

-Using OR(||)operater, this loop runs until both loops return false
while (num1<=15 || num2<=15)

-Here we using two operators NOT and AND
while ( num1!=num2 && num1<num2 )

Here we using OR and NOT
while ( num1<num2 || num1!=num2 )
Try it Your Self
#include <stdio.h>
int main()
{
   int i=2, j=2;
   while (i <= 6 || j <= 3)
   {
	printf("%d %d\n",i, j);
	i++;
	j++;
   }
   return 0;
}

Ok my dears, Its time to me wind up this tutorial. You can try the above program yourself and find the outputs. I think you learned something valuable from this tutorial. Wish you all the best and Stay Safe. Good Bye..

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.