While loop in C

In the for loop, we know how many times a loop will execute. While we did not know how many times a while loop will be executed. Execution of while loop is based on the test condition. 

While loop in C

The syntax of While loop in C

While loop will be executed until the test condition is failed.

while (test_condition)
{
body statements;
update expressions;
}



Flow Diagram of while loop in C


while loop in C

First, the test condition inside the parenthesis is evaluated. If the test condition is true then while loop body is executed until the test condition fails.

The test condition may be any value (true or non-zero) or any expression.

A program which prints the value from 1 to 10.

#include <stdio.h>
int main( )
{
    int value = 1; //Initialize value
    while (value <= 10) //Test condition
    {
        printf("value is %d\n",value);
        value++; //increment value
    }
    return 0;
}

Output
value is 1
value is 2
value is 3
value is 4
value is 5
value is 6
value is 7
value is 8
value is 9
value is 10

Explanation
In the above program variable 'value' is set to '1'. Execution of while loop is started. If the test condition is true then the statements inside the loop body will execute i.e. print statement and post-increment statement. This loop will execute until the test condition (value<=10) fails. 

While loop in C with no body

#include <stdio.h>
void main( )
{
    int a=1,b=11;
    printf ("%d\n",a);
    printf ("%d\n",b);
    while(a<b);
}

Output
1
11

Infinite while loop in C

Example 1
#include <stdio.h>
int main( )
{
    while(1)
        printf("Infinite While Loop in C");
    return 0;
}

Output
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
.
.
.
.
.
.
.
Up to infinite 

Example 2
#include <stdio.h>
int main( )
{
    while(11>1)
        printf ("Infinite while loop in C\n ");
    return 0;
}

Output
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
.
.
.
.
.
.
.
Up to infinite 

Example 3
#include <stdio.h>
int main( )
{
    int a = 10;
    while(a<=20)
    {
        printf ("Infinite While Loop in C\n");
        a--;
    }
    return 0;
}

Output
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
Infinite While Loop in C
.
.
.
.
.
.
.
Up to infinite 

C program to print the table of 3.

#include <stdio.h>
int main ( )
{
    int a = 3,i=1;
    while (i<=10)
    {
        printf ("%d\n",a*i);
        i++;
    }
    return 0;
}


Output
3
6
9
12
15
18
21
24
27
30

Use of Logical Operators in While loop in C

Like relational operators, logical operators can also use in the while loop in C

Using AND Operator 

while ( a > 10 && b 10)
The loop body will execute when both conditions are true.

Using OR Operator

while (<=10 || <=11)
The loop body will execute if the anyone test condition is true.

Using NOT Operator

while (!= b)
The loop body will execute if the variable 'a' is does not equal to 'b'.

Program for while loop using logical operator in C

#include <stdio.h>
int main ( )
{
    int a = 10, b = 30, c = 4;
    while((a > 6 || b < 35 && c)!=a)
    {
        printf ("Loop Body Executed\n");
        a--;
        b++;
        c--;
    }
}


Output
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed
Loop Body Executed

What will happen when the test condition in the while loop has zero value?


#include <stdio.h>
int main ( )
{
    while(0) //Zero value is considered as false value, so the loop body will not execute.
    {
        printf ("Loop will executed\n");
    }
    printf ("Loop will not be executed\n");
    return 0;
}


Output
Loop will not be executed






While loop in C While loop in C Reviewed by Glory on 08:01 Rating: 5

No comments:

Powered by Blogger.