Counter and Event Controlled
Loop
Counter Controlled Loop
• definite repetition loop
• number of iterations is known before the loop begins to
execute
• Components:
1. a control variable.
2. the increment (or decrement) value by which the control
variable is modified at each iteration of the loop.
3. the loop terminating condition that checks if looping
should continue.
Example: Printing the first 10 natural numbers.
Example:
Event-Controlled loop
• loop an action is repeated until a certain event occurs
• Types:
1. Sentinel-controlled loop
2. End-of-file controlled loop
3. Flag controlled loop
Sentinel Controlled Loop
• indefinite repetition loop
• number of iterations is not known before the loop starts
executing
• sentinel value is used to change the loop control
expression from true to false
Example: Processing of data from a text file of unknown
size.
Example
End-of-file controlled loop
• used when reading from a file
• determined by checking the EOF() function after each
read from the file
The EOF() function will return true when the end-of-file is
reached.
Example:
Flag controlled loop
• bool variable is defined and initialized to serve as a flag
• loop continues until the flag variable value flips (true
becomes false or false becomes true)
Example: