Looping statements in c language

Looping statements in c language with examples – loops in c language

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Looping statements in c language

If I tell you that you want to print Looping statements in c language 10 times, how will you do this? Generally: To do this you will write 10 times printf () statements. This is a useless approach. Doing this, the programmer waste time and memory of the computer.

You have provided loops in C language. With the help of loops, you can execute the same statement repeatedly. Each type of loop provides a block in which statements are written which you want to execute more than once.

The Looping statements in c language made up of 3 things.

  • Initial variable – this is the variable where you start the loop. This is an integer variable. This variable is incremented until the given condition is false. This variable is included in the condition of the loop.
  • Condition – This is the condition that controls the loop. As long as the condition remains true, the loop keeps executing. As soon as this condition is false the loop gets terminate.
  • Increment/Decrement – How much you want to increment from or how to initial variable/condition is defined in this increment part.

Types of Looping statements in c language

There are 3 types of Looping statements in c language
  • While loop
  • Do-while loop
  • For loop
Now let’s try to know about the loops in C language.
Looping statements in c language

While Loops in C language

The first Looping statements in c language is While loops in C language is a simple loop, it executes as long as the condition remains true. When the condition is false, this loop gets terminated. The general syntax of while loop is being given below.
initial variable declaration;
while(condition)
{
            //statements
           //increment
}
while loops in c language
If the condition is false at the first time then the compiler does not enter into the loop. The loop is completely skipped. Let’s try to understand while loops in c language with an example.
#include<stdio.h>
void main()
{
   int num=1;
   while(num<=10)
   {
         printf(“%d\t”,num);
         num++;
   }
}

 

In the above example, the first num variable is set with value 1. The condition inside the loop has been given that until the num works 10 or not equal to 10 the loop is executed. The values of num variable are being printed in every iteration inside the loop. After this, the num variable is being incremented. This program generates the given below output.

1  2  3  4  5  6  7  8  9  10

If the initial variable is not incremented then the condition will never be false. In this case, the loop will run until infinite time.

 

Do-while Loops in C language

This is second Looping statements in c language, Do while loops in C language is similar to the while loops in C language. But instead of checking the first condition in this loop, the first statements are executed, then there is a condition check. But this is just the first time. The general syntax of Do while loop is being given below.
do
{
    //statements;
   //increment;
}while(condition);
do while loops in c language

As soon as the compiler comes in the section, all the statements of doing block are executed and the initial variable is incremented. After this, the compiler checks the condition. If the condition is true then do block is executed back. But if the condition is false, then the compiler moves forward by terminating the loop.

Whether the condition of whether the condition is true or false do block statements are at once executed. Let’s try to understand the do while loop with an example.

#include<stdio.h>
void main()
{
     int num=1;
     do
     {
            printf(“%d\n”,num);
            num++;
     }while(num>2);
    printf(“Num is not greater than 2 that is why loop terminated!!”);
}
In the above example, although the condition is false still the statement given in the do block will certainly execute once. This program generates the given output below.
1
Num is not greater than 2 that is why loop terminated!!

For Loops in C language

This is third Looping statements in c language, For loops in C language, the most used for loops are done. This loop is very easy and gets defined in a single statement. The general structure of this loop is given below.
for(initialvariable;condition;increment)
{
       //statements
}
for loops in c language
The special thing about for loop is that you define the three elements in a statement. This loop has been explained by the example below.
#include<stdio.h>
void main()
{
    int num;
        for(num=1;num<=10;num++)
        {
              printf(“%d”,num);
        }
}

The above example produces the given below output.

1  2  3  4  5  6  7  8  9

C Loop Control Statements

Loop control statements are statements that change the execution of the loops in C language according to any condition. These statements are very useful for changing the loop accordingly.

The following information about available control Looping statements in c language is being explained.

break statement

The break statement is used to terminate the Looping statements in c language. When a break statement is executed inside a loop, the loop is terminated at the same time and the execution of the next statement begins after that loop.

The general syntax of the C break statement is being given below.

break;
A simple example is given below.
#include<stdio.h>void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
{
printf(“Loop Terminated…”);
break;
}
printf(“%d\n”,i);
}
}

In the above example, as soon as the third iteration of the loop comes, the break statement is execute and the loop gets terminated. This example produces the below given output.

1
2
Loop Terminated…

 

continue Statement

The continuation of the statement in the C language is used to skip the iteration of the loop. For example, if you want the third iteration of the loop not to be executed, you can skip it by using the condition by identifying the third iteration by the condition.

The general syntax of the C continue statement is being given below.

continue;
A simple example is given below.
#include<stdio.h>void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
{
printf(“Third iteration skipped…\n”);
continue;
}
printf(“%d\n”,i);
}
}

 

The above example produces the given below output.
1
2
The third iteration skipped…
4
5

 

goto Statement

C language is used to jump from place to place in the goto statement program. This jump is performed on the defining label.

The general syntax of the C goto statement is being given below.



goto label-name;


label-name:

As you can see in the syntax given above, to define the label, the name of the label is written by a colon (:). The name of the label is written after the goto statement to jump on the label.
The lower goto statement is being explained by a simple example.
#include<stdio.h>void main()
{
printf(“I dont want to print Hello. Jumping…\n”);
goto hi;
printf(“Hello\n”);
hi:
printf(“Hi\n”);
}

 

The above example produces the given below output.
I don’t want to print Hello. Jumping…
Hi

 


Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

10 comments

  1. I love this blog . This is one of the best blog i ever seen. It’s all about what i’m searching for. I love to read this blog again and again . Every time i enter this blog i get something new. This blog inspire me to write new blog.

Leave a Reply

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