Loop In C [ Full Information With Examples ]

Loop In C Language
Loop In C Language

What is Loop In C

When we want to run a particular part of a program or a block multiple times, then we use Loop Statements.

Meaning that when we want to run any particular statement of our program repeatedly till a particular condition is true, then we use Loop Statements.

Let’s understand these things with this one example.

Example -: If we want to print “Welcome To CsTutorialpoint” 10 times, then one way we can print “Welcome To CsTutorialpoint” by writing 10 printf () in the program. Another way can be that we use Loop Statements.

If we use the previous method then the program will be something like this:

#include <stdio.h>
Void main ()
{
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");
    printf ("Welcome To CsTutorialpoint \n");

}

Output -:

Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint

If we want to print “Welcome To CsTutorialpoint” 10 times using Loop Statements, then for this we have to write a Loop Statement in which we will give condition and secondly we have to write a printf () Statement by which we will print “Welcome To CsTutorialpoint”.

#include <stdio.h>
int main ()
{
    int i = 1; // initialization expression
  
    while (i<=10) // Loop Condition - Test Expression
    {
        printf ("Welcome ToCsTutorialpoint \n");
 
         i++; // update expression
    }
    return 0;
}

Output -:

Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint
Welcome To CsTutorialpoint

If we print the first way (in which we are printing Welcome To CsTutorialpoint 10 times by writing printf ()) and second ways (in which we are printing Welcome To CsTutorialpoint by writing printf () 1 time).

Talking about both these methods, the loop method is quite easy and time-saving, in that we do not need to write the same code, again and again.

There are three types of loops in C language.

Types of Loop in C

In C language, we can use loop in three ways.

  1. While loop
  2. Do while loop
  3. For loop

1. While Loop

While Loop is used when we do not already know how often to run the loop.

In While Loop, we write the condition in parenthesis “()” after the while keyword. If the condition is true then the control enters the body of the while Loop and runs the statements inside the while loop.

As the control comes to the end of While Loop, the control again goes to While Loop and the condition is checked again and if the condition is true this time, then the statement inside While Loop runs again.

This action continues till the condition of the while Loop does not become False. We also use the increment (++) and increment (-) operators to change the condition of while loop.

While Loop is also called the entry control loop because the condition is checked before the control reaches the block of the while loop, and if the condition is true then the control enters the body of the while Loop and runs the statement inside it.

While Loop’s syntax is something like this:

Syntax

while (condition)
{
// code to be executed
statements;
statements;
.
.
}

2. Do while loop

Do while loop is used when we want to execute the statement inside the Do while loop block once.

We finally check the condition in the Do while loop and if the condition is true then the control again goes to the Do while loop and the statement inside it start executing and if the condition is False then the control is removed from the do while loop and another statement is executed.

Syntax -:

Do
{
// code to be executed
statements;
statements;
statements;
.
.
} while (condition);

3. For Loop

For Loop is used when we have to run a particular statement repeatedly until a particular condition is correct.

In the case of for Loop, we already know how many times the loop will run, so for Loop, is also called a pre-tested loop or open-ended loop.

In for loop, we write both the initialization and control condition of the variable together inside the parentheses “()”. If the condition of For Loop is true, then the statement inside it is run, or else the statement is not run.

Syntax -:

for (initial value; condition; incrementation or decrementation)
{
  statements;
}

Conclusion -:

Friends, I hope that after reading this post today, you have learned what is Loop in C language? And how to use it in C language? 

Friends, I hope you have found the answer of your question and you will not have to search about the Loop in C language.

However, if you want any information related to this post or related to programming language, computer science, then comment below I will clear your all doubts 

Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Loop in C Language. 

To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.

Leave a Comment