Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Saturday, March 14, 2020

While Loop in C

While loop: A while loop is a control flow statement that allows code to be executed repeatedly based on a given condition.

while loop | welcome2protec.com

A while loop is a entry controlled loop where the condition checking is performed before before the program control moves within the while loop.

The while loop is one of the most used looping structure in C programming language after for loop. Let's first see the syntax of a while loop.



Syntax: while loop

while (testExpression) 
{
    // statements inside the body of the loop 
    //increment (++) or decrement (--) operator 
}

How does a while loop work in C?

Step1: The variable "i" is initialized with value 1 and then it has been tested for the condition.

Step2: If the condition (test expression) returns true, then the statements inside the body of while loop are executed else control comes out of the loop.

Step3: The value of "i" is incremented using ++ operator then it has been tested again for the loop condition. Have a look at the below example to understand it better.

To learn more about test expression check out relational and logical operators.


Flow chart of while loop in C

while loop | welcome2protec.com
while loop | welcome2protec.com

Example 1: Program to print numbers form 1 to 5 using while loop

// Print numbers from 1 to 5

#include <stdio.h>
int main()
{
    int i = 1;
    
    while (i <= 5)
    {
        printf("%d\n", i);
        i++;
    }

    return 0;
}

Output

1
2
3
4
5

In the above example 1: Counter variable "i" is initialized to 1, when "i" is 1 the condition " <= 5" is true. Hence, the body of while loop is executed and print 1 on the screen. Then the counter variable "i" is incremented to 2.

Now, "i" is 2, the condition i <= 5 is again true. The body of while loop is executed again and print 2 on the screen. Then the counter variable "i" is incremented to 3.

This process goes on until "i" becomes 6. Then condition i <= 5 will returns false and the loop will be terminated.


Example 2: Program to find factorial of a number using while loop

Let's understand what is factorial of a number?

A factorial is the product of all the positive number which is less than or equal to that number.

For example: 5! = 5 * 4 * 3 * 2 * 1 = 120

/*Program to calculate Factorial of a number using while loop*/
#include <stdio.h>
#include <conio.h>

void main()
{
 int num, count, fact = 1;
 clrscr();
 
 printf("Enter a number: ");
 scanf("%d", &num);
 count = num;
 while(count >= 1)
 {
   fact = fact * count;
   count--;
 }
 printf("factorial of %d is %d\n", num, fact);
 getch();
}    

Output

Enter a number: 5 
factorial of 5 is 120 // 5! = 5 * 4 * 3 * 2 * 1 = 120

Infinite Loop in C:

A loop that repeats indefinitely and never terminates is called an infinite loop. Let's have a look at the below example:

Example of Infinite Loop using while loop:

#include <stdio.h> 
int main()
{
     int var=1;
     while (var <=2)
     {
        printf("%d ", var); 
     }
}

Above program is an example of infinite while loop. Since the value of the variable var is same (there is no "++ or –" operator used on this variable, inside the body of loop) the condition "var<=2" will be true forever and the loop would never terminate.












Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us