0% found this document useful (0 votes)
2 views

C Loops

The document explains the concept of loops in the C programming language, highlighting their purpose in simplifying complex problems by allowing code to be reused. It describes three types of loops: do-while, while, and for, providing syntax and examples for each. The advantages of using loops include code reusability and the ability to traverse data structures.

Uploaded by

Mr. RAVI KUMAR I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Loops

The document explains the concept of loops in the C programming language, highlighting their purpose in simplifying complex problems by allowing code to be reused. It describes three types of loops: do-while, while, and for, providing syntax and examples for each. The advantages of using loops include code reusability and the ability to traverse data structures.

Uploaded by

Mr. RAVI KUMAR I
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C Loops

The looping can be defined as repeating the same process multiple times until a
specific condition satisfies. There are three types of loops used in the C language

Use loops in C language

The looping simplifies the complex problems into the easy ones. It enables us to
alter the flow of the program so that instead of writing the same code again and
again, we can repeat the same code for a finite number of times. For example, if we
need to print the first 10 natural numbers then, instead of using the printf statement
10 times, we can print inside a loop which runs up to 10 iterations.

Advantage of loops in C
1) It provides code reusability.

2) Using loops, we do not need to write the same code again and again.

3) Using loops, we can traverse over the elements of data structures (array or linked
lists).

Types of loops

There are three types of loops in C language that is given below:

1. do while

2. while

3. for

do-while loop in C
The do-while loop continues until a given condition satisfies. It is also called post
tested loop. It is used when it is necessary to execute the loop at least once (mostly
menu driven programs).
The syntax of do-while loop in c language is given below:

1. do{
2. //code to be executed
3. }while(condition);

Example of do while loop

do-while loop in C example to print a table of number 2:

#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}

Output:

2
4
6
8
10
12
14
16
18
20
while loop in C
The while loop in c is to be used in the scenario where we don't know the number of
iterations in advance. The block of statements is executed in the while loop until the
condition specified in the while loop is satisfied. It is also called a pre-tested loop.

The syntax of while loop in c language is given below:

1. while(condition){
2. //code to be executed
3. }

Example of while loop

// Print numbers from 1 to 5

#include <stdio.h>
int main()
{
int i = 1;

while (i <= 5)
{
printf("%d\n", i);
++i;
}

return 0;
}

Output

1
2
3
4
5
for loop in C
The for loop is used in the case where we need to execute some part of the code
until the given condition is satisfied. The for loop is also called as a per-tested loop.
It is better to use for loop if the number of iteration is known in advance.

The syntax of for loop in c language is given below:

1. for(initialization;condition;incr/decr){
2. //code to be executed
3. }

Example of for loop


#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

The above program prints the number series from 1-10 using for loop.

You might also like