Best post to learn loops in C part 2-for loop

Loops in C

In programming there will be some statements or group of statements which we want to execute continuously for more than one time, this is where loops comes in the picture.

Loop allow us to continuously execute particular statement or block of statement to more than one time.

Loop will check for the condition, and loop will continuously execute particular statement or block of statement until the condition get false or invalid.

Following are the types of loops in C program.

  1. while loop
  2. do-while loop
  3. for loop
  4. nested loop

for loop

for loop has 3 parts in parameters.

  1. initialization
  2. condition
  3. increment or decrement

for loop is more control loop among all. We can mention condition and increment or decrement with initialization.

This loop has a counter variable.

In C program we can only initialized the value of counter variable, we are not allowed to declare the variable, deceleration of variable is allowed in C99 and C11 mode.

loops-in-c-for-loop-itvoyagers

Syntax:

    for(initialization ; condition ; increment or decrement)
  {
  block_of_code;
   }

Example:

    #include<stdio.h>
void main()
{
int num;
for(num=1; num<=5; num++)
{
print("%d ITVoyagersn", num);
}
}

Output:

    1 ITVoyagers
2 ITVoyagers
3 ITVoyagers
4 ITVoyagers
5 ITVoyagers

Explanation

    In above program we have declared a int variable num and in for loop we initialized value 1 to num variable. When for loop will start it’s iteration it will check if condition is valid, in this case yes it is valid, so for loop will start it’s first iteration.

    Iteration 1 :

         When value of num is 1 for loop will check if the condition is true.

         Now since the condition is true for loop will start executing block of code.

         First it will print “1 ITVoyagers”

         Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1. 

    Iteration 2 :

         Now value of num is 2, for loop will check if the condition is true.

         Now since the condition is true for loop will start executing block of code.

         First it will print “2 ITVoyagers”

         Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1.

  Iteration 3 :

 

         Now value of num is 3, for loop will check if the condition is true.

         Now since the condition is true for loop will start executing block of code.

         First it will print “3 ITVoyagers”

         Now before starting next iteration for loop will upgrade the counter, in this case it will increment the counter value by 1.

  .

    .

    .

This will go on till 5th iteration. In 5th iteration value of num will increment to 6.

    Iteration 6 :

         Now value of num is 6.

         Since condition is not valid while loop will stop iteration and compiler will get out of the loop

Try this fun quiz on for loop

ITVoyagers

Nested loop

As name says nesting one loop in another loop is called as nested loop.

Example:

    #include<stdio.h>
void main()
{
int num1 = 1, num2 = 1;

do
{
while(num>5)
{
printf("%d itvoyagers.inn", num2);
num2++;
}
print("%d ITVoyagersn", num1);
}
while(num1>5);
}

Output:

    1 itvoyagers.in
2 itvoyagers.in
3 itvoyagers.in
4 itvoyagers.in
5 itvoyagers.in
1 ITVoyagers

Explanation

    We have while loop nested in do-while loop.

    Iteration 1 :

         When value of num1 is 1 do-while loop will first execute its block of code and then it will check if condition is true or not.

         First it will run inner while loop

                 Inner Iteration 1 :

                     When value of num2 is 1 while loop will check if the condition is true.

                     Now since the condition is true while loop will start executing block of code.

                     First it will print “1 ITVoyagers”

                     Next it will increment the value of num2.

                Inner Iteration 2 :

                     Now value of num2 is 2 while loop will check if the condition is true.

                     Now since the condition is true while loop will start executing block of code.

                     First it will print “2 ITVoyagers”

                     Next it will increment the value of num2.

                     .

                     .

                     .

                        This will go on till 5th iteration. In 5th iteration value of num2 will increment to 6.

                Inner Iteration 6 :

                     Now value of num2 is 6.

                     Since condition is not valid while loop will stop iteration and compiler will get out of the loop

         Now compiler is out of inner loop i.e. while loop.

 

         Next it will print “1 ITVoyagers”

         Now it will check if condition is true or not. Since the condition is false compiler will get out of do-while loop.

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment