0% found this document useful (0 votes)
3 views28 pages

Problem Solving 6

This document discusses control structures in problem solving, focusing on working with arrays. It covers sequence structures, selection statements, iteration through loops, nested structures, and common errors in logic design. Key concepts include how arrays store values, the use of indices, and examples of various control structures in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views28 pages

Problem Solving 6

This document discusses control structures in problem solving, focusing on working with arrays. It covers sequence structures, selection statements, iteration through loops, nested structures, and common errors in logic design. Key concepts include how arrays store values, the use of indices, and examples of various control structures in programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 5: CONTROL STRUCTURES IN

PROBLEM SOLVING: WORKING WITH ARRAYS

By
Dr. Johnwendy

1
CONTROL STRUCTURES IN PROBLEM
SOLVING: Working with Array
Unit 5.1: Sequence Structure
Unit 5.2: Selection (If, If-Else, Switch)
Unit 5.3: Iteration (Loops – For, While, Do-While)
Unit 5.4: Nested Structures
Unit 5.5: Common Errors in Logic Design

2
Learning Goal
➢By the end, you should understand:
➢How the computer processes instructions
➢How arrays work with conditions and loops

3
Array
An array is a data structure that stores multiple values of the same data
type in a single variable, where each value is accessed using an index
(position).
Key Points
✓Stores many values in one name
✓All values must be the same type
✓Uses index (0, 1, 2, …)
✓Size is usually fixed

4
Understanding Array
An array is like a row of boxes.
Each box:
Stores a value
Has a position (index)
Think of this array:
DECLARE scores[5] = [10, 20, 30, 40, 50]
Index: 0 1 2 3 4
Value: 10 20 30 40 50

5
Important Rule

Index Value
0 10
1 20
2 30
3 40
4 50

6
Unit 5.1: Sequence Structure

START
DECLARE scores[3] = [5, 10, 15]
sum ← 0
sum ← sum + scores[0]
sum ← sum + scores[1]
sum ← sum + scores[2]
PRINT sum
END

7
Step-by-Step Execution
sum = 0
sum = 0 + 5 → 5
sum = 5 + 10 → 15
sum = 15 + 15 → 3

Final Answer: 30

8
Unit 5.2: Selection (If, If-Else, Switch)

Example 2: IF (Check Positive Numbers)


START
DECLARE arr[3] = [5, -2, 10]
FOR i ← 0 TO 2 DO
IF arr[i] > 0 THEN
PRINT "Positive"
ENDIF
ENDFOR
END

9
Execution

i arr[i] Condition Action


0 5 TRUE Print Positive
1 -2 FALSE Skip
2 10 TRUE Print Positive

Output
Positive
Positive

10
Unit 5.2: Selection (If, If-Else, Switch)
START
DECLARE scores[3] = [45, 70, 30]
FOR i ← 0 TO 2 DO
IF scores[i] ≥ 50 THEN
PRINT "Pass"
ELSE
PRINT "Fail"
ENDIF
ENDFOR
END

11
Execution

i score Result
0 45 Fail
1 70 Pass
2 30 Fail

Output
Fail
Pass
Fail
12
Unit 5.2: Selection (If, If-Else, Switch)
START
INPUT grade
SWITCH grade
CASE 'A': PRINT "Excellent"
CASE 'B': PRINT "Good"
CASE 'C': PRINT "Average"
DEFAULT: PRINT "Fail"
ENDSWITCH
END

13
Execution
Execution (grade = 'B')
A
B
Output
Good

14
Unit 5.3: Iteration (Loops – For, While, Do-
While)

START
DECLARE arr[3] = [10, 20, 30]
FOR i ← 0 TO 2 DO
PRINT arr[i]
ENDFOR
END

15
Execution
Output i arr[i] Output
10
0 10 10
20
30 1 20 20
2 30 30

16
Unit 5.3: Iteration (Loops – For, While, Do-
While)
START
DECLARE arr[3] = [5, 10, 15]
sum ← 0
FOR i ← 0 TO 2 DO
sum ← sum + arr[i]
ENDFOR
PRINT sum
END

17
Execution
Output
i Value sum
30
0 5 5

1 10 15

2 15 30

18
Unit 5.3: Iteration (Loops – For, While, Do-While)

START
i←0
WHILE i < 3 DO
PRINT i
i←i+1
ENDWHILE
END

19
Execution
Output i Output
0
1 0 0
2 1 1

2 2

20
Unit 5.3: Iteration (Loops – For, While, Do-While)

START
i←0
DO
PRINT i
i←i+1
WHILE i < 3
END

21
Execution
Execution
Runs first before checking condition
Output
0
1
2

22
Unit 5.4: Nested Structures

START
DECLARE matrix[2][2] = [[1,2],[3,4]]
FOR i ← 0 TO 1 DO
FOR j ← 0 TO 1 DO
PRINT matrix[i][j]
ENDFOR
ENDFOR
END

23
Execution
Output i j Output
1
0 0 1
2
3
0 1 2
4 1 0 3
1 1 4

24
Nested If
START
DECLARE arr[3] = [4, -3, 7]
FOR i ← 0 TO 2 DO
IF arr[i] > 0 THEN
IF arr[i] MOD 2 = 0 THEN
PRINT "Positive Even"
ELSE
PRINT "Positive Odd"
ENDIF
ENDIF
ENDFOR
END

25
Execution
Output Value Output
Positive Even
Positive Odd 4 Positive Even

-3 Skip

7 Positive Odd

26
Unit 5.5: Common Errors in Logic Design

START
DECLARE arr[3] = [10, 20, 30]
PRINT arr[3]
END

Output
ERROR (Index out of range)

27
ERROR – INFINITE LOOP
START
i←0
WHILE i < 3 DO
PRINT i
ENDWHILE
END
Output
0
0… forever

28

You might also like