Loops in C Programming

In our daily life, when we use the word loop, one thing which comes to our mind is just like a circle and a circle is a closed shape. If we consider the circle's boundary like a road and we have to travel on that road, what will happen? We come back on our starting point again and again.

The same concept is used in programming languages with the word loop. When we have to perform a specific task, again and again, we use a loop to repeat that task instead of writing the same code repeatedly. You will understand this concept by understanding that if we have to print counting from 1 to 100 or if we have to print the records of all the people in our database or if we want to find the factorial of a particular number, we use loops because in all above scenarios we have to repeat something.

Loop in Computer Programming:

In programming, a loop is a set of code that is continually repeated until or unless a certain condition is reached.
When we have to repeat a statement many times that maybe the very time-consuming process and due to this our program is getting much larger and larger so to avoid this type of scenarios we use loops that repeat a statement or group of statements until the condition is true.

Types of Loops:

1. While Loop (Top Conditional Loop)
2. For Loop (Counter Loop)
3. Do-While Loop (Bottom Conditional Loop)

The syntax of While Loop:

1. Initialization
2. while(Condition){
  Statement(s) to be executed until the Condition is True
Increment/Decrement Operator

Initialization: We have to define a variable and initialize it. For example, we define a variable x and initialize it by 1 just like that
int i=1;

Condition: After writing the keyword while we have to define the condition in round braces as shown in the above syntax. The condition can be anything like x<=10 this is true until or unless the value of x is greater than 10. We have to write condition in our code just like that while(x<=10)

In curly braces, we have to write the statements which we want to execute repeatedly until the condition is true and after all, we have to increase or decrease the value of x just like x++ or x-- according to our condition to fulfill the criteria.

Program to print Counting from 1 to 10 using while loop:
while loop in c programming

The Syntax of For Loop:

    Body to be executed until the Condition is True

To use for loop in our program we need to write the keyword for and in round braces, we initialize a variable and then after a semi-colon write condition and then again after semi-colon use increment or decrement operator according to your requirement.

Program to print Counting from 1 to 10 using for loop:
for loop in c programming

The syntax of do-while Loop:

1. Initialization
  Statement(s) to be executed until the condition is true

To use do-while loop we have to initialize a variable first just like we did for while loop then writes the keyword do and in curly braces write statements to be executed and at the end Increment/Decrement operator and then write the keyword while and in round braces a condition just like we did in other loops.

Program to print Counting from 1 to 10 using a do-while loop:
do-while loop in c programming


Popular posts from this blog


Change your Application background in Scene Builder || JavaFX FXML Tutorial

Implementation of Graph in C || Directed and Undirected Graph