Unable to open this exercise
Selection is a fundamental programming construct that allows an algorithm to choose alternative execution paths based on whether a conditional expression evaluates to TRUE or FALSE.
1. Single and Two-Way Selection
Syntax:
IF <Condition> THEN
// Code executed when Condition is TRUE
ELSE
// Code executed when Condition is FALSE
ENDIF
- The
ELSEclause is optional if no action is required when the condition is false. - Every
IFstatement must terminate with a matchingENDIF.
2. Multi-Way Nested Selection
When selecting between three or more outcomes, IF statements can be nested inside the ELSE branch:
IF Mark >= 80 THEN
Grade <- "A"
ELSE
IF Mark >= 70 THEN
Grade <- "B"
ELSE
IF Mark >= 60 THEN
Grade <- "C"
ELSE
Grade <- "U"
ENDIF
ENDIF
ENDIF
Notice that three opened IF statements require three matching ENDIF statements at the close.
3. Logical Operators in Conditions
Compound conditions combine multiple relational comparisons using Boolean operators:
Operator | Meaning | Example |
|---|---|---|
| Evaluates to |
|
| Evaluates to |
|
| Inverts the logical state ( |
|
AI Socratic Tutor
Need guidance? Ask a question or use the hint below.
AI Socratic Tutor is an All-Access Benefit
Unlock step-by-step Socratic hints, algorithm trace table walkthroughs, and error diagnostics for every Cambridge exercise.
You're chatting with an AI tutor, not a person. AI can make mistakes, so double-check its answers.
Independent Practice: Grade boundaries
Read an integer mark from 0 to 100. Output A for at least 80, B for at least 60 but below 80, C for at least 40 but below 60, otherwise U.
Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.
Interactive exercises for Module 2 and beyond require an active plan.
Run code to inspect step-by-step variable trace states.