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.
- How does a while loop work in C?
- Flow chart of while loop
- Example 1: Print numbers form 1 to 5 using while loop
- Example 2: Find factorial of a number using while loop
- Infinite Loop in C:
Read more about:
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 |
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.
- What is loop?
- For loop
- Nested for loop
- do...while loop
- Infinit or endless loop
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.