September 24, 2022

CurledMark

curled up on the sofa

Loops are used in programming to execute a block of code repeatedly until a specified condition is met. In C programming, there are three loops: For Loop, While Loop, and Do While Loop.

While loop in loops

Let us first learn about our first loop which is the While Loop. A while loop in C programming repeatedly executes a target statement as long as a given condition is true. In While Loop, the condition is evaluated before processing the loop’s body.

Syntax

While(condition)

{

statement 1;

statement 2;

}

Here is our first program that uses a while loop.

Program

#include<stdio.h>

int main()

{

int i=0;

while(i<5)

{

printf(“Simi\n”);

i++;

}

return 0;

}

Output

Simi
Simi
Simi
Simi
Simi

Execution flow in while loops

when i=0 then the condition is true and prints Simi. In the next line i++, i is incremented to 1, and now i=1. Then again the condition is true and prints Simi. Again i=2 and prints Simi. It continues till i=4 and Simi is printed 5 times that is 0 to 4 times.

Infinite while loops

while(1)

{

statements;

}

In C programming whenever you write a non-zero value such as 1 or any non-zero value, it is considered true and 0 is considered false. In this loop, the statements will execute forever.

&

include<stdbool.h>

while(true)

{

statements;

}

In this while loop, the statements will execute forever. Although we are going to study break statements which help to break any loop.

Note

#include<stdio.h>

int main()

{

int i=0;

while(i<5);

{

printf(“Simi\n”);

i++;

}

return 0;

}

The semicolon after a while means an empty statement. It is actually {} which is an infinite loop so if mistakenly you write a semicolon after a while then the loop will execute forever. So keep an eye on this. If you want your loop to be executed forever then you can put a semicolon after a while.

For loop in loops

The for loop in C language is used to iterate the statements or a part of the program several times. The reason they are called for loops is that you can tell your app how many times you want it to repeat the code. You can think about for loops as telling your app, “repeat this, 17 times”.

Syntax

for (init; condition; increment) 
{
   statements;
}

Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 3). If the condition is true, the loop will start over again, if it is false, the loop will end.

Flow Diagram

for loop in C

Program

#include<stdio.h>

int main()

{

for (i=0; i<3; i++)
{

printf(“Simi\n”);

}

return 0;

}

Execution flow in for loops

when i=0 then the condition is true and prints Simi.  i++, i is incremented to 1, and now i=1. Then again the condition is true and prints Simi. Again i=2 and prints Simi. It continues till i=3 and the condition is false.

Output

Simi

Simi

Simi

Simi

Do while loop is explained in the next article. Click Here!

Reference of loops

http://www.freebookcentre.net/Language/Free-C-Books-Download.html

http://www.freebookcentre.net/programming-books-download/Computer-Programming-Fundamentals-Using-C-Part1.html

Conclusion of loops

Try to understand, you will get it easily although you can read the explanation for more clarification and stay tuned for the upcoming blogs.

Also, read

https://curledmark.in/the-largest-number-program-in-an-array-in-c-programming/

https://curledmark.in/armstrong-number-and-arrays-in-c-programming/

https://curledmark.in/prime-reversepower-programs-in-c-programming/

The Book Of Five Rings Summary

The Blue Umbrella

How to Talk to Anyone by Leil Lowndes

Jonathan Livingston Seagull Book Summary

Your Brain on PORN Book Summary by Gary Wilson

THE IMMORTALS OF MELUHA By Amish Tripathi full summary

Eleven Minutes by Paulo Coelho Summary

Hyperfocus by Chris Bailey Summary

Black Beauty by Anna Sewell Summary

You Can Heal Your Life by Louise Hay Summary

ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY

THE BIOGRAPHY OF SWAMI VIVEKANANDA

THE SHAWSHANK REDEMPTION EXPLAINED

Turtles All the Way Down by John Green Story Explained