0% found this document useful (0 votes)
6 views

Lecture 6 1

Uploaded by

muttitariq501
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture 6 1

Uploaded by

muttitariq501
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Outline

 Repetition Structure (Loops)

 Analyzing Repetition Structure Problem

 While Loop

 Counter Control Loop

 Infinite Loop

 While Loop Cases

 Practice Problems
Repetition Structure

 In our day to day life, most of the things are repeated.


 Days and nights repeat themselves 30 times a month.
 Four seasons replace each other every year.

 We can see similar phenomenon in the practical life. e.g., in the payroll system,
few procedures such as salary generation method for regular employees
are same.
 Such procedures are repeatedly applied while dealing with the employees.
 So repetition is very useful structure in the programming.

What are Loops?


 A Repetition Structure (loop) statement allows us to execute a statement or
a group of statements multiple times.

 C++ has three repetition structures that repeat a statement a certain


number of times, or while a condition is fulfilled.
Repetition Structure

Problem Statement
 Print the sum of first 10 whole numbers.
Solution
 Following statement may be the one way to do it
cout<<“Sum of First 10 Whole
Number
is:”<<1+2+3+4+5+6+7+8+9+10;
- And you can apply the same method for calculating sum of 20, 30,
40, 50 or even 100 whole numbers.

What if we calculate sum of first 1000 numbers or


more?
Repetition Structure – Analyze the Problem

- To calculate sum of first 1000 numbers.


- We will start from 1 and end at 1000. So, our first integer is 1
- How shall we find the next integer?
- Simply add 1 to the integer and find the next which is 2
- To Find the next integer simply add 1 to the previous
integer (2) and find the next which is 3.
- So, whenever we want to find the next integer, just add 1 to the
previous integer.
Repetition Structure – Problem Solution

- Take a variable sum of type int


- Initialize the variable s u m w i t h 0 .
- int sum=0;
- Note: For sum always initialize with zero
- Note: For product always initialize with one
- Take another variable num with initial value 1 and add it to sum (sum
would become 0+1=1)
- Get next value by adding 1 to num; thus, it’s value become 2,
afterwards, add num to sum (sum would become 1+2=3)
- Repeat previous step until you reach 1000.
- So, we use while loop structure.
Repetition Structure – while Loop Structure

 A while loop repeatedly executes target statements as long as the


given condition is true.
Sysntax of while Loop
is
while(condition)
{
statement(s);
}
 Here, statement(s) may be a single statement or a block of statements
(compound statement), known as body of the loop.
 The condition may be any expression, and true is any non-zero
value. The loop iterates while the condition is true.
 When the condition becomes false, program control passes to
the line immediately following the loop.
Note: while is the reserve word
Repetition Structure
Repetition Structure – while Loop
Counter - Controlled Condition
using namespace std;
#include<iostream>
main()
{
int sum, number;
sum = 0;
number = 1;
while(number <= 1000)
{
sum = sum + number;
number = number +
1;
} "The sum of first 1000 integers starting from 1 is " <<
cout <<
} sum;
In counter controlled while loop
 The exact number of pieces of data is known – e.g., it is 1000
 The counter is initialized to 0, then is evaluated to be <= 1000, then
incremented in the body of the loop.
Repetition Structure

Counter Control Repetition


 The body of this while executes even when the control variable is 1000. The
loop terminates when the control variable is greater than 1000 (i.e., when
number becomes 1001).
 The while condition can be made more concise by initializing
number to 0 and by replacing the while statement with
while ( ++number <= 1000 )
sum=sum + number;

 By using ++ in condition we will no longer required to


use statement number=number+1 to increment value
of number.
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.
Repetition Structure

Problem Statements
 Write a program for sum of numbers by prompting(input from user) upper limit.

 Write a program for sum of even number by prompting upper limit.

 Write a program for product of odd number by prompting user lower and
upper limit.
Repetition Structure
while Loop Cases

Case 1: Counter-controlled Condition – As discussed


earlier
Case 2: Sentinel Controlled Condition

Case 3: Flag-controlled Condition

Case 4: EOF-controlled Condition


Case 2 - Sentinel Controlled Condition

 The exact number of pieces of data is not know, but the last entry is a
special value, called a sentinel.

 The first item is read before the while loop

if it does not equal the sentinel, the body of the while loop

Executes.

 Each additional item is read within the while loop.


Case 2 - Controlled Condition
Sentinel
Example
int n, sum;
cout << "Enter the to be added (for a sentinel enter 0):" <<
numbers endl;

sum = 0;
cin >> n;
while (n != 0)
{
sum = sum + n;
cin >> n;
}
cout << "The sum of the entered numbers is " << sum <<
endl;
Case 2-Sentinel Controlled Condition

Telephone Digit Example


Write a program reads the letter codes A to Z and prints the
corresponding
telephone digit. Program will terminate when user press # button.
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 entered is: " case 'J':
<< letter << endl; case 'K':
cout << "The corresponding telephone case 'L':
\n digit is: "; cout << "5" << endl;
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': <<
case 'S': endl;
cout << "7" << endl; cout << "\n Enter another
break; uppercase "
case 'T': << "letter to find its "
case 'U': << "corresponding telephone
case 'V': digit."
cout << "8" << endl; << 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 Condition

 Uses a boolean variable to control the loop

 the boolean variable is set to either true or false

 While the expression evaluates to true, the loop statement continues

 The Boolean variable must be set/reset within the while statement.


Case 4: EOF-controlled Condition

 The while statement executes until there are no more data items.


If the program has reached the end of the input data 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.

 Note. In the DOS environment, the end-of-file marker is entered using ctrl+z.
Case 4: EOF-controlled Condition

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;
}
cout << "The sum of the entered numbers is " << sum <<
endl;
Factorial Number Problem

Problem Statement
Calculate the factorial of a given
number
Solution

The factorial of a number N is defined as:

N(N-1)(N-2)………….3.2.1
Factorial Number Problem
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
 3 - 1 digit
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 a given upper limit
separately
using forloop structure. Also draw flow chart of the program.
only one

 Find the Factorial for the number input by the user.


 Flow Chart Must be practiced at home

You might also like