Decision Making — IF & CASE Grade 10
Decision structures let your program choose different paths based on conditions. Delphi uses if…then…else and case for this.
What is Decision Making?
Decision-making (also called conditional logic) is when a program tests whether something is true or false and then chooses what to do next based on the answer. A condition is any question that can only be answered with true or false — for example, "is the age 18 or more?".
Everyday analogy: A robot (traffic light) at an intersection makes a decision for you. If the light is green you go; else you stop. Your program does the same thing — it looks at a condition and takes one path or another.
Why Do Programs Need to Make Decisions?
Without decisions, a program could only ever do the exact same thing every single time it runs. Decisions make a program react to its data and its user.
- React to input – an age-check on a website: if you are old enough it lets you in, otherwise it blocks you.
- Validate data – if a mark is below 0 or above 100, warn the user it is invalid.
- Choose an outcome – if a learner scored 50 or more, display "Passed", otherwise display "Failed".
- Stay safe – if the password matches, grant access; otherwise deny it.
The IF Statement
Evaluates a condition (true/false) and executes code accordingly.
The IF statement is the most common decision in programming. In plain words it says: "If this is true, then do that; otherwise (else) do something else." Everyday analogy: "If it is raining, take an umbrella, else wear a cap." The "if it is raining" part is the condition that is checked first.
IF … THEN (one-way)
if condition then
statement;IF … THEN … ELSE (two-way)
Never put a ; on the line before else — it ends the if statement too early.
if condition then
statement
else
statement;Multiple Statements — use BEGIN…END
if iAge >= 18 then
begin
lblResult.Caption := 'Adult';
btnVote.Enabled := True;
end
else
begin
lblResult.Caption := 'Minor';
btnVote.Enabled := False;
end;Examples
if iNum1 > iNum2 then
ShowMessage(IntToStr(iNum1) + ' is larger');
if age >= 18 then
ShowMessage('Old enough to vote');
if username <> 'admin' then
ShowMessage('Access denied')
else
ShowMessage('Welcome, administrator!');Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | iAge = 18 |
<> | Not equal to | sName <> 'Admin' |
> | Greater than | iScore > 50 |
< | Less than | iAge < 18 |
>= | Greater than or equal to | iMark >= 30 |
<= | Less than or equal to | rPrice <= 100.0 |
Boolean Operators
Combine multiple conditions in one statement.
Boolean operators let you ask more than one question at the same time. Everyday analogy: to get into a club you might need to be 18 AND have an ID — both must be true. To qualify for a discount you might need to be a student OR a pensioner — either one is enough. NOT simply flips an answer around, like saying "if it is not a holiday, school is open".
| Operator | Rule | Example |
|---|---|---|
AND | Both conditions must be true | (iAge >= 18) AND (bHasID = True) |
OR | At least one condition must be true | (sGender = 'M') OR (sGender = 'F') |
NOT | Inverts the condition | NOT (iScore > 50) |
if (iAge >= 18) AND (bHasLicense = True) then
bCanDrive := True;
if (iScore >= 30) OR (bHasSymbol = True) then
lblResult.Caption := 'Passed';Nested IF
An IF inside another IF.
if iAge >= 18 then
begin
if bHasLicense = True then
bDrive := True
else
bDrive := False;
end
else
bDrive := False;Decision Components
RadioGroup
Uses an ItemIndex property — first item is index 0.
if rgpGender.ItemIndex = 0 then
sGender := 'Male'
else
sGender := 'Female';CheckBox
Uses the Checked property — True or False.
if chkInsurance.Checked = True then
rCost := rCost + rInsuranceAmount;The IN Operator
Tests if a value is in a set. Cleaner than multiple OR conditions.
// Check for specific values
if iNumber IN [1, 3, 5, 7] then
ShowMessage('Odd single digit found');
// Range using ..
if iMark IN [0..49] then
lblSymbol.Caption := 'F';
// Works with Char too
if cLetter IN ['A', 'E', 'I', 'O', 'U'] then
ShowMessage('Vowel');The CASE Statement
Cleaner alternative to multiple IF…ELSE when testing one variable against many values. Works with Integer and Char only.
Use CASE when one variable could take many possible values and each value leads to a different outcome — like turning a mark into a grade symbol. Everyday analogy: a vending machine. You press one button (the value), and depending on which button it was, a different snack drops out. Writing that as a long chain of IF…ELSE statements works, but CASE lays it out neatly so it is much easier to read.
case expression of
value1: statement1;
value2: statement2;
value3: statement3;
else
statementDefault;
end;CASE has no begin, but always has end. You can use ranges with ..
case iMark of
80..100: lblSymbol.Caption := '7 — Outstanding';
70..79: lblSymbol.Caption := '6 — Meritorious';
60..69: lblSymbol.Caption := '5 — Substantial';
50..59: lblSymbol.Caption := '4 — Adequate';
40..49: lblSymbol.Caption := '3 — Moderate';
30..39: lblSymbol.Caption := '2 — Elementary';
else
lblSymbol.Caption := '1 — Not Achieved';
end;MessageDlg
A pop-up dialog that asks the user a yes/no question and returns a result.
if MessageDlg('Are you sure?', mtConfirmation, mbYesNo, 0) = mrYes then
// execute if user clicked Yes
ShowMessage('Confirmed!');| Parameter | Options |
|---|---|
| Message type | mtConfirmation, mtInformation, mtWarning, mtError |
| Buttons | mbYesNo, mbOKCancel, mbYesNoCancel |
| Result | mrYes, mrNo, mrOK, mrCancel |