What is Loop

C offers us three ways to perform a loop: for loops, while loops and do while
loops.

They all allow you to iterate over arrays, but with a few differences.
Let’s see them in details.

In programming, a loop is a sequence of statements which is use to repeat a set of statement until a specific condition met.

By the example, we can understand in an easy way without using loop

#include <stdio.h>
#include <conio.h>
void main()
 { int x=10;
 printf("Hi how are your"); 
}

Suppose that you want to print 10,20,100,1000 times then just think it’s possible but very difficult to type or copy-paste. By using loop we can print many types only two or three line of code.
There are three types of loop used in c.

  • for
  • while
  • do while

For Loop In C

The first, and probably most common, way to perform a loop is for loops.
Using the for keyword we can define the rules of the loop up front,

and then provide the block that is going to be executed repeatedly.
Like this:

for (int i = 0; i <= 10; i++) {
/* instructions to be repeated */
}

The (int i = 0; i <= 10; i++) block contains 3 parts of the looping details.

  • the initial condition ( int i = 0 )
  • the test ( i <= 10 )
  • the increment ( i++ )

We first define a loop variable, in this case named i . i is a common variable name to be used for loops, along with j for nested loops

(a loop inside another loop). It’s just a convention.The variable is initialized at the 0 value, and the first iteration is done.

Then it is incremented as the increment part says ( i++ in this case, incrementing by
1), and all the cycle repeats until you get to the number 10.

Inside the loop main block we can access the variable i to know at which
iteration we are. This program should print 0 1 2 3 4 5 5 6 7 8 9 10 .

for (int i = 0; i <= 10; i++) {
/* instructions to be repeated */
printf("%u ", i);
}

Loops can also start from a high number, and go a lower number, like this:


for (int i = 10; i > 0; i--) {
/* instructions to be repeated */
}

You can also increment the loop variable by 2 or another value:

for (int i = 0; i < 1000; i = i + 30) {
/* instructions to be repeated */
}

Or loop used for executes for a group of statement repetition until condition not reached.

First, initialize then check print statement and after increment/decrement then check a condition and again print statement.

the syntax of – for loop in c.

for(initilization;condition;incr/decr)
{
statement/expression;
}

A simple example of – for loop in c.
Print Codeverb.com message for 5 times by using for loop

#include <stdio.h>
#include<conio.h>
void main(){
int i;
clrscr();
for(i=1;i<=5;i++)
{
printf>(“Codeverb.com %dn”,i);
}
getch();
}

Output
Codeverb.com 1
Codeverb.com 2
Codeverb.com 3
Codeverb.com 4
Codeverb.com 5

This article is written by our awesome writer
Comments to: Loops In C Programming Language

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Explore Tutorials By Categories

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password