Chapter 2
Repetition (Loop)
The looping can be defined as repeating the same process multiple times until a
specific condition satisfies.
Types of C Loops
There are three types of loops in C language that is given below:
• For
• While
• Do while
syntax:
for (initialization Statement; test Expression; update Statement)
// statements inside the body of loop
LOOP LAKSHMI S HANNE
Example 1 : Write a C Program to print the number from 1 to 10
2] write the c program to calculate the sum of first n natural number.
LOOP LAKSHMI S HANNE
3] write a c program to print the factorial of the given number.
LOOP LAKSHMI S HANNE
While loop:
• The while loop evaluates the test expression inside the parenthesis ().
• If the test expression is true, statements inside the body of while loop are executed.
Then, the test expression is evaluated again.
• The process goes on until the test expression is evaluated to false.
• If the test expression is false, the loop terminates (ends).
Syntax of while loop in C language
The syntax of while loop in c language is given below:
while(condition)
{
//code to be executed
}
LOOP LAKSHMI S HANNE
Examples:
Program to print the numbers from 0 to 10.
LOOP LAKSHMI S HANNE
2] write a c Program to print only the odd numbers.
LOOP LAKSHMI S HANNE
3] write the c program to reverse the given number.
LOOP LAKSHMI S HANNE
4] Write a C Program to find the sum of the digit.
LOOP LAKSHMI S HANNE
DO WHILE LOOP IN C
The do while loop is a post tested loop. Using the do-while loop, we can repeat the
execution of several parts of the statements. The do-while loop is mainly used in the case
where we need to execute the loop at least once.
do while loop syntax
The syntax of the C language do-while loop is given below:
do
//code to be executed
while(condition);
LOOP LAKSHMI S HANNE
1] Write a C Program to add numbers until the user enters zero.
LOOP LAKSHMI S HANNE
2] Write a C Program to print the table of given number.
LOOP LAKSHMI S HANNE