Chapter 5 PF Lecture Slides
Chapter 5 PF Lecture Slides
• The statements within the braces do not form the body of the while
loop.
Designing while Loops
• A while loop executes as long as its condition evaluates to true.
• The loop control variable (LCV) is a variable that determines when
the loop should stop.
• Steps to properly design a while loop:
o Initialize the LCV before the loop starts.
o Write a condition in the while statement that checks the LCV.
o Update the LCV inside the loop so the condition eventually
becomes false, preventing an infinite loop.
Designing while Loops
Case 1: Counter-Controlled while Loops
• A counter-controlled while loop runs a fixed number of times.
• The loop control variable (LCV) acts as a counter to track iterations.
• It is used when you know exactly how many times a loop should
execute.
Case 1: Counter-Controlled while Loops
Case 1: Counter-Controlled while Loops
Case 1: Counter-Controlled while Loops
Case 2: Sentinel-Controlled while Loops
• Used when the number of inputs is unknown.
• A sentinel value signals when to stop taking input.
• The first input is taken before the loop starts.
• The loop runs until the sentinel value is encountered.
Case 2: Sentinel-Controlled while Loops
Case 2: Sentinel-Controlled while Loops
Case 2: Sentinel-Controlled while Loops
Case 2: Sentinel-Controlled while Loops
Telephone Digit Example
Case 2: Sentinel-Controlled while Loops
Telephone Digit Example
Case 2: Sentinel-Controlled while Loops
Telephone Digit Example
Case 3: Flag-Controlled while Loops
• Uses a bool flag variable to control the loop.
• The loop continues until the flag variable is set to true.
• The flag is updated inside the loop based on a condition.
• The flag name describes the condition (e.g., isFound, isFull).
Case 3: Flag-Controlled while Loops
Number Guessing Game Example
• Random Number Generation: The program generates a random
number between 0 and 99 using rand() and srand(time(0)) to
ensure different outputs each time.
• User Interaction & Looping: The program continuously asks the user
to guess the number using a flag-controlled while loop until they
guess correctly.
• Conditional Checks & Feedback: Based on the user's guess, the
program provides feedback if the guess is too high or too low and
prompts them to try again until they get the correct number.
Case 3: Flag-Controlled while Loops
Number Guessing Game Example
Case 3: Flag-Controlled while Loops
Number Guessing Game Example
Case 4: EOF-Controlled while Loops
• EOF-Controlled While Loops: Used when reading data from a file
without relying on a sentinel value. The loop continues reading until
it reaches the end of the file.
• Input Stream Behavior: The cin or file input stream returns false
when it reaches the end of the file or encounters an invalid input,
which helps control the loop.
Case 4: EOF-Controlled while Loops
• eof() Function: Used to explicitly check if a file stream has reached the
end of the file, commonly used when processing text files.
• Example Applications:
o Summing Numbers: Reads a set of numbers and sums them up until
EOF is reached.
o Processing Student Grades: Reads student names and test scores,
assigns grades, and calculates the class average using an EOF-
controlled loop.
• File Handling & Error Checking: Ensures files are opened correctly,
processes data line by line, and closes files properly to prevent data loss
or corruption.
Case 4: EOF-Controlled while Loops
Case 4: EOF-Controlled while Loops
Case 4: EOF-Controlled while Loops
Case 4: EOF-Controlled while Loops
More on Expressions in while Statements
• Unlike simple loops controlled by a single variable, some while
loops use multiple conditions.
• Logical operators (&&, ||, !) can be used to combine multiple
conditions.
More on Expressions in while Statements
Two Content Layout with Table
• First bullet point here Class Group 1 Group 2
• Second bullet point here Class 1 82 95
• Third bullet point here Class 2 76 88
Class 3 84 90
For Looping (Repetition) Structure
• for loop is a specialized form of while loop.
• Used for counter-controlled repetition.
• The syntax of a while loop:
For Looping (Repetition) Structure
• Execution:
• Run init.
• Check condition.
• If true: run body, then update.
• Repeat until condition is false.
• for is a reserved keyword in C++.
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
For Looping (Repetition) Structure
do while Looping (Repetition) Structure
• do...while is the third type of loop in C++.
• Syntax:
do while Looping (Repetition) Structure
• Loop body runs at least once.
• Executes the statement first, then checks the expression.
• If expression is true, repeats the loop.
• Use braces {} for compound statements.
• Avoid infinite loops by ensuring expression becomes false.
• do is a reserved keyword in C++.
Do while Looping (Repetition) Structure
Do while Looping (Repetition) Structure
do while Looping (Repetition) Structure
• Divisibility Test by 3 and 9:
• If m divides n, then n = m × t for some integer t.
• An integer is divisible by 3 or 9 if the sum of its digits is divisible by
3 or 9.
• Example:
• n = 27193257, digit sum s = 2+7+1+9+3+2+5+7 = 36
• Since 36 is divisible by both 3 and 9 → 27193257 is divisible by
both.
do while Looping (Repetition) Structure
• To extract digits:
• num % 10 → gives last digit.
• num / 10 → removes last digit.
• Use a do...while loop to find digit sum: