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.

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)

Syntax
if condition then
  statement;

IF … THEN … ELSE (two-way)

No semicolon before ELSE

Never put a ; on the line before else — it ends the if statement too early.

Syntax
if condition then
  statement
else
  statement;

Multiple Statements — use BEGIN…END

Delphi
if iAge >= 18 then
begin
  lblResult.Caption := 'Adult';
  btnVote.Enabled   := True;
end
else
begin
  lblResult.Caption := 'Minor';
  btnVote.Enabled   := False;
end;

Examples

Delphi
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

OperatorMeaningExample
=Equal toiAge = 18
<>Not equal tosName <> 'Admin'
>Greater thaniScore > 50
<Less thaniAge < 18
>=Greater than or equal toiMark >= 30
<=Less than or equal torPrice <= 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".

OperatorRuleExample
ANDBoth conditions must be true(iAge >= 18) AND (bHasID = True)
ORAt least one condition must be true(sGender = 'M') OR (sGender = 'F')
NOTInverts the conditionNOT (iScore > 50)
Delphi — AND and OR examples
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.

Delphi
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.

Delphi
if rgpGender.ItemIndex = 0 then
  sGender := 'Male'
else
  sGender := 'Female';

CheckBox

Uses the Checked property — True or False.

Delphi
if chkInsurance.Checked = True then
  rCost := rCost + rInsuranceAmount;

The IN Operator

Tests if a value is in a set. Cleaner than multiple OR conditions.

Delphi
// 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.

Syntax
case expression of
  value1: statement1;
  value2: statement2;
  value3: statement3;
else
  statementDefault;
end;
Remember

CASE has no begin, but always has end. You can use ranges with ..

Delphi — Grade symbol example
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.

Delphi
if MessageDlg('Are you sure?', mtConfirmation, mbYesNo, 0) = mrYes then
  // execute if user clicked Yes
  ShowMessage('Confirmed!');
ParameterOptions
Message typemtConfirmation, mtInformation, mtWarning, mtError
ButtonsmbYesNo, mbOKCancel, mbYesNoCancel
ResultmrYes, mrNo, mrOK, mrCancel