Lecture 8
Lecture 8
While Loop
Infinite Loop
Practice Problems
2
Repetition Structure
To add a list of numbers a variable for each number and one for the total
would need to be declared. For a different number of values, recoding
would be required. .
For that purpose, C++ provides flow control statements that serve to
specify what has to be done by our program, when, and under which
circumstances.
C++ has three repetition structures that repeat statements over and over
until certain conditions are met
3
Repetition Structure
We can see similar phenomenon in the practical life. For example, in the
payroll system, some procedures are same for all the employees.
4
Repetition Structure
5
Repetition Structure
Problem Statement
Print the sum of first 10 whole numbers.
Solution
Following statement may be the one way to do it
7
Repetition Structure
Solution of Problem
We have to calculate the sum of first 100 numbers.
What you need to store sum value?
We will take a variable sum of type int
Initialize the variable with some valid value.
What would be that valid value?
int sum=0;
For sum always initialize with zero
For product always initialize with one
Start with 1 and add it to sum (sum would become 0+1=1)
Get next integer by adding 1 to previous integer i.e. 2 and add it to
sum (sum would become 1+2=3)
Repeat previous step until you reach 100
8
Repetition Structure
Solution of Problem
Have you noticed any thing in the solution different?
?
We are adding 1 to previous integer and the new integer to sum and we are
repeating this procedure again and again.
This is what we call as REPETITION .
C++ provides many looping constructs
We will start with while loop structure
9
Repetition Structure
Explanations:
(Step 1) If logical Expression evaluates to true, the statement executes.
(Step 2) The logical Expression is reevaluated. The body of the loop
continues to execute until the logical expression is false.
11
Repetition Structure
12
Repetition Structure
main()
{
int sum, number;
sum = 0;
number = 1;
while(number
<= 100)
{
sum = sum + number;
number = number +
1;
}
int number=1;
We use both methods of initialization
14
Repetition Structure
number = number + 1;
increments the loop counter by 1 each time the loop's body is performed
The statement
15
Repetition Structure
16
Repetition Structure
Infinite Loop
A loop becomes infinite loop if a condition never becomes false.
Simply if the loop increment statement
number=number+1;
Changes to
number=number-1;
The loop will become infinite, because the loop condition will never be
false. It means the value of number will always remain less than the
upperlimit.
17
Repetition Structure
Problem Statements
Write a program for sum of numbers by prompting upper limit
18
While Loop Cases
19
Case 1- Counter-Controlled While loop
An infinite loop just never ends - loops and loops forever, because the
stopping condition is never met.
20
Case 1- Counter-Controlled While loop
Example:
int i, sum;
sum = 0;
i = 1;
while (i
<= 100)
{
sum = sum + i;
i=i+1;
}
cout << "The sum
of the first 100
numbers is " <<
sum << endl;
21
Case 2-Sentinel Controlled While Loop
22
Case 2-Sentinel Controlled While Loop
Example
int N, sum;
cout << "Enter the numbers to be added (for a sentinel enter 0):" <<
endl; sum = 0;
cin >> N;
while (N !=
0)
{
sum = sum + N;
cin >> N;
}
23
cout << "The
Case 2-Sentinel Controlled While Loop
24
int main() case 'C':
{ cout << "2" <<endl;
char letter; break;
cout << "Program to convert uppercase case 'D':
\n letters to their corresponding \n case 'E':
telephone digits. \n To stop the case 'F':
program enter #." cout << "3" << endl;
<< endl; break;
cout << "Enter a letter: "; case 'G':
cin >> letter; case 'H':
cout << endl; case 'I':
while (letter != '#') cout << "4" << endl;
{ break;
cout << "The letter you case 'J':
entered is: " case 'K':
<< letter << endl; case 'L':
cout << "The corresponding cout << "5" << endl;
telephone
\n digit is: ";
if (letter >= 'A' && letter <= 'Z') break;
switch (letter) case 'M':
{ case 'N':
case 'A': case 'O':
case 'B': cout << "6" << endl;
break;
case 'P': else
case 'Q': cout << "Invalid input." <<
case 'R': endl;
case 'S': cout << "\n Enter another
cout << "7" << endl; uppercase "
break; << "letter to find its "
case 'T': << "corresponding telephone
case 'U': digit."
case 'V': << endl;
cout << "8" << endl; cout << "To stop the program
break; enter #."
case 'W': << endl;
case 'X': cout << "Enter a letter: ";
case 'Y': cin >> letter;
case 'Z': cout << endl;
cout << "9" << endl; }
} Getch();
}
Case-3 Flag-controlled while loops
28
Number Guessing Game
If the program has reached the end of the input data or enters into the fail
state, the input stream variable returns false; in other cases, the input stream
variable returns true
The first item is read before the while statement and if the input stream
variable is true, the while statement is entered; additional items are read
within the while statement.
33
Case 4: EOF-controlled while loop
If the program has reached the end of the input data, the input
stream variable returns the logical value false
34
Case 4: EOF-controlled while loop
Example
int N, sum;
cout << "Enter the numbers to be added (to end enter CTRL+Z):" <<
endl; sum = 0;
cin >> N;
while (cin)
{
sum = sum + N;
cin >> N;
}
Problem Statement
Calculate the factorial of a given number
Solution
N(N-1)(N-2)………….3.2.1
36
Factorial Number Problem
37
Practice Problems
Problem-1
Write a program that determine whether given number is prime or not?
(without using break statement)
Problem-2
Write a program for Counting Digits and displaying their total count at the
end of the program.
8713105 - 7 digits
156 - 3 digits
- 1 digit
Problem-3
Write a program of Euclid’s for gcd. Read the properties first and then start
your coding.
Properties
1. gcd(a,a)=a
2. If a > b, then gcd(a,b) = gcd(a‐b,b)
38
Repetition Structure
Home Activities
Calculate the sum of odd integers for a given upper limit. Also draw flow
chart of the program.
Calculate the sum of even and odd integers separately for a given upper limit
using only one loop structure. Also draw flow chart of the program.
Find the Factorial for the number input by the user.
Flow Chart Must be practiced at home
39
40