Lecture 2.1.1 Looping Statements
Lecture 2.1.1 Looping Statements
INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Introduction to Problem Solving
Code:22CSH-101
Introduction to Problem
Solving
Course Objectives
CO5 Design and develop modular programs for real world problems
using control structure and selection structure.
3
4
Scheme of Evaluation
Contents
5
Looping Statements
I/O Statements
6
Types of
I/OLooping
types statements
• Entry controlled loop or Pre-Test Loop: In the
entry controlled loop or Pre-Test loop, the
condition is checked before we start and at the
beginning of each iteration of the loop. If the
condition is true, we execute body of loop; if the
control expression is false, we terminate the
loop.
• Examples: while statement and for statement
C do while loops are very similar to the while loops, but it always executes the code
block at least once and furthermore as long as the condition remains true.
Syntax:
do
{
Do while statement(s);
}while( condition ); 8
9
While loop
Working:
step1: The loop variable is
initialized with some value and
then it has been tested for the
condition.
step2: If the condition returns true
then the statements inside the
body of while loop are executed
else control comes out of the loop.
step3: The value of loop variable
is incremented/decremented then
it has been tested again for the
loop condition.
1
0
Example 1
Example 1 Program to print first 10
natural numbers.
#include<stdio.h>
void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
x++;
}
}
1
1
Example 2
Infinite loop
1
2
Do-while loop
Working:
1. First we initialize our variables,
next it will enter into the Do While
loop.
2. It will execute the group of
statements inside the loop.
3. Next we have to use Increment
and Decrement Operator inside the
loop to increment or decrements the
value.
4. Now it will check for the condition.
If the condition is True, then the
statements inside the do while loop
will be executed again. It will
continue the process as long as the
condition is True.
5. If the condition is False then it will
exit from the loop.
1
3
Example
Program to print fibonacci series
#include<stdio.h>
void main()
{
int n,f,f1=-1,f2=1;
printf(" Enter The Number Of Terms:");
scanf("%d",&n);
printf(" The Fibonacci Series is:");
do
{
f=f1+f2;
f1=f2;
f2=f;
printf(" \n %d",f);
n--;
}while(n>=0);
}
1
4
For loop Flowchart of continue statement
Working:
Step 1: First initialization happens and
the counter variable gets initialized.
Step 2: In the second step the condition
is checked, where the counter variable
is tested for the given condition, if the
condition returns true then the C
statements inside the body of for loop
gets executed, if the condition returns
false then the for loop gets terminated
and the control comes out of the loop.
Step 3: After successful execution of
statements inside the body of loop, the
counter variable is incremented or
decremented, depending on the
operation (++ or –)
1
5
Example
Program to print factorial of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int fact, i, n;
fact = 1;
printf("Enter the number\t");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
{
fact = fact*i;
}
printf("Factorial of %d is %d", n , fact);
getch();
}
1
6
Summary
It continues to process a block of code until a
for statement becomes false, and everything is defined in
a single line.
It has one control condition, and executes as long the condition
while is true.
FAQ
• Q2 Write a program to print table of a number enter by user.
• #include <stdio.h>
• void main()
• {
• int j,n;
• printf("Input the number (Table to be calculated) : ");
• scanf("%d",&n);
• printf("\n");
• for(j=1;j<=10;j++)
• {
• printf("%d X %d = %d \n",n,j,n*j);
• }
• }
1
9
Assessment Questions
1. Program to check Armstrong number.
2. Write a c program to check a number is prime number or not.
3. Write programs for the following :
a) program to read 10 numbers from keyboard and find their sum and average.
b) to calculate the simple interest.
4. do-while loop terminates when conditional expression returns?
A One
B ZERO
C NON-ZERO
D NONE OF THE ABOVE
5. What will be the output of following program ?
#include <stdio.h>
void main()
{
int cnt=1;
do
{ printf("%d,",cnt);
cnt+=1;
}while(cnt>=10);
printf("\nAfter loop cnt=%d",cnt);
printf("\n")
2
0
Discussion
Forum
THANK YOU