VJC Chapter 3 Control flow
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pagesVJC/H2Computing/9569 Chapter 3: Control Flow Contents 1 What is control flow? 1.1 Algorithm 1.2 Pseudo code 1.3 Flow chart 2 Control flow statements 2.1 If 2.1.1 Short hand if 2.1.2 Short hand if-else 2.1.3 Evaluating non-zero numerical values 2.1.4 Evaluating non-empty strings 2.1.5 Built-in bool() method 2.2 While 2.3 For 2.3.1 The range() function 2.3.2 Iterating by Sequence Index 2.4 Break, continue and pass 2.4.1 Break 2.4.2 Continue 2.4.3 Pass 3 Decision table Syllabus Learning Outcomes 1.1 Algorithmic Representation Write algorithms in pseudo-code and flowchart for given problems. 1.1.1 Use appropriate techniques or tools such as pseudo-code and flowchart to show program flow. 1.1.2 Use standard flowchart symbols. 1.1.3 Use a combination of various control structures. 1.1.4 Use decision tables to explore the actions for combinations of different input conditions. Note: up to three conditions 2.2 Programming Elements and Constructs Use programming language elements and constructs to write recursive and non-recursive programs to solve a variety of problems. 2.2.3 Apply the fundamental programming constructs to control the flow of program execution: – Sequence – Selection – Iteration 1
VJC/H2Computing/9569 1 What is control flow? In the examples that we have seen till now, there has always been a series of statements faithfully executed by Python in exact top-down order. What if you wanted to change the flow of how it works? For example, you want the program to take some decisions and do different things depending on different situations, such as printing 'Good Morning' or 'Good Evening' depending on the time of the day? As you might have guessed, this is achieved using control flow statements. There are three control flow statements in Python - if, for and while. 1.1 Algorithm An algorithm is a step-by-step procedure (well-defined instructions) to solve a given problem. For instance, the algorithm to find the sum of two numbers is 1. Take two numbers 2. Add the numbers 3. Display the result Or the algorithm to log in to VJC portal 1. Go to https://portal.vjc.sg/ 2. Enter vjc email and password 3. Click on Sign in button 4. Enter OTP 5. Click on Submit button Sometimes there might be different steps depending on some other conditions. For example, if the day is a weekday, you will wake up at 6:00 am, else you will wake up at 10:00 am. Many problems have more than one solution. Sometimes it is a personal preference which solution to choose. Sometimes one solution will be measurably better than another. We can write algorithm using pseudo code or flowchart. 1.2 Pseudo code Pseudo code is a description of the steps in an algorithm using natural language (such as English) mixed with sequence, selection and iteration constructs. 2
VJC/H2Computing/9569 Sequence construct is when a number of steps are performed one after the other. Selection construct allows some steps to be performed under certain conditions, otherwise different steps (or no steps) are performed. Repetition construct is when a sequence of steps is performed a number of times. This is also known as iteration or looping. A general idea to solving a problem involves inputting data to the computer, processing the data and outputting results. English Pseudo code Example General Assignment and sequence Set A to 34 Increment B A 34 B B + 1 <identifier> <value> Selection If A is greater than B then do something, else do another thing IF A > B THEN Do something ELSE Do another thing ENDIF IF <condition> THEN <statements> ELSE <statements> ENDIF Repetition Repeat some steps until A is equal to B REPEAT Some steps UNTIL A = B REPEAT <statements> UNTIL <condition> While Num is smaller than 10, increment Num WHILE Num < 10 DO Num Num + 1 ENDWHILE WHILE <condition> DO <statements> ENDWHILE For index 1 to 30, assign an empty string to Names FOR Index 1 TO 30 Names[Index] '' ENDFOR FOR <identifier> <value1> TO <value2> <statements> ENDFOR Input Take in an input and assign to A with and without a prompt INPUT A INPUT ʺ Enter name: ʺ A INPUT <identifier> Output Output/print “Message” Output B OUTPUT ʺ Message ʺ OUTPUT B OUTPUT ʺ Your age is ʺ , B OUTPUT <values(s)> 3
VJC/H2Computing/9569 A condition consists of at least one logic proposition. Logic propositions use the relational operators. Operator Comparison = Is equal to < Is less than > Is greater than <= Is less than or equal to >= Is greater than or equal to <> Is not equal to The pseudo code presented in the table is the recommended style of writing pseudo code in examination papers. In the recommended style, keywords are in uppercase with indentation at appropriate parts of the algorithm. However, there is no fixed or defined syntax. It is acceptable as long as the logic correctly solves the problem. 1.3 Flow chart A flowchart is the graphical or pictorial representation of an algorithm with the help of different symbols, shapes, and arrows to demonstrate a process or a program. There are 6 basic symbols commonly used in Flowchart (We focus only on the first 5) Symbols Name Description Start/End Indicates the starting or ending of the program Input/Output Use for Input/Output(I/O) operation i.e. taking input and showing output 4
VJC/H2Computing/9569 Process Indicates any type of internal operations like initialization, calculation etc. Decision Use for asking questions that can have either TRUE or FALSE (YES or NO) as an answer Control flow Show direction of flow Connector Connectors are used to connect breaks in the flowchart. If a flowchart takes more than one page, then we use the connector to connect the flowchart between pages. (However, A-level questions on flowchart are usually within 1 page.) 2 Control flow statements There are three control flow statements in Python: if , for and while . 2.1 IF statement The if statement is a selection construct that is used to check a condition: if the condition is true, we run a block of statements (called the if-block), else we process another block of statements (called the else-block). The else clause is optional. If we need to have another if-else statement in an else-block, we can simplify the code using if-elif-else statement. Syntax Flowchart if <condition>: <statement> 5
VJC/H2Computing/9569 if <condition>: <statement> else: <statement> if <condition>: <statement> elif <condition>: <statement> else: <statement> 6
VJC/H2Computing/9569 When there is more than one condition to be evaluated, logical operators can be used. 2.1.1 Short hand if If you have only
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

