Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Decision making &

Looping
C Programming  Unit 3 Chapter 1
Introduction..
 Looping requires repeated execution of a set of statements until some condition is
satisfied.
 Two types of loops based on where the condition is tested.
 Entry controlled loops(pre-test)
• Condition is checked at the beginning of the looping block.
• Looping block executed 0 or more times.
 Exit controlled loops(post-test)
• Condition is checked at the end of the looping block.
• The looping body is executed unconditionally for the first time.

2
Loop Control Structure

3
Introduction Contd..
Based on nature of control variable and the kind of value assigned ,loops are classified into 2 categories:
1. Counter – controlled loops(definite repetitions loop)
2. Sentinel –controlled loops (indefinite repetitions loop)

Counter – controlled loops:


 Exactly know how many times the loop is executed.
 Counter is used as control variable it must be initialized, tested and updated.
 Definite repetition loop

Sentinel – controlled loops:


 A special value called a Sentinel value is used for control variable.
 This value will change the loop control expression from true to false.
 Indefinite repetition loop
4
Example of Counter-controlled loops
• Counter controlled loops are those loops where we know the number of the executions in
advance. The control variable is known as counter. In this loop, we set the value of the
counter and max limit.
• int sum = 0; int n = 1;
while (n <= 10)
{
sum = sum + n*n;
n = n+ 1;
}
• In the above example, the loop executes exactly 10 times for n = 1,2,3,……,10. The initial
value of the counter n is 1, and it increments by 1 in each loop. When it reaches 11, the
loop breaks as the condition breaks. 5
Example of sentinel-controlled loops
• Sentinel controlled loop are those loop where where we do not know the number of
execution in advance. In this case, the value of the control variable differs within a
limitation and the execution can be terminated at any moment. The control variable in
this case is termed by sentinel variable. The value of this variable changes inside the
loop. The loop breaks when the value of the control variable matches the condition.
• do
{
printf("Input a number.n");
scanf("%d", &num);
}while(num > 0);
• In the above example, the loop executes till the entered value of the variable num is not
0 or less then 0. This is a sentinel controlled loop and here the variable num is a sentinel.
6
Introduction Contd..
C provides 3 constructs for performing loop operations :
 while statement
 do-while statement
 for statement

7
while statement
 Entry controlled loop-statement.
 General Syntax:
while(test condition)
{
body of the loop
}
 As long as test condition is satisfied body of the loop will be executed.
 If test condition failed, execution proceeds to statement after while loop.

8
Flowchart of while Loop

9
Example
#include<stdio.h>
int main()
{
int i=1; //initialized I value
while(i<=5) //condition
{
printf(“ABC”);
i++; // increment
}
return 0;
} 10
While statement Contd..
Prg 1:To print numbers from 1 to 5 :
Program :
Algorithm : Flow Chart:
#include<stdio.h>
Step 1 : start int main()
Step 2 : initialize value of i =1 {
Step 3 : repeat as long as i<=5 int i=1;
i.Print i value. while(i<=5)
{
ii. i=i+1 printf(“%d”,i);
Step 4 : stop i=i+1;
}
return 0;
}

11
while statement
Prg 2 : Sum of squares first 10 numbers using while loop:
#include<stdio.h>
void main()
Output :
{ Sum of Squares of first 10 nos : 385
int n=1, sum=0;
while(n<=10)
{
sum = sum+n*n;
n++;
}
printf("Sum of squares of first 10 nos:%d",sum);
} 12
do while statement
 An exit controlled loop
General Syntax:
do
{
// body of the loop
}
while(test-condition);
 Body of the loop executed at least once.

13
Flowchart of do….while loop

14
Example
#include<stdio.h>
int main()
{
int i=1;
do
{
printf(“ABC”);
i++;
}while(i<=5);
return 0;
15
}
do while statement Contd..
Program to add numbers until the user enters zero.
void main()
{
int n,sum=0;
do
{
printf("Enter a number:");
scanf("%d",&n);
sum = sum+n;
}
while(n!=0);
printf("sum = %d",sum);
16
Nested loops
• Loops can be nested, similar to nested if else.
• Structure of two nested while loops
while(test-condition-1)
{
// some code here.
while(test-condition-2){
}
// some code here.
}

17
Nested loops Contd..
Sum of squares of numbers, repeated until user enters zero.
main(){
printf("Enter n:");
scanf("%d", &n);
while(n!=0)
{
sum = i = 0;
while(i<=n)
{
sum = sum+i*i;
i++;
}
printf("Sum of squares of 0 to %d:%d\n",n,sum);
printf("Enter another n:");
scanf("%d", &n);
} // End of outer while
18
for loop
 Entry-controlled loop.
 General syntax:
for( initialization; test-condition; increment)
{
// Body of the loop
}
• Initialization : The control variable initialized.
• Test-condition : control variable is tested to check if loop execution to be continued or
terminated.
• Increment: control variable is incremented/decremented, taking a step towards termination
condition.
19
Flowchart of for loop

20
Example
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf(“Abc”);
}
return 0;
}
21
Example of for loop :squares of the first 10
integers

for(i = 0; i <= 10; i++)


printf("%d ", i*i);

22
Another way of representing for loop
flowchart

23
24
SLE-Jumps in loops
1: keyword ‘break’
• To come out of the loop unconditionally.
• Rest of the loop body won’t executed.
• Execution continues after loop body.
• Used for, while, do-while loops and switch.
while(i<20)
{
….
if(i == 10) break;
i++;
25
}
Jumps in loops 1: keyword ‘break’
• E.g
while(i<20)
{
….
if(i == 10) break;
…..
i++;
}
// remaining statements
26
‘break’ examples
void main()
{
int i, sum=0;

i=1;
while(1)
{
printf("Hello %d\n",i);
if(i == 20)
break;
i++;
}
printf("Program terminating..");
27
‘break’ examples
void main()
{
int i;
for(i=1;i<=10000 ;i++)
{
printf("Hello %d\n",i);
if(i == 20)
break;
}
printf("Program terminating..");
28
}
Jumps in loops 2: keyword ‘continue’
• To skip execution of remaining statements of an iteration(not come
out of loop)
• Loop will continue to execute from next iteration.
• Used for, while, do-while loops.

29
Jumps in loops 1: keyword ‘continue’
• E.g
while(i<20)
{
….
if(i == 10) continue;
…..
i++;
}
// remaining statements
30

You might also like