While Loop in C
While Loop in C
programmerdouts.blogspot.com/2019/06/while-loops-in-c.html
Loops
Looping is process of performing some task repeatedly till certain condition is met
loops are very best way to perform repeated task.
looping are very efficient
lets see what will be the approach, of writing
your name 10 times, without using looping function.
#include<stdio.h>
void main()
{
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
printf("Roy\n");
}
Output:
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
You saw, how was the approach ,it took 10 to 12 lines of code to do that work.
this work will be done very easily by looping function.(while loop)
1/6
#include<stdio.h>
void main()
{
int i;
while(i >= 10)
{
printf("Roy\n");
i++;
}
}
Output:
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
Roy
you saw that how easy it was to write, through looping functions.
Using this approach is good as well as efficient.
while Loop
Syntax:
#include<stdio.h>
void main()
{
while(condition)
{
while loop is also called entry control loop, because condition is checked first.
First the Condition is evaluated if, condition evaluated is true then the body of loop is
2/6
executed.
After execution again condition will be checked and body of loop going to be executed
again.
It will perform same task repeatedly until the condition gets false.
if you want to stop loop at certain point then
You can terminated(stop) the loop by using break keyword.
Soon we will going to cover break keyword in our further modules.
#include<stdio.h>
void main()
{
int i=1; //Initializing the variable
Output:
1
2
3
4
5
At i =6 loop got terminated because while checking the condition was evaluated as
false, because i had reached to 6,which is greater than 6.
Further Concepts
Practice Programs
Further Topics
If-else statements
What are Control Statements
Ternary Operators
What are keywords?
What are Identifiers?
What are data types ?
What are Variables?
Constants In C
Escape Sequences
different types of Declaring constants
Keyword 'const'
5/6
Practice Programs
6/6