Professional Documents
Culture Documents
Unit 3 - CH 1.decision Making and Looping
Unit 3 - CH 1.decision Making and Looping
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)
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
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