HCI 3. Control Statements
Uploaded by adrianwang2003 · 28 May 2024
Preview
Text from the first pagesHwa Chong Institution H2 Computing 1 3 Control Statements Learning Outcome 3.1 Selection: if and if-else Statements Selection statements allow a computer to make choices based on a condition. 3.1.1 The Boolean Type, Comparisons, and Boolean Expressions Boolean data type consists of two values: true and false (typically through standard True/False) For example, 4 != 4 evaluates to False. 3.1.2 One-Way Selection Statement (if statement) 3.1.3 Two-Way Selection Statements (if-else statements) Programming Elements and Constructs Apply the fundamental programming constructs to control the flow of program execution: Sequence, Selection, Iteration if x < 0: x = - x
Hwa Chong Institution H2 Computing 2 Syntax: If-else statement can be used to check inputs for errors: 3.1.4 Multi-Way if Statements Testing conditions that entail more than two alternative courses of action. Syntax: Example: the grading scheme below can be described in code by a multi-way if statement.
Hwa Chong Institution H2 Computing 3 Another way to describe the grading scheme in pseudocode is to make use of CASE statements. BEGIN INPUT number CASE OF number > 89: letter ← 'A' > 79: letter ← 'B' > 69: letter ← 'C' OTHERWISE: letter ← 'F' ENDCASE END Note that the case clauses are tested in sequence. When a case that applies is found, its statement is executed and the CASE statement is complete. Any remaining cases are not tested. 3.1.5 Logical Operators and Compound Boolean Expressions When there are multiple conditions to check, we can use logical operators and compound Boolean expressions to simplify the code. Logical Operators: and, or, not Examples:
Hwa Chong Institution H2 Computing 4 Compare the two approaches below to check inputs for errors: The logical operators are evaluated after comparisons but before the assignment operator not has higher precedence than and and or TYPE OF OPERATOR OPERATOR SYMBOL Exponentiation ** Arithmetic negation - Multiplication, division, quotient, remainder *, /, //, % Addition, subtraction +, - Comparison ==, !=, <, >, <=, >= Logical negation not Logical conjunction and disjunction and, or Assignment = 3.1.6 Nested if Statements An auto insurance agency assigns rates based on sex and age. Males below age 25 pay the highest premium, $1000. Males 25 or older pay $400. Females below age 21 pay $800, whereas those 21 or older pay $500. if gender == "m": if age < 25: rate = 1000 else: rate = 400 else: if age< 21: rate = 800 else: rate = 500
Hwa Chong Institution H2 Computing 5 Some nested if statements can be described as an else-if statements. Nested if statements elif statements score = int(input("Enter score: ")) print ("Grade of student is:", end = " ") if score>=90: print ("A") else: if score >=80: print ("B") else: if (score >= 70): print ("C") else: print ("F") score = int(input("Enter score: ")) print ("Grade of student is:", end = " ") if score>=90: print ("A") elif score >=80: print ("B") elif score >= 70: print ("C") else: print("F") Tutorial 3A 1. Determine the value of each of the following Python expressions: a. not ((4.5 < 12.9) and (6 * 2 >= 13)) b. not ((4.5 < 12.9) or (6 * 2 >= 13)) c. (0 == 1) or (2 < 3) and (7 < 6) d. (2 < 3) or (0 == 1) and (7 < 6) e. (50 < 100) and ('100' < '1034') 2. What’s the difference? (a) score = int(input ("Enter score: ")) eligible = input("Enter eligibility: ") if score > 90: if eligible == "Y": print ("Hire") else: print ("Reject") (b) score = int(input ("Enter score: ")) eligible = input("Enter eligibility: ") if score > 90: if eligible == "Y": print ("Hire") else: print ("Reject")
Hwa Chong Institution H2 Computing 6 3. Write code to determine someone’s risk of heart disease using the following rules based on age and body mass index (BMI). 4. Suppose that a student is assigned a band 1, 2 or 3 as follows: - Band 1 for a score of at least 85 - Band 2 for a score of under 85 but at least 70 - Band 3 for a score under 70 Neither of the program segments below is well-written -- one is correct but inefficient; the other is incorrect. Explain what is wrong with each. if score >= 70: band = 2 elif score >=85: band = 1 else: band = 3 if score >= 85: band = 1 if score >= 70 and score < 85: band = 2 If score < 70: band = 3 5. Assume that the variables x and y refers to strings. Write a code segment that prints these strings in alphabetical order. You should assume that they are not equal. 6. [Referring to the practice question in Tutorial 1A] John is requesting a program that computes his income tax. The rule for calculation is Taxpayers must enter their gross income. Taxpayers are allowed $10,000 standard deduction. For each dependent, taxpayer is allowed additional $2000 deduction. Income tax is charged at a flat rate of 20% based on taxable income after all deductions. Write a program following your design in Tutorial 1A. 7. In a two-person game of Rock-Scissors-Paper, each player selects either ‘R’, ‘S’ or ‘P’. The winner is determined as follows: Rock breaks Scissors. Paper covers Rock. Scissors cuts Paper. The game is a tie if both players select the same choice. Write a program to allow players make their input and output the winner.
Hwa Chong Institution H2 Computing 7 3.2 Definite Iteration: The for Loop – Repetition statements (or loops) repeat an action – Each repetition of action is known as pass or iteration 3.2.1 Range Function Range function generates a list of integers to help to iterate the loop. It expects up to three arguments. – range (<upper bound + 1>) With one argument only, the list starts from 0 and includes all numbers smaller than the argument. – range (<lower bound>, <upper bound + 1>) With two arguments, the list starts from the first argument and includes all numbers smaller than the second argument. – range (<lower bound>, <upper bound + 1>, <step>) With three arguments, it allows you specify a step value. When the third parameter is a negative number, it becomes a list that counts down. 3.2.2 Executing a Statement a Given Number of Times Python’s for loop is control statement that most easily supports definite iteration. The form of this type of loop is: loop header statements in body must be indented and aligned in the same column loop body
Hwa Chong Institution H2 Computing 8 Example: Loop to compute an exponentiation for a non-negative exponent. If the exponent were 0, the loop body would not execute and value of product would remain as 1 Example: Loop to print the same scripts several times. 3.2.3 Control Variable Incrementing the control variable by step of 1 FOR i ← 1 to 4 OUTPUT i, " squared is ", i * i ENDFOR The output will be: 1 squared is 1 <-- printed when i = 1 2 squared is 4 <-- printed when i = 2 3 squared is 9 <-- printed when i = 3 4 squared is 16 <-- printed when i = 4 Decrementing the control variable by step of 1 FOR i ← 4 TO 1 STEP −1 OUTPUT i, " squared is ", i * i ENDFOR The output will be: 4 squared is 16 <-- printed when i = 4 3 squared is 9 <-- printed when i = 3 2 squared is 4 <-- printed when i = 2 1 squared is 1 <-- printed when i = 1
Hwa Chong Institution H2 Computing 9 Do Not Alter the Control Variable A statement in the for loop body should never assign a value to the
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

