INFO 0102 Introduction Problem Solving & Programming Study Notes

INFO 0102

Introduction Problem Solving & Programming

Repetition Control Structures


Page 1

Title: INFO 0102 Introduction Problem Solving & Programming

Key Topic: Repetition Control Structures


Page 2

Agenda

  1. Recap
  2. Format of Component D
  3. Reminders
  4. Types of Repetition Control Structures
    • Using the WHILE…DO control structure
    • Using the REPEAT…UNTIL control structure
    • Using FOR Control Structure

Page 3

Recap

Simple Selection

  • Simple selection occurs when a choice is made between two alternative paths, depending on whether a condition is true or false.
  • The structure is represented in pseudocode using the keywords:
    • IF
    • THEN
    • ELSE
    • ENDIF

Control Structures

  • Simple IF control structures are used in programming to control the execution flow of a program. They determine the order in which instructions are executed, allowing for decision making, repeated actions, and logical sequencing.
  • The null ELSE structure is a variation of the simple IF structure, performing a task only when a specific condition is satisfied.

Variants of IF Structures

  • Simple IF with Null Branch:
    • Used when a task is executed only if a condition is true; otherwise, it is skipped.
  • Non-linear Nested IF Statements:
    • Occur when multiple conditions must be satisfied before an action takes place.
    • Named non-linear due to separation of ELSE from IF statement. Indentation must align each ELSE and ENDIF with corresponding IF.
  • IF…ELSE Selection Statement: A structure indicating alternate paths based on conditions.
  • Linear Nested IF Statements:
    • Tests a field for various values with corresponding actions and ensures each ELSE immediately corresponds to its IF.

Structure and Indentation

  • Proper indentation enhances readability in pseudocode, aligning each IF, ELSE, and ENDIF appropriately.

Page 4

The CASE OF Control Structure

  • A case or switch statement allows the value of a variable to determine control flow via a multiway branch.
  • Pseudo code demonstration may involve a syntax seen in 'try with switch' mechanics.

Page 5

Component D (10%)

  • An in-class assignment focusing on algorithms and pseudocode.
  • Date: March 20th, 2026
  • Platform: Online
  • Format includes Multiple Choice Questions, True/False Questions, and Logic Questions
  • Reminder: No class on Friday, February 13th, 2026. Resuming on Friday, February 20th, 2026.

Page 6

Reminders

  • Engagement based on participation in class and on the online platform Canvas.
  • Assignment 1 Due: February 8th
  • Assignment 2 Posting Date: February 20th
  • No class on February 13th, 2026, with resumption on February 20th, 2026.

Page 7

Types of Repetition Control Structures

  • Repetition control structures (loops): execute a set of instructions repeatedly based on a condition.
  • They automate repetitive tasks efficiently without needing multiple code lines.
  • Essential for data processing, counting values, or repeating actions until a condition is met.
  • Three main types are:
    1. WHILE…DO
    2. REPEAT…UNTIL
    3. FOR loops

Page 8

Repetition Control Structures Overview

  • The best approach involves a looping structure in algorithms to repeat a sequence of instructions.
  • This is determined by where the decision to repeat is located:
    • Leading Decision Loop: where the condition is assessed at the start of the loop.
    • Trailing Decision Loop: where the condition is assessed at the end of the loop.
    • Counted Loop: designed to execute a set number of times.

Page 9

WHILE…DO Control Structure

  • Description:
    It's a repetition structure executing a block of code as long as a specified condition is true.
  • Condition Testing: The condition is checked before the loop begins.
    • If true, execute statements in the loop.
    • If false, skip the loop entirely.
  • This may lead to zero or more executions due to initial condition check.
  • Usage: Ideal when repetitions are not predetermined, based on a condition's fulfillment (e.g., user input).
  • Importance of Variable Update: Properly updating loop variables is crucial to avoid infinite loops where conditions remain perpetually true.

Page 10

Example: WHILE…DO

Example_WD

```plaintext
START
// Declare and initialise the counter variable
SET count = 1
// WHILE condition is tested BEFORE the loop runs
WHILE count <= 5 DO
// Display the current value of count
PRINT count
// Update the counter to avoid an infinite loop
SET count = count + 1
ENDWHILE
END

---

# Page 11  
## Questions on WHILE…DO  
1. Write an algorithm using a WHILE…DO loop to display the numbers 1 to 10.  
2. Write an algorithm using a WHILE…DO loop to display the even numbers from 2 to 20.  
3. Write an algorithm using a WHILE…DO loop to count backwards from 10 to 1.  
4. Write an algorithm using a WHILE…DO loop that will read numbers from the user and stop when 0 is entered.  
5. Write an algorithm using a WHILE…DO loop to calculate and display the square of numbers from 1 to 5.  

---

# Page 12  
## Answers to WHILE…DO Questions  
### Example_WD1  

plaintext
START
// Declare and initialise the counter variable
SET count = 1
// WHILE condition is tested BEFORE the loop runs
WHILE count <= 10 DO
// Display the current value of count
PRINT count
// Update the counter to avoid an infinite loop
SET count = count + 1
ENDWHILE
END

---

# Page 13  
## Answers to WHILE…DO Questions  
### Example_WD2  

plaintext
START
// Declare and initialise the counter variable
SET number = 2
// WHILE condition is tested BEFORE the loop runs
WHILE number <= 20 DO
// Display the current even number
PRINT number
// Update the counter to the next even number
SET number = number + 2
ENDWHILE
END

#### Input/Processing/Output  
- Input: numbers between 2 – 20  
- Processing: Check number to see if divisible by 2, display number  
- Output: Display even numbers between 2 and 20  

---

# Page 14  
## Answers to WHILE…DO Questions  
### Example_WD3  

plaintext
START
// Declare and initialise the counter variable
SET count = 10
// WHILE condition is tested BEFORE the loop runs
WHILE count >= 1 DO
// Display the current value of count
PRINT count
// Decrease the counter to move backwards
SET count = count - 1
ENDWHILE
END

---

# Page 15  
## Answers to WHILE…DO Questions  
### Example_WD4  

plaintext
START
// Read the first number from the user
INPUT number
// WHILE condition is tested BEFORE the loop runs
WHILE number <> 0 DO
// Display the number entered
PRINT number
// Read the next number
INPUT number
ENDWHILE
END

---

# Page 16  
## Answers to WHILE…DO Questions  
### Example_WD5  

plaintext
START
// Declare and initialise the counter variable
SET number = 1
// WHILE condition is tested BEFORE the loop runs
WHILE number <= 5 DO
// Display the square of the number
PRINT number * number
// Update the counter to avoid an infinite loop
SET number = number + 1
ENDWHILE
END

---

# Page 17  
## REPEAT…UNTIL Control Structure  
- **Description:**  
The REPEAT…UNTIL structure operates similarly to the DOWHILE structure; however, it tests the condition at the end rather than the beginning.  
- Statements inside the loop are executed at least once before the condition check.  
- **Logic:**  
If the condition is false, statements will be repeated until the condition becomes true.  
- Contrast with DOWHILE:  
  - ‘DOWHILE <condition>’ is equivalent to ‘REPEAT…UNTIL <negation of condition>’.  
- No need for a priming read when utilizing REPEAT…UNTIL loops.  

---

# Page 18  
## REPEAT…UNTIL Structure Comparison  
- One read statement at the loop start suffices. An additional IF statement could prevent processing of a trailer record if needed.  
- Example algorithm using DOWHILE:  

plaintext
Process_student_records
Set student_count to zero
Read student record
DOWHILE student_number NOT = 999
Write student record
increment student_count
Read student record
ENDDO
END
Print student_count

- This can be transformed into a REPEAT…UNTIL structure:  

plaintext
Process_student_records
Set student_count to zero
REPEAT
Read student record
IF student number NOT = 999 THEN
Write student record
increment student_count
ENDIF
UNTIL student number = 999
END
Print student_count

---

# Page 19  
## Example 5.4: Processing Inventory Items  
- A program to read inventory records containing:  
  - item number  
  - item description  
  - stock figure  
- The final record in the file has an item number of zero.  
- The goal is to generate a low stock item report, printing records with stock figures less than 20.  
- Outputs:  
  - A heading at the report’s top  
  - Total count of low stock items at the report’s end.  

### Defining Diagram:  
- **Input:** inventory record
  - item_number  
  - item_description  
  - stock_figure  
- **Processing:** Read inventory records, identify low stock items.  
- **Output:** Print low stock records.
  - total_low_stock_items.  

---

# Page 20  
## Example 5.4: Processing Inventory Items [Algorithm]  
- **Solution Algorithm Using REPEAT…UNTIL:**  

plaintext
Process_inventory_records
Set total_low_stock_items to zero
Print 'Low Stock Items' heading
REPEAT
Read inventory record
IF item_number > zero THEN
IF stock_figure < 20 THEN
Print item_number, item_description, stock_figure
increment total_low_stock_items
ENDIF
ENDIF
UNTIL item_number = zero
Print total_low_stock_items
END

### Input/Processing/Output  
- **Input:** inventory record  
- **Processing:** Select low stock items  
- **Output:** Print total low stock items  

---

# Page 21  
## Questions on REPEAT…UNTIL  
1. Create an IPO model and a REPEAT…UNTIL algorithm to read student IDs and test scores. Stop when a student ID of 0 is entered. Display only those who scored 50 or more and show the total number of students who passed.  
2. Create an IPO model and a REPEAT…UNTIL algorithm to read employee numbers and overtime hours. Stop when an employee number of 0 is entered. Display only employees who worked more than 10 overtime hours, and show the total number of such employees.  
3. Create an IPO model and a REPEAT…UNTIL algorithm to read book numbers and days overdue. Stop when a book number of 0 is entered. Display only overdue books by more than 7 days and show the total overdue books.  
4. Create an IPO model and a REPEAT…UNTIL algorithm to read item numbers and stock figures. Stop when an item number of 0 is entered. Display only items with stock figures less than 25, showing the total number of low-stock items.  
5. Create an IPO model and a REPEAT…UNTIL algorithm to read order numbers and order amounts. Stop when an order number of 0 is entered. Display orders with amounts over 5,000, showing the total number of large orders.  

---

# Page 22  
## Answers to REPEAT…UNTIL Questions  
### Example Solution for Student ID and Test Scores  
- **Input/Processing/Output Structure:**  

plaintext
Input student_id
Check if test_score ≥ 50
Count number of students who passed
Print total number of students who passed

---

# Page 23  
## Answers for Student ID and Test Scores  
### Example: REPEAT…UNTIL Algorithm  

plaintext
START
Set pass_count = 0
REPEAT
Input student_id
IF student_id ≠ 0 THEN
Input test_score
IF test_score ≥ 50 THEN
Print student_id, test_score
Set pass_count = pass_count + 1
ENDIF
ENDIF
UNTIL student_id = 0
Print pass_count
END

---

# Page 24  
## Answers for Employee Numbers and Overtime Hours  
### Example: REPEAT…UNTIL Algorithm  

plaintext
START
Set overtime_count = 0
REPEAT
Input employee_number
IF employee_number ≠ 0 THEN
Input overtime_hours
IF overtime_hours > 10 THEN
Print employee_number, overtime_hours
Set overtime_count = overtime_count + 1
ENDIF
ENDIF
UNTIL employee_number = 0
Print overtime_count
END

---

# Page 25  
## Answers for Books and Days Overdue  
### Example: REPEAT…UNTIL Algorithm  

plaintext
START
Set overdue_count = 0
REPEAT
Input book_number
IF book_number ≠ 0 THEN
Input days_overdue
IF days_overdue > 7 THEN
Print book_number, days_overdue
Set overdue_count = overdue_count + 1
ENDIF
ENDIF
UNTIL book_number = 0
Print overdue_count
END

---

# Page 26  
## Answers for Item Numbers and Stock Figures  
### Example: REPEAT…UNTIL Algorithm  

plaintext
START
Set low_stock_count = 0
REPEAT
Input item_number
IF item_number ≠ 0 THEN
Input stock_figure
IF stock_figure < 25 THEN
Print item_number, stock_figure
Set low_stock_count = low_stock_count + 1
ENDIF
ENDIF
UNTIL item_number = 0
Print low_stock_count
END

---

# Page 27  
## Answers for Order Numbers and Order Amounts  
### Example: REPEAT…UNTIL Algorithm  

plaintext
START
Set large_order_count = 0
REPEAT
Input order_number
IF order_number ≠ 0 THEN
Input order_amount
IF order_amount > 5000 THEN
Print order_number, order_amount
Set large_order_count = large_order_count + 1
ENDIF
ENDIF
UNTIL order_number = 0
Print large_order_count
END
```


Page 34

Conclusion

  • Thank you for your attention.
  • Insert the subtitle of your presentation.