DEV Community

Cover image for Loops in C!!
Jahnvi Srivastava
Jahnvi Srivastava

Posted on

Loops in C!!

Loops are really useful in programming
they reduce lines of code and make our work easy.
Now suppose we have to print Hello World five times then without loop we have to write 5 printing statement something like this

int main() {
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
printf( "Hello, World!\n");
}

But with the help of Loop, we can do it in a simpler way

int main() {
int i = 0;
while ( i < 5 ) {
printf( "Hello, World!\n");
i = i + 1;
}
}

so both the codes gave the same output i.e.
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

Now imagine printing Hello, World! hundred times😶
Then definitely first code is not a sensible one so we go for the second one i.e loops

So here we have 2 types of loops-

Entry Controlled.

Alt Text
Entry controlled loop is like a door which has condition set to be fullfiled as seen in the picture i.e firstly we satisfy the condition then the door opens.

Definition
An entry control loop checks the condition at the time of entry and if condition or expression is true then control transfers into the body of the loop.
Such type of loop controls entry to the loop that’s why it is called entry control loop.

For Example- for,while

Exit-controlled.
Alt Text
So now in this picture you can see the door is opened and the person get inside and then he is checked
so he gets a chance to enter the room atleast once even if he does not satisfy the condition set of entering.

Definition
Exit controlled loop is a loop in which the loop body is executed first and then the given condition is checked afterward. In exit controlled loop, if the test condition is false, loop body will be executed at least once.

For Example- Do While Loop.

So let's dive into the syntax of these loops

for loop

Syntax

for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}

while loop

Syntax

while (testExpression)
{
// statements inside the body of the loop
}

do-while loop

Syntax

do {
statement(s);
} while( condition );

Infinite loops

So infinite loop is one which has no condition to be fullfiled or to terminate the loop so loop keeps on running.
like in this case the loop will continue printing Hello World infinite times
Infinite for loop

int main()
{
for(;;)
{
printf("Hello World"); // loop runs infinitely
}
return 0;
}

Infinite while loop

while(1) or while(any non-zero integer)
{
printf("Hello World");
}

Infinite loops become useful when we are making Menu driven program in which we want menu to be displayed again and again unless user exits.
Used when you need to do some calculation before you know the exit condition.

Empty loop
Loop without body.
You can use it to delay execution of next operation for some time.

for(i=1 ; i<10 ; i++)

{
}

Top comments (1)

Collapse
 
epsi profile image
E.R. Nurwijayadi

Coooool.