HCI 3. Control Statements
Uploaded by adrianwang2003 · 28 May 2024
Preview
Hwa 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") el
Content continues in the PDF.
Related notes
- VJC Chapter 21 SQLite with PythonNotes/Practices · 2025
- VJC Chapter 23 Web Applications PrinciplesNotes/Practices · 2025
- VJC Chapter 10 RecursionNotes/Practices · 2025
- VJC Chapter 20 SQLNotes/Practices · 2025
- VJC Chapter 16 Hash TableNotes/Practices · 2025
- VJC Chapter 22 NoSQLNotes/Practices · 2025

