Exercise · Q4
Q.Give an example of a loop that is to be executed a certain number of times.
Puducherry TnboardTextbookSubjective· 2mImportance★★★★★est
58% · 21/36 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A counter-controlled loop uses a counter variable tested against a limit and incremented every pass — it runs an exact, known number of times.
The idea. When the number of repetitions is known in advance, we use a counter-controlled (definite) loop: initialise a counter, test it against the limit, do the body, increment. The three parts — initialisation, condition, update — guarantee the loop executes exactly the intended number of times and then stops (miss the update and you get an infinite loop).
Example — print "Hello" exactly 5 times
PRINT_HELLO_5_TIMES
SET count = 1
WHILE count <= 5
PRINT "Hello"
count = count + 1
END WHILE
END
Python implementation
for count in range(1, 6): # runs for count = 1,2,3,4,5
print("Hello")
Expected output
Hello
Hello
Hello
Hello
Hello
Dry run
| Pass | count at test | count <= 5? | Action |
|---|---|---|---|
| 1 | 1 | yes | print Hello, count → 2 |
| 2 | 2 | yes | print Hello, count → 3 |
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.