Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Understanding and Using Decisions

By Zarko Gajic, About.com

if language = Delphi then
begin
Use(language)
end
else
Skip(language) ;

Branching
If you want to control the flow of code execution depending on what the program has already done or what it has just encountered you need to use one of the two Delphi Pascal branching statements: if statements and case statements.

The IF THEN ELSE statement

The if statement is used to test for a condition and then execute sections of code based on whether that condition is True or False. The condition is described with a Boolean expression, If the condition is True, the code flow branches one way. If the condition is False, the flow branches in another direction. Let's see this behavior on an example:
var iNumber : Integer;
begin
//some value must be
//assigned to iNumber here!

if iNumber = 0 then
   ShowMessage('Zero value encountered!') ;
end;
If the number (assigned to iNumber variable) is 0, the expression iNumber = 0 evaluates to True and the message is displayed; otherwise, nothing is displayed. If we want more than one thing to happen when the tested condition is True, we can write multiple statements in a begin ... end block.
var iNumber : Integer;
begin
  //some value must be
  //assigned to iNumber here!

  if iNumber = 0 then
  begin
   ShowMessage('Zero value encountered!') ;
   Exit; // exit from the current procedure
  end;
  //if iNumber is 0 the folowing
  //code will never be executed

  ShowMessage('Nobody likes 0, ha!') ;
end;
More often, we will want to process multiple statements if a condition is True or False.
var iNumber : Integer;
begin
  //some value must be
  //assigned to iNumber here!

  if iNumber < 0 then
  begin
   //statements ...
   ShowMessage('Your number is negative!') ;
   //statements ...
  end
  else
  begin
   //statements ...
   ShowMessage('Your number is positive or zero!') ;
   //statements ...
  end;
end;
Note: Each statement in the begin..end block ends with a semicolon. We cannot have a semicolon before or after the else keyword. The if-then-else statement, is a single statement, therefore we cannot place a semicolon in the middle of it.

An if statement can be quite complex. The condition can be turned into a series of conditions (using the and, or and not Boolean operators), or the if statement can nest a second if statement.

var
  iNumber : Integer;
begin
  if iNumber = 0 then
  begin
   ShowMessage('Zero number not allowed!') ;
   exit;
  end
  else
  //no need to use begin-end here
  if iNumber < 0 then
    ShowMessage('Your number is negative!')
  else
    ShowMessage('Your number is positive!') ;
end;
Note: When you write nested if statements choose a consistent, clear indentation style. This will help you and anyone else who reads your code see the logic of the if statement and how the code flows when your application runs.

The CASE statement

Although, we can use the if statement for very complex (nested) condition testing, the case statement is usually easier to read (debug!) and the code runs more quickly.

The case statement makes it clear that a program has reached a point with many branches; multiple if-then statements do not.

var
  iNumber : Integer;
begin
  //some value must be
  //assigned to iNumber here!

  case iNumber of
   0 : ShowMessage('Zero value') ;
   1..10 : ShowMessage('Less than 11, greater than 0') ;
   -1, -2, -3 : ShowMessage('Number is -1 or -2 or -3') ;
   else
    ShowMessage('I do not care') ;
  end;
end;
What follows the case keyword is usually called the selector. The selector is a variable or expression taken from either the char type or any integer type (an ordinal type). String type are invalid!. However, the StringToCaseSelect custom function enables you to use the Case statement with string type variables

As you can see, the individual case statements use a single constant, a group of constants (separated by comma), or a range of constants (double dot separated). We can even add an else keyword to take care of all the remaining cases at once.

Note 1: Only one case statement will be executed, we cannot have overlapping conditions in the case statements.

Note 2: If you want to include more than one statement in the part following the colon (:), place the begin and end keywords around the multiple statements.

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Getting Started with Delphi
  5. Understanding and Using Decisions (IF And Case statemens) in Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.