Looping Statements in C
Prof. Navya Francis
Asst. Professor
Kristu Jayanti College
• Loops in programming are used to repeat a block of code until the
specified condition is met.
• A loop statement allows programmers to execute a statement or
group of statements multiple times without repetition of code.
Introduction
Two types of loops in C Programming:
• Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the
main body of the loop.
• For Loop and While Loop is Entry-controlled loops.
• Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop
body. The loop body will execute at least once, irrespective of whether the condition is true or
false.
• do-while Loop is Exit Controlled loop.
For Loop
• For loop in C programming is a repetition control structure that allows
programmers to write a loop that will be executed a specific number of
times.
• For loop enables programmers to perform n number of steps together
in a single line.
Syntax:
for(initialization;condition;increment/decrement)
{
statement(s);
}
Flowchart
while (test_expression) { // body of the while loop update_expression; }
while (test_expression) { // body of the while loop update_expression; }
Example 1:
#include <stdio.h>
void main ()
{
int a;
for( a = 10; a <=15; a++ )
{
printf("value of a: %dn", a);
}
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
While Loop
• While loop does not depend upon the number of iterations.
• In while loop, the execution is terminated on the basis of the test
condition.
• If the test condition will become false then it will break from the while
loop else body will be executed.
Syntax:
while(condition)
{
// Body of the while loop
update expression;
}
Flowchart
while (test_expression) { // body of the while loop update_expression; }
while (test_expression) { // body of the while loop update_expression; }
Example 1:
#include <stdio.h>
void main ()
{
int a = 10;
while( a <= 15 )
{
printf("value of a: %dn", a);
a++;
}
}
Output
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
Do-While Loop
• The do-while loop is similar to a while loop but the only difference lies in
the do-while loop test condition which is tested at the end of the body.
• In the do-while loop, the loop body will execute at least
once irrespective of the test condition.
Syntax:
do
{
statement(s);
}while(condition);
Flowchart
while (test_expression) { // body of the while loop update_expression; }
while (test_expression) { // body of the while loop update_expression; }
Example 1:
#include <stdio.h>
void main ()
{
int a = 10;
do
{
printf("value of a: %dn", a);
a = a + 1;
} while( a <=15);
}
Output
• value of a: 10
• value of a: 11
• value of a: 12
• value of a: 13
• value of a: 14
• value of a: 15
Thank You

C Programming: Looping Statements in C Pgm

  • 1.
    Looping Statements inC Prof. Navya Francis Asst. Professor Kristu Jayanti College
  • 2.
    • Loops inprogramming are used to repeat a block of code until the specified condition is met. • A loop statement allows programmers to execute a statement or group of statements multiple times without repetition of code. Introduction
  • 3.
    Two types ofloops in C Programming: • Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop. • For Loop and While Loop is Entry-controlled loops. • Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. • do-while Loop is Exit Controlled loop.
  • 4.
    For Loop • Forloop in C programming is a repetition control structure that allows programmers to write a loop that will be executed a specific number of times. • For loop enables programmers to perform n number of steps together in a single line.
  • 5.
    Syntax: for(initialization;condition;increment/decrement) { statement(s); } Flowchart while (test_expression) {// body of the while loop update_expression; } while (test_expression) { // body of the while loop update_expression; }
  • 6.
    Example 1: #include <stdio.h> voidmain () { int a; for( a = 10; a <=15; a++ ) { printf("value of a: %dn", a); } } Output value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
  • 7.
    While Loop • Whileloop does not depend upon the number of iterations. • In while loop, the execution is terminated on the basis of the test condition. • If the test condition will become false then it will break from the while loop else body will be executed.
  • 8.
    Syntax: while(condition) { // Body ofthe while loop update expression; } Flowchart while (test_expression) { // body of the while loop update_expression; } while (test_expression) { // body of the while loop update_expression; }
  • 9.
    Example 1: #include <stdio.h> voidmain () { int a = 10; while( a <= 15 ) { printf("value of a: %dn", a); a++; } } Output value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
  • 10.
    Do-While Loop • Thedo-while loop is similar to a while loop but the only difference lies in the do-while loop test condition which is tested at the end of the body. • In the do-while loop, the loop body will execute at least once irrespective of the test condition.
  • 11.
    Syntax: do { statement(s); }while(condition); Flowchart while (test_expression) {// body of the while loop update_expression; } while (test_expression) { // body of the while loop update_expression; }
  • 12.
    Example 1: #include <stdio.h> voidmain () { int a = 10; do { printf("value of a: %dn", a); a = a + 1; } while( a <=15); } Output • value of a: 10 • value of a: 11 • value of a: 12 • value of a: 13 • value of a: 14 • value of a: 15
  • 13.