What You'll Learn at This Station
HAP's Discovery: I thought that if I wanted something to happen multiple times, I should write it multiple times. That seemed logical! But I learned there is a better way—and that even the better way can go wrong if you do not trace through it carefully.
Copy-Paste Has Problems
Repeating Code Repeats Mistakes
When you copy the same logic over and over, any mistake in that logic also gets copied. And if you need to change something, you have to change it everywhere.
Loops Are a Better Way
Say It Once, Repeat It Many
A loop lets you write the logic once and have it repeat until something changes. But you have to make sure something actually can change!
Reading Is Not Tracing
Walk Through Every Step
When you read logic, your brain fills in gaps. When you trace, you follow exactly what happens—step by step, no assumptions.
HAP's Confession:
- My first instinct was to copy and paste. It felt like the obvious solution—if I want something three times, I write it three times.
- I did not think about what happens when the player wins early. My copy-paste version kept asking even after they got it right!
- When I switched to a loop, I forgot something important. I felt confident until Prof. Teeters asked me to trace through it step by step. 😳
My First Idea: Copy and Paste
I wanted to give the player three chances to guess. My first thought was simple: write the guess-and-check part three times.
BEGIN
DISPLAY "Welcome to the Secret Number Game!"
DISPLAY "I am thinking of a number."
DISPLAY "Can you guess what it is?"
INPUT guess
IF guess is not correct THEN
DISPLAY "Wrong guess. Try again!"
ENDIF
INPUT guess
IF guess is not correct THEN
DISPLAY "Wrong guess. Try again!"
ENDIF
INPUT guess
IF guess is not correct THEN
DISPLAY "Wrong guess. Try again!"
ENDIF
DISPLAY "Thanks for playing!"
END I showed this to Grace, feeling pretty clever. She looked at it carefully, then asked me a question.
Grace's Question:
"What happens if the player guesses correctly on the first try?"
I traced through it in my head. If they guess right on the first try... the IF condition is false, so it skips the "Wrong guess" message. But then... it still asks for another guess! And another! The player already won, but the game keeps going!
What Is Wrong with Copy-Paste?
Grace helped me see several problems with my approach.
It Does Not Stop When You Win
Even if the player guesses correctly, the game keeps asking for more guesses. There is no way to exit early.
It Does Not Scale
What if I want ten guesses? Or unlimited guesses until they get it right? I would have to keep copying and pasting forever.
Mistakes Get Copied Too
If I made an error in one copy, I probably made it in all of them. And if I need to fix something, I have to fix it in every copy.
It Is Hard to Read
All that repeated code makes it hard to see what the logic actually does. The important parts get buried in repetition.
Grace's Observation:
"When you copy the same logic over and over, any mistake in that logic also gets copied over and over. And if you need to change something, you have to change it everywhere. There is a better way."
A Better Way: Loops
Grace introduced me to something called a WHILE loop. The idea is simple: instead of writing the same thing multiple times, you write it once and tell the computer to keep doing it until something changes.
How a WHILE loop works:
- WHILE some condition is true DO
- (do these steps)
- ENDWHILE
The computer keeps repeating the steps inside the loop as long as the condition is true. When the condition becomes false, it stops and moves on.
This seemed perfect! I could say "WHILE the guess is not correct, keep asking." When they finally get it right, the condition becomes false and the loop stops.
I rewrote my pseudocode using a loop.
My Loop Attempt
Here is what I wrote. Read it through—does it look right to you?
BEGIN
DISPLAY "Welcome to the Secret Number Game!"
DISPLAY "I am thinking of a number."
DISPLAY "Can you guess what it is?"
INPUT guess
WHILE guess is not correct DO
DISPLAY "Wrong guess. Try again!"
ENDWHILE
DISPLAY "Thanks for playing!"
END I was pleased with myself. Much cleaner than the copy-paste version! I showed it to Prof. Teeters.
She looked at it, then said something that surprised me: "Before you trust this logic, trace through it. Slowly. Every time."
I thought it was fine. But she asked me to actually walk through what happens, step by step, if the player guesses wrong.
Tracing the Logic
I pretended to be the computer and followed each step exactly. The player guesses "5" but the secret number is "7".
Step 1: DISPLAY "Welcome to the Secret Number Game!"
Step 2: DISPLAY "I am thinking of a number."
Step 3: DISPLAY "Can you guess what it is?"
Step 4: INPUT guess
→ Player types: 5
→ guess = 5
Step 5: WHILE guess is not correct DO
→ Is 5 not correct? YES (secret is 7)
→ Enter the loop
Step 6: DISPLAY "Wrong guess. Try again!"
Step 7: Go back to WHILE
Step 8: WHILE guess is not correct DO
→ Is 5 not correct? YES (guess is STILL 5!)
→ Enter the loop again
Step 9: DISPLAY "Wrong guess. Try again!"
Step 10: Go back to WHILE...
... this never stops!
🟠 The Moment I Understood:
"Wait," I said. "The guess never changes! It is still 5 every time we check. The player never gets to type a new number!"
Prof. Teeters nodded. "The loop keeps running, but nothing inside it changes the thing you are checking. So it can never stop."
I had created a trap. The player would be stuck seeing "Wrong guess. Try again!" forever, with no way to actually try again.
Seeing the Problem
Prof. Teeters showed me my logic as a flowchart. Look at what is inside the loop:
What did I notice?
The loop goes around and around, but there is only one box inside it: the "Wrong guess" message. There is no INPUT box inside the loop. The player never gets to enter a new guess!
The arrow goes back to check the condition, but the guess is still the same. So the condition is still true. So it loops again. And again. Forever.
Correcting the Logic
Once I understood the problem, the fix was clear: I need to ask for a new guess inside the loop.
BEGIN
DISPLAY "Welcome to the Secret Number Game!"
DISPLAY "I am thinking of a number."
DISPLAY "Can you guess what it is?"
INPUT guess
WHILE guess is not correct DO
DISPLAY "Wrong guess. Try again!"
INPUT guess
ENDWHILE
DISPLAY "You got it!"
DISPLAY "Thanks for playing!"
END The Key Change
I added INPUT guess inside the loop, right after the "Wrong guess" message. Now the player can enter a new guess each time.
Why It Works
Each time the loop runs, the guess gets a new value. Eventually, the player will enter the correct number, and the loop will stop.
Added Success Message
I also added "You got it!" so the player knows they won. The game should celebrate their success!
Same Structure, One Line Different
The fix was just one line. But without tracing, I never would have found the problem.
The Corrected Flowchart
Now look at the corrected version. See the difference?
What changed?
Now there are two boxes inside the loop: the "Wrong guess" message AND the INPUT box. After telling the player they were wrong, the game asks for a new guess. Then it loops back to check if the new guess is correct.
The arrow now goes: check → wrong message → get new guess → check again. Eventually the guess will be correct, the condition will be false, and the loop will exit.
🟠 What I Learned:
Looking at the flowcharts side by side made the problem obvious. In the broken version, the loop had nothing that could change the outcome. In the fixed version, the INPUT inside the loop gives the player a way to eventually succeed.
Prof. Teeters reminded me: "A loop that repeats is only useful if something inside it can change. Otherwise, it just does the same thing forever."
What I Learned
This experience taught me something important about logic and about myself as a learner.
Copy-Paste Is Not the Answer
My first instinct—repeating code—created problems. Loops are a better way to handle repetition.
Loops Need Something to Change
A loop checks a condition over and over. Something inside the loop must be able to make that condition eventually become false.
Reading Is Not the Same as Tracing
When I read my logic, I saw what I intended. When I traced step by step, I saw what actually happens.
Flowcharts Show What Is Missing
The visual made the problem obvious. I could see that the loop had nothing inside it that changed the guess.
🎓 Reasoning About Logic Quick Reference
Do Not Just Copy and Paste
If you want something to repeat, a loop is usually better than copying the same code multiple times. Loops are easier to change and harder to mess up.
Loops Need an Exit
A loop keeps running while its condition is true. Make sure something inside the loop can eventually make the condition false.
Trace Before You Trust
Do not assume logic works because you can read it. Walk through each step exactly as the computer would—no skipping, no assuming.
Use Flowcharts to See the Problem
When something is wrong, drawing the logic can help you see what is missing. The visual makes gaps and problems obvious.