0% found this document useful (0 votes)
16 views25 pages

Programming For Engineers Lecture 06

The document discusses different types of loops in C++ including while loops, do-while loops, and examples of each. It provides the syntax for while and do-while loops and explains the difference between them. While loops execute zero or more times based on the condition being true. Do-while loops execute at least once and then check the condition. Examples are given to demonstrate guessing games using while and do-while loops.

Uploaded by

Malik Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views25 pages

Programming For Engineers Lecture 06

The document discusses different types of loops in C++ including while loops, do-while loops, and examples of each. It provides the syntax for while and do-while loops and explains the difference between them. While loops execute zero or more times based on the condition being true. Do-while loops execute at least once and then check the condition. Examples are given to demonstrate guessing games using while and do-while loops.

Uploaded by

Malik Adnan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

What we have learned ?

Loops
Two types of loops
Counter loop

1
Train your mind……….!!!

2
while
while ( Logical Expression )
{
statements; :
}
int sum ;
sum = 0 ;
int sum = 0; ( Optional )
Example
int sum , number ;
sum = 0 ;
number = 1 ;
while ( number <= 1000 )
{
sum = sum + number ;
number = number + 1 ;
}
cout << “ The sum of the first 1000 integer starting from 1 is ” << sum ;
while (number <= UpperLimit)
Example
int sum, number , UpperLimit ;
sum = 0 ;
number = 1 ;
cout << “ Please enter the upper limit for which you want the sum ” ;
cin >> UpperLimi t;
while (number <= UpperLimit)
{
sum = sum + number ;
number = number +1 ;
}
cout << “ The sum of the first ” << UpperLimit << “ integer is ” << sum ;
if ( number % 2 == 0 )
{
sum = sum + number ;
number = number + 1 ;
}
Example
sum = 0;
number = 1;
cout << “ Please enter the upper limit for which you want the sum ”;
cin >> UpperLimit;
while (number <= UpperLimit)
{
if (number % 2 == 0)
{
sum = sum + number;
number = number + 1;
}
}
2 * ( number / 2 ) ;

?
int Junk ;
Junk = 1 ;
while ( Junk <= UpperLimit ) ( infinite loop ) X
{
sum = sum + number ;
number = number + 1 ;
}
Flow Chart for While Construct
WHILE Statement

Entry point for WHILE block

Condition is No
While Exit
true?

Process

Exit point for WHILE block


Factorial Definition

n! = n*(n-1)*(n-2)*(n-3)…………*3*2*1
Example: Factorial
#include <iostream.h>
main ( )
{
int number ;
int factorial ;
factorial = 1 ;
cout << “Enter the number of Factorial” ;
cin >> number ;
while ( number >= 1 )
{
factorial = factorial * number ;
number = number – 1 ;
}
Property of While Statement

It executes zero or more times


while loop
while (condition)
{
statements; :
}
statements;
While loop executes zero
or more times. What if we
want the loop to execute
at least one time?
do-while
Do while loop execute on
or more times
Syntax of do-while loop
do
{
statements ;

}
while ( condition ) ;
Example-Guessing game
char c ;
int tryNum = 1 ;
do
{
cout << "Please enter your guess by pressing a character key from a to z “ ;
cin >> c ;
if ( c == 'z‘ )
{
cout << "Congratulations! you guessed the right answer“ ;
tryNum = 6 ;
}
else
tryNum = tryNum + 1 ;
} while ( tryNum <= 5 ) ;
Flow chart for do-while loop

Do-while

Process

false
condition Exit

true
Relational Operators
char c ;
int tryNum , maxTries ;
tryNum = 1 ;
maxTries = 5 ;
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) )
{
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
tryNum = tryNum + 1 ;
}

You might also like