Loops in c Program
Loops in c Program
WHAT IS A LOOP?
• A loop is used to repeatedly execute a
block of code as long as given
condition is true.
• Loops make coding simpler by letting
you repeat actions without writing the
same lines of code over and over
again.
3
TYPES OF LOOPS IN C
1. For Loop
2. While Loop
3. Do-while Loop
FOR
SYNTAX:
• For(initialization;condition;increment/
decrement)
• {
• // code block
• }
5
WHILE
• A While loop repeats a block of code as long
as given condition is true. Before each run it
checks the condition and if its true the code
inside runs. If the condition becomes false
the loop stops.
SYNTAX
• While(condition)
• {
• // code block
• }
7
EXAMPLE:WHILE LOOP
• int i=0;
• while(i<5)
• {
• printf(“%d\n” , i); Click icon to add picture
• i++;
• }
8
DO-WHILE
• Do-while loop is similar to the while loop but it runs the code inside
it at least once no matter what. After the code runs, it checks the
condition if the condition is true the loop repeats if its false the
loop stops
SYNTAX:
• do
• {
• // code block
• }
• while(condition);
9
EXAMPLE:DO-WHILE LOOP
• int i=0;
• do
• {
• printf(“%d\n” , i);
• i++;
• }
• while(i<5);
10
DIFFERENCE BETWEEN
FOR,DO,DO-WHILE LOOPS
THANK YOU