Loop Statements In C.

Today our c programming tutorial for beginners tutorial is going to be about loop statements in c. In our last tutorial where we learned about how to determine gcd and lcm of 2 numbers in c. To do so, we have used something like while(condition){…}. This is actually a while loop statement. Today I will try to introduce you to loop statements.

 

Following things I will try to cover in this tutorial:

 

  • What Is A Loop Statement?
  • Types Of Loop Statements.
  • Nested Loops.
  • Why We Should Use Loop Statement?
  • Example Using Different Loop Statements.

 

 

 

 

What Is A Loop Statement?

Actually, a loop statement is a process that executes an operation continuously and repeatedly under a given condition. Suppose you need to perform a task repeatedly 100 times. That will be very much difficult and time-consuming for you to do this by writing the task 100 times. So, for this type of task, you can easily use a loop statement.

 
 
 
 
 
Loop statements in c.

 

 
 

 

Types Of Loop Statements:

 

In c there are generally 3 loop statements. They are:

1) For loop:

For loop is the most favorite and used loop for c programmers. Under a for loop, you have to initialize a variable first. Then you should create a condition. If that condition is true then the loop will process otherwise terminate. Then you should increase or decrease the value of the initialized variable. So, the syntax of for loop is:

for(initialization; condition; increment or decrement)

{

    statement;

}

 

2)While loop:

While loop is also an important loop statement in c. For using while loop you must initialize the variable before the loop or at the start of the main function. Then test the condition. If the condition is true then do the loop process and then increase or decrease the variable which was initialized before the while loop. So, the syntax for a while loop is:

initialization;

while(condition)

{

    statement;

    increment or decrement;

}

 

3)do..while loop:

For this loop, actually at first loop operation was done then the condition were checked. It was similar to the while loop. Just the condition was checked at the output section. Syntax for while loop is:

initialization;

do

{

    statement;

    increment or decrement;

}while(condition);

 

Actually, loop statements are 2 types. They are:

1)Entry Control Loop: 

This kind of loop first tests the condition. If the condition is true then it executes the operation means to enter the loop statement. Otherwise, it terminates the loop process. For example: For loop generally is an entry control loop, while loop is definitely an entry control loop as it tests the condition first then enters the loop body.

 

 

 

 

2) Exit Control Loop: 

This is the type of loop which at first executes the loop body once and then tests the condition. If the condition is true then again execute the loop operation otherwise it will terminate the operation. For this kind of loop statement operation into the loop, the body should be performed at least once whatever the condition is. do..while is an exit control loop. In some cases, for loop could be used for the exit control loop. To use a for loop as an exit control loop its syntax should be like this:

for(initialization; ; increment or decrement)

{

    statement;

    if(condition for terminating the operation)

        {

          break;

       }

 

 

 

 

 

Nested Loops:

You can use two or more loops under two or more loops. Using one or more loops under one or more loops is called the nested loop. For example, we have to do a task for the 10 conditions 20 times. Suppose we want to print multiplication of 1 to 10 by 1 to 5. So, using for loop our code will be:

#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=10;i++)
{
for(j=1;j<=5;j++)
{
printf(“%d*%d=%dn”,i,j,i*j);
}
}
}

The output of this code will be like this:

 

1*1=1

1*2=2

1*3=3

1*4=4

1*5=5

2*1=2

2*2=4

2*3=6

2*4=8

2*5=10

3*1=3

3*2=6

3*3=9

3*4=12

3*5=15

4*1=4

4*2=8

4*3=12

4*4=16

4*5=20

5*1=5

5*2=10

5*3=15

5*4=20

5*5=25

6*1=6

6*2=12

6*3=18

6*4=24

6*5=30

7*1=7

7*2=14

7*3=21

7*4=28

7*5=35

8*1=8

8*2=16

8*3=24

8*4=32

8*5=40

9*1=9

9*2=18

9*3=27

9*4=36

9*5=45

10*1=10

10*2=20

10*3=30

10*4=40

 

10*5=50

 

 

 

 

 

You can also do this task using the while loop statement. Try to do this by yourself. Don’t worry just try to do this by yourself after reading the whole tutorial. So at first, try to understand the rest of the tutorial then do this.

 

So, for nested for loop syntax should be:

for()

{

    for()

    {

        statement;

    }

.

.

.

}

 

For nested while loop syntax should be:

initialization of variable-1;

while(condition-1)

{

    initialization of variable-2;

    while(condition-2)

        {

            statement;

            increment or decrement of variable-2;

        }

    increment or decrement of variable-2;

.

.

.

}

 

 

 

 

 

Why We Should Use The Loop Statement?

 

Suppose I am telling you to write a code that will print 1 to 10. Then you will easily do this by writing the following code:

#include <stdio.h>

int main()

{

printf(“1n”);

printf(“2n”);

printf(“3n”);

printf(“4n”);

printf(“5n”);

printf(“6n”);

printf(“7n”);

printf(“8n”);

printf(“9n”);

printf(“10n”);

return 0;

}

 

 

 

 

But if I told you to write a code to print 1 to 10000. Then what will you do? Of course,  you will not go to write printf function 10000 times for printing 1 to 10000. For this, you must use a loop statement.

Code:

#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=10000;i++)
{
printf(“%dn”,i);
}
}

 

You see for which you have to write 10000 lines of code I just did it by writing 3 lines of code. 😎

I think you do have not any questions anymore about why we should use loop statements.

 

 

 

 

 

 

Example Using Different Loop Statements:

Consider the following problem:

1+3+5+….+99 this is a parallel section. Write a code in c that will print the summation of this parallel section.

Code using for loop:

#include<stdio.h>
int main()
{
int i,j,sum=0;
for(i=1;i<100;i+=2)
{
sum+=i;
}
printf(“%dn”,sum);
}

Here, as the sequence starts from 1 we have initialized i like 1. As the section ended at 99 which is less than 100. So, we set the condition i<100. You can use i<=99 no problem. So, until i is less than 100 this loop will execute and after 99 i will be 101 and the loop will be terminated. As the difference between each next element is 2. So, we increase the variable i after each operation by 2.

We stored the summation of the series in the sum variable. So, at the beginning sum was 0 which was initialized before the loop. Also, you can initialize it in the for loop in the initialization section. This for loop will be like:

for(i=1, sum=0;i<100;i++)

 

Then we sum all the elements one by one by i and store the summations on sum variable. And after summing all the elements from 1 to 99 of the series. We print it after the for loop.

Here is the code using the while and do while loop.

 

Code using while loop:

#include<stdio.h>
int main()
{
int i,j,sum=0;
i=1;
while(i<100)
{
sum+=i;
i+=2;
}
printf(“%dn”,sum);
}

Code using do…while loop:

#include<stdio.h>
int main()
{
int i,j,sum=0;
i=1;
do
{
sum+=i;
i+=2;
}while(i<100);
printf(“%dn”,sum);
}

 

The output of these programs is 2500. Which is the summation of the given parallel section.

Now, I am giving you one task try to do this by yourself using the 3 loop statements.

Problem: 1^2 + 2^2 + 3^2 + …..+ n^2 is a section of number where n is a positive integer (less than 1000). Write a code in c which will print the sum of the section.

 

Of course, try to solve this problem by yourself. And then submit your code to the comment box. If you couldn’t then see my next tutorial I will discuss this problem and show you the code for this problem in my next tutorial. So, stay well. Stay home. Wear a mask and follow all the sanitizing rules strictly. Stay safe. Happy coding.

 

Similar Posts

Leave a Reply

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