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 statementThe 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:
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 ShowMessage('Zero value encountered!') ; 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 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;
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.
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;
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.
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.
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;
The CASE statementAlthough, 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.
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
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;
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.