You are on page 1of 27

CONTROL STATEMENTS

(LOOPING) WHILE, DO..WHILE, FOR


LOOP
MODULE NAME: Basic C Programming
MODULE CODE: CS 6224
FACILITATOR: Pangahela, R. A
Email: pangriz@gmail.com
Phone: 0742 926 569
WEEK – 5
1.Control Statements (Looping) While, Do While, For Loop
2.Sample C Programs
CONTROL STATEMENTS (LOOPING)
Loop is a mechanism through which you repeatedly
execute a set of statements. In looping, a sequence of statements
is executed until some condition for termination of the loop is
satisfied.
A program loop therefore consists of two segments, one
the body of the loop & other the control statement. The control
statement tests certain conditions and then directs the repeated
execution of statements contained in body of the loop.
Depending on the position of the control statement in the loop, a
control structure may be classified either as entry-controlled
loop or exit-controlled loop.
 In entry-controlled loop, the control conditions are tested
before start of the execution. If conditions are not
satisfied, then body of the loop will not be executed.
 In exit-controlled loop, the test is performed at the end of
the body of the loop and therefore the body is executed
unconditionally for the first time.
The test conditions should be carefully stated in order to perform
the desired number of loop executions. It is assumed that the test
condition will eventually transfer the control out of the loop. In
case, due to some reason it does not do so, the control sets up an
infinite loop and the body is executed over and over again.
A looping process, in general, would include the following four
steps:
1. Setting & Initialization of a counter
2. Execution of the statements in the loop
3. Test for a specified condition for execution of the loop
4. Updating the counter
The test may be either to determine whether the loop has been
repeated the specified number of times or to determine whether
a particular condition has been met.
The C language provides the following loop constructs:
(a) while statement
(b) do..while statement
(c) for statement
The WHILE Statement
 The simplest of all looping structures is the while (an
entry-controlled loop) statement.
 The basic format of while statement is
Syntax:
while(test_condition)
{
statement;
}
test_condition Is any valid C condition. Statement is
repeatedly executed as long as condition is
true. Once the condition is false, loop is
terminated and control is transferred to the
statement that is immediately after the loop
statement (Body of the loop) May be either a single
statement or a compound statement.
 On exit, the program continues with the statement
immediately after the body of the loop.
Example 1: To print the message 5 times
/*Print message 10 times*/
#include<stdio.h>
main()
{
int i=1; Output:
while(i<=5) WELCOME
{ WELCOME
printf("\nWELCOME"); WELCOME
i++; WELCOME
} WELCOME
return 0; }
Example 2: To print 1 to 10 numbers.
Output:
/*Print 1 to 10 numbers*/
1
#include<stdio.h>
2
main()
3
{
4
int i=1;
5
while(i<=10)
6
{
7
printf("\n%d",i);
8
i++;
9
}
10
return 0; }
Example 3: To print 1 to 10 odd numbers.
/*Print 1 to 10 numbers*/
#include<stdio.h>
main()
{
int i=1; Output:
while(i<=10) { 1
printf("\n%d",i); 3
i+ = 2; 5
} 7
return 0; 9
}
Example 4: Program for finding sum of series: 1+2+3+4+
……………..+n
#include <stdio.h>
int main()
{
int i=1;
int n,sum=0;
printf(“Give the Number of n:”);
scanf(“%d”,&n);
while ( i<= n)
{
sum = sum+i;
i=i+1;
}
printf(“The sum of the series is: %d”,sum);
return 0;
}

If you run this program and give 7 as value of n then it will


print: The sum of the series is: 28
Example 5: Write a program to count backwards, from 20 to 1 (use
while loop) and display “MUST” each time.
#include<stdio.h>
main()
{
int b=20;
while(b>=1){
printf(“%d MUST\n”,b);
b--;
}
return 0;
}
The DO…WHILE Statement
 When you need to execute statements at least for once
irrespective of the result of the condition then you have to
use do...while loop.
 Unlike while loop, in which condition is checked at the top
of the loop; in do...while, condition is checked at the
bottom.
 Do…while executes statements first and then checks
condition. As the result statements are executed at least once
as condition is not at all checked before the first iteration.
 The basic form of do…while is
Syntax:
do
{
statements;
}
while(test_condition);
 Braces { } are must for do…while. And we can have any
number of statements between braces (body of the loop).
 Since the test_condition is evaluated at bottom of the loop,
the do…while construct provides an exit-controlled loop
and therefore the body of loop is always executed at least
once.
printf("\nThe sum of 10 natural numbers is: %d",sum);
return 0;
}

Output:
The sum of 10 natural numbers is: 55
Example 2: program which uses do while loop for printing numbers 1 to
10.
#include <stdio.h>
int main()
{ int i=1;
do
{
printf(“Number is: %d\n”,i);
i=i+1;
}
while( i<=10);
return 0; }
FOR Loop:
 This is another entry control loop.
 This integrates 3 basic ingredients of a loop (initialization,
condition and incrementing).
 For loop is typically used to repeat statements for a fixed number
of times.
 The basic form of for statement:
Syntax:
for(initialization;condition;updation)
{
statement;
}
Initialization Executed only for once just before loop
starts. Normally counter (variable used in
loop) is initialized here.
condition Is any valid C condition. As long as this
is true statement is repeatedly executed.
updation Executed after the statement is executed.
Typically contains incrementing counter or
decrementing counter as the case may be
statement (Body of the loop) This is repeatedly
executed as long as condition is true. It may
be a compound statement also.
Example 1: program which uses for loop for printing numbers 1 to 10.
#include <stdio.h> output :
int main() Number: 1
{ Number: 2
int i; Number: 3
for( i=1; i<= 10; i++) Number: 4
{ Number: 5
printf(“Number: %d\n”,i); Number: 6
} Number: 7
return 0; Number: 8
} Number: 8
Number: 10
Program for finding sum of series: 1+2+3+4+……………..+n
#include <stdio.h>
main()
{
int i,n,sum=0;
printf(“Give the Number of n:”);
scanf(“%d”,&n);
for( i=1; i<= n; i++)
{
sum = sum+i;
}
printf(“The sum of the series is: %d”,sum);
return 0;
}
If you run this program and give 5 as value of n then it will print:
The sum of the series is:15
END

You might also like