Looping
statements in CPresented by,
S.Jeyalakshmi B.E.,
Sr.Lecturer/CE
MSPVL Polytechnic college, Pavoorchatram
Repetition
You Will Want to display HAI
More Than Once
HAI
HAI
HAI
HAI
HAI
HAI
HAI
HAI
You Could Code The ‘HAI’
Like This
printf("HAI");
printf(" HAI ");
printf(" HAI ");
printf(" HAI ");
printf(" HAI ");
printf(" HAI ");
printf(" HAI ");
printf(" HAI ");
No good programmer
does this!
Loop
• Don’t need to write this code 8 times.
• A loop is a piece of code which allows you to repeat
some code more than once without having to write it out
more than once.
printf("HAI ");
Looping statements
• Loops provide a way to repeat commands and control
how many times they are repeated.
• 3 types.
• while
• do-while
• for
while ( expression )
{
Single statement
or
Block of statements;
}
While
Syntax
7
Flow chart
If the test condition is true: the
statements get executed until the
condition becomes false.
While - Example
#include <stdio.h>
main()
{
int i = 0;
while ( i < 10 )
{
printf("Hello %dn", i );
i ++;
}
}
OUTPUT
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
do
{
Single statement
or
Block of statements;
}while ( expression )
Do While
Syntax
9
Flow chart
The statements get executed once.
Then the condition is evaluated. If the
test condition is true: until the
condition becomes false.
Do While - Example
#include <stdio.h>
main()
{
int i = 0;
do
{
printf("Hello %dn", i );
i ++;
} while ( i < 10 )
}
OUTPUT
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
for (initialization_expression;loop_condition;increment_expression)
{
// statements
}
)
for
Syntax
11
Flow chart
for- Example
#include <stdio.h>
main()
{
int i;
for( i = 0; i <10; i ++ )
{
printf("Hello %dn", i );
}
}
OUTPUT
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Break
• It is used in terminating the loop immediately after it is
encountered.
• It can be used inside a switch, for loop, while loop and
do-while loop.
Break- Example
#include <stdio.h>
main()
{
int i;
for( i = 0; i <10; i ++ )
{
printf("Hello %dn", i );
if(i==4)
break;
}
}
OUTPUT
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Continue
• It is sometimes desirable to skip some statements inside
the loop.
• In such cases, continue statements are used.
Continue - Example
#include <stdio.h>
main()
{
int i;
for( i = 0; i <10; i ++ )
{
printf("Hello %dt", i );
if(i==4)
continue;
printf(“Welcomen”);
}
}
OUTPUT
Hello 0 Welcome
Hello 1 Welcome
Hello 2 Welcome
Hello 3 Welcome
Hello 4
Hello 5 Welcome
Hello 6 Welcome
Hello 7 Welcome
Hello 8 Welcome
Hello 9 Welcome

Looping statements in C

  • 1.
    Looping statements in CPresentedby, S.Jeyalakshmi B.E., Sr.Lecturer/CE MSPVL Polytechnic college, Pavoorchatram
  • 2.
  • 3.
    You Will Wantto display HAI More Than Once HAI HAI HAI HAI HAI HAI HAI HAI
  • 4.
    You Could CodeThe ‘HAI’ Like This printf("HAI"); printf(" HAI "); printf(" HAI "); printf(" HAI "); printf(" HAI "); printf(" HAI "); printf(" HAI "); printf(" HAI "); No good programmer does this!
  • 5.
    Loop • Don’t needto write this code 8 times. • A loop is a piece of code which allows you to repeat some code more than once without having to write it out more than once. printf("HAI ");
  • 6.
    Looping statements • Loopsprovide a way to repeat commands and control how many times they are repeated. • 3 types. • while • do-while • for
  • 7.
    while ( expression) { Single statement or Block of statements; } While Syntax 7 Flow chart If the test condition is true: the statements get executed until the condition becomes false.
  • 8.
    While - Example #include<stdio.h> main() { int i = 0; while ( i < 10 ) { printf("Hello %dn", i ); i ++; } } OUTPUT Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9
  • 9.
    do { Single statement or Block ofstatements; }while ( expression ) Do While Syntax 9 Flow chart The statements get executed once. Then the condition is evaluated. If the test condition is true: until the condition becomes false.
  • 10.
    Do While -Example #include <stdio.h> main() { int i = 0; do { printf("Hello %dn", i ); i ++; } while ( i < 10 ) } OUTPUT Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9
  • 11.
  • 12.
    for- Example #include <stdio.h> main() { inti; for( i = 0; i <10; i ++ ) { printf("Hello %dn", i ); } } OUTPUT Hello 0 Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 Hello 7 Hello 8 Hello 9
  • 13.
    Break • It isused in terminating the loop immediately after it is encountered. • It can be used inside a switch, for loop, while loop and do-while loop.
  • 14.
    Break- Example #include <stdio.h> main() { inti; for( i = 0; i <10; i ++ ) { printf("Hello %dn", i ); if(i==4) break; } } OUTPUT Hello 0 Hello 1 Hello 2 Hello 3 Hello 4
  • 15.
    Continue • It issometimes desirable to skip some statements inside the loop. • In such cases, continue statements are used.
  • 16.
    Continue - Example #include<stdio.h> main() { int i; for( i = 0; i <10; i ++ ) { printf("Hello %dt", i ); if(i==4) continue; printf(“Welcomen”); } } OUTPUT Hello 0 Welcome Hello 1 Welcome Hello 2 Welcome Hello 3 Welcome Hello 4 Hello 5 Welcome Hello 6 Welcome Hello 7 Welcome Hello 8 Welcome Hello 9 Welcome