C LanguageProgramming

Loops In C Language For, While and Do While

loops in c language
Spread the love

Loop is an important part of any programming language. With the help of loops, we can do repeat any block of code any times we want without typing it again and again. In this article, I will tell you about the Loops in C language and there are three types of loops in C programming.

Also read: Switch Case Statements in C Programming

Loops in C language

loops in c language
  • Entry control Loop: here the condition is passed at the starting of the loop and the loop will not execute if the condition is false. ex: for and while loop.
  • Exit control loop: here condition is passed at the end of the loop and this loop executes once even if the condition is false. ex: do-while loop.

1. For loop

In for loop, we knew the number of iterations beforehand (expression value and condition to terminate the loop). Here first we initialize the value, then passed the condition and in last we either increment(++) or decrement (–) the initialized value as per our condition.

for (initialize expr; condition; update expr)
{    
     // codes or statements we want to execute
}

Also read: C Program For Addition Of Two Integers (Entered By The User)

2.While loop

In the while loop, we only knew about the condition to terminate the execution of the loop. Here we initialize the value before declaring the loop then use while keyword, pass the condition and write our code. Do not forget to increment(++) or decrement (–) the initialized value as per our condition otherwise, the loop will become an infinite loop.

initialize the expression;
while (condition)
{
   // code we want to execute
 
   // update_expression;
}

3.Do-while loop

It is an exit control loop which means this loop will execute at least once even if the condition is false. First, we initialize the value before declaring the loop. Now declare the loop write the code and in last passed the condition. Do not forget to increment(++) or decrement (–) the initialized value as per our condition otherwise, the loop will become an infinite loop.

initialize expression;
do
{
   // code

   //update_expression;
} while (condition);
important point about loops in c language


Spread the love
Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.