06-Unit5-CS 1 – PROGRAMMING LOGIC FORMULATION
06-Unit5-CS 1 – PROGRAMMING LOGIC FORMULATION
Structures
Why Repetition is Essential in Programming:
Efficiency: Explain that repetitive tasks are
common in programming, and without a way to
automate them, writing code would be tedious
and time-consuming.
Consistency: Emphasize that repetition helps
maintain consistency in code. When a task needs
to be performed multiple times, a loop ensures
that the same set of instructions is executed
consistently.
Why Repetition is Essential in Programming:
Reduced Redundancy: Discuss how repetitive code can
lead to redundancy, making programs longer and
harder to maintain. Iterative control structures provide
a cleaner and more efficient way to handle repetition.
Adaptability: Point out that in many situations, the
number of times a task needs to be repeated may not
be known beforehand. Iterative control structures allow
for adaptability, accommodating scenarios where
repetition depends on changing conditions.
Introduction to Iterative Control
Structures:
These structures provide the tools needed to
implement repetition in a program.
While Loop:
Briefly introduce the while loop as a structure that
repeats a set of statements while a condition is true.
Relate it to real-life scenarios, such as "While your phone
battery is below 20%, charge it."
Introduction to Iterative Control
Structures:
Do-While Loop:
Discuss the do-while loop, highlighting that it is
similar to the while loop but checks the condition
after executing the loop.
Use an analogy like "Do your homework until you
finish, then check if you need to review."
Introduction to Iterative Control
Structures:
Repeat-Until Loop:
Introduce the repeat-until loop, emphasizing that it
repeats a set of statements until a specified
condition becomes true.
Draw parallels with activities like "Repeat jumping
jacks until you reach 50, then check if you're tired."
EXAMPLES OF REPETITION
CONTROL STRUCTURE IN
PSEUDOCODE
Example while loop
Problem Statement:
Create a program that prints
the numbers from 1 to 10.
Example while loop
Explanation:
1.Initialization:
Start •A variable counter is declared to keep track
of the current number.
Declare Integer counter •counter is initialized to 1.
2.While Loop:
Set counter = 1 •The while loop continues as long as
While counter <= 10 counter is less than or equal to 10.
•Inside the loop:
Output counter •The current value of counter is printed
to the console.
Set counter = counter + 1 •counter is incremented by 1 to move to
End While the next number.
3.Termination:
End •Once counter exceeds 10, the loop ends,
and the program terminates.
Example do-while loop
• Problem Statement:
Write a program that the user should
generate a random number between 1 to 100
and it will guess if the generated random
number from the user is correct.
Example do-while loop
1. Start Execution Steps:
2. Generate a random number between 1 and • The program starts.
100 • A random number between 1 and 100 is generated.
3. Initialize variables: guess, randomNumber • Variables guess and randomNumber are initialized.
4. do • The do-while loop begins.
5. Input guess from the user • The user is prompted to input their guess.
• If the guess is correct (equals randomNumber), the
6. if (guess equals randomNumber) program outputs a congratulatory message and
7. Output "Congratulations! You guessed the exits the loop.
correct number." • If the guess is too low, the program outputs a
8. else if (guess is less than randomNumber) message and continues to the next iteration of the
loop.
9. Output "Too low. Try again." • If the guess is too high, the program outputs a
10. else message and continues to the next iteration of the
11. Output "Too high. Try again." loop.
• The loop continues until the guess is correct.
12. while (guess not equals randomNumber)
• The program ends.
13. Stop
Example repeat-until
• Problem Statement:
Write a program that prompts the user to
enter a password. The user has to keep
entering the password until it matches a
predefined correct password. Use a repeat-
until loop for password verification.
Example repeat-until
Execution Steps:
1. Start • The program starts.
2. Set correctPassword to "Secret123" • The correct password is set to "Secret123".
3. Initialize userPassword to an empty • The variable userPassword is initialized to
string an empty string.
4. repeat • The repeat-until loop begins.
5. Input userPassword from the user • Inside the loop:The user is prompted to
input a password (userPassword).
6. until (userPassword is equal to • If the input password matches the correct
correctPassword) password, the loop exits; otherwise, it
7. Output "Access granted. Welcome repeats.
to the system." • Once the loop exits, the program outputs
8. Stop "Access granted. Welcome to the system.“
• The program stops.
Structure of Loops
While Loop
- Define the while loop as a control structure that repeats a set
of statements while a condition is true.
- Use a simple real-life analogy to explain the concept: "While it's
raining, keep your umbrella open.“