Вы находитесь на странице: 1из 6

While Loops In C | Programmerdouts

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)
{

// body of while loop.


//code written in this block will run repeatedly till the condition evaluated to false.
}
}

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.

Lets see a program to understand it better

#include<stdio.h>

void main()
{
int i=1; //Initializing the variable

while(i <= 5) //condition is checked for every iteration or repetation. .


{ // if satisfies the condition code execute again. and variable get increment
by one.

//block of while loop


printf("%d\n",i);
i++; //incrementing the variable.

Output:
1
2
3
4
5

Let's Trace The Output and Understand code.


Condition is that 'i' should be less than or equal to 5,if the value of 'i'
becomes greater than 5 which is '6' than,
while loop will get terminated(stop),and control will go to the next piece of code in the
program .
In this case i should be '1 ,2,3,4,5' But not greater than 5.
Step by Step Explanation.
The variable i was 1,it came in the while loop as condition is checked before so here
condition was checked and
3/6
it evaluated true. so, it printed the value of i which is 1 and incremented that value by
1 now value of 'i' has become 2
with 'i' value '2' condition is checked another time ,and this time also it evaluated true,
so it printed the value of i which is 2 and incremented that value by 1 which is 3 now.
The checking the value of i and printing of i variable and incrementing it by 1,this
process was repeated till the value of i had reached to 6.
Once the condition gets false, which in this case at the value of i = 6,then loop will get
terminated and control will not stay within the will loop.
In below photo you can get clear idea of how variable changed and get loop got
terminated at the end.

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

What is do-while loop?


What is for loops
4/6
break keyword
continue keyword
difference between break and Continue keywords
Nested if-else statement
Practice Programs

Practice Programs

Programs Regarding If-else Statements


Program Regarding Nested If - statements
Programs Regarding loops

Further Topics

What are Array?


Why Array?
Types of Array
How to declare array ?
how to Initialize an array?
What is One Dimensional Array
Concept of One Dimensional Array
Concept of Initializing One Dimensional Array
How to take array elements as input from the User
What is Two Dimensional Array ?
Concept of Two Dimensional Array
How to initialize Two Dimensional Array?
How to take Elements of Two dimensional array from the User?
How to display 2D Array As an Table?
Practice Program Regarding Arrays

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

Вам также может понравиться