Conditional Statements

 

Introduction

When programming, you will ask the computer to check various kinds of situations and to act accordingly. The computer performs various comparisons of various kinds of statements. These statements come either from you or from the computer itself, while it is processing internal assignments.

Let’s imagine you are writing an employment application and one question would be, "Do you consider yourself a hot-tempered individual?" The source file of such a program would look like this:

program Project1;

{$APPTYPE CONSOLE}

var
Answer : Char;

begin
	Write('Do you consider yourself a hot-tempered individual? ');
	Readln(Answer);

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end. 

Here is an example of running the program:

Do you consider yourself a hot-tempered individual? g

Press any key to continue...

Some of the answers a user would type are y, yes, Y, Yes, YES, n, N, no, No, NO, I don’t know, Sometimes, Why are you asking?, and What do you mean? The variety of these different answers means that you should pay attention to how you structure your programs, you should be clear to the users.

A better version of the line that asks the question would be:

Write('Do you consider yourself a hot-tempered individual(y=Yes/n=No)? ');

This time, although the user can still type anything, at least you have specified the expected answers.

 

if a Condition is True

In Object Pascal, comparisons are made from statements. Examples of statements are:

  • "You are 12 years old" 
  • "It is raining outside" 
  • "You live in Sydney"

One of the comparisons the computer performs is to find out if a statement is true (in reality, programmers (like you) write these statements and the computer only follows your logic). If a statement is true, the computer acts on a subsequent instruction.

The comparison using the if statement is used to check whether a condition is true or false. The syntax to use it is:

if Condition then Statement

If the Condition is true, then the compiler would execute the Statement. The compiler ignores anything else:

If the statement to execute is (very) short, you can write it on the same line with the condition that is being checked. Consider a program that is asking a user to answer Yes or No to a question such as "Are you ready to provide your credit card number?". A source file of such a program could look like this:

program Project1;

{$APPTYPE CONSOLE}

var
Answer : Char;

begin
	Write('Are you ready to provide your credit card number(1=Yes/0=No)? ');
	Readln(Answer);

	// Since the user is ready, let's process the credit card transaction
	if Answer = '1' then Writeln('Now we will need your credit card number.');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

This would produce:

Are you ready to provide your credit card number(1=Yes/0=No)? 0

Press any key to continue...

You can write the if condition and the statement on different lines. This makes your program easier to read. The above code could be written as follows:

program Project1;

{$APPTYPE CONSOLE}

var
	Answer : Char;

begin
	Write('Are you ready to provide your credit card number(1=Yes/0=No)? ');
	Readln(Answer);

	// Since the user is ready, let's process the credit card transaction
	if Answer = '1' then
		Writeln('Now we will need your credit card number.');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

You can also write the statement on its own line if the statement is too long to fit on the same line with the condition. Although the (simple) if statement is used to check one condition, it can lead to executing multiple dependent statements. If that is the case, the statement section must start with the begin keyword and with the end keyword. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Answer : Char;
	CreditCardNumber: string;

begin
	Write('Are you ready to provide your credit card number(1=Yes/0=No)? ');
	Readln(Answer);

	// Since the user is ready, let's process the credit card transaction
	if Answer = '1' then
	begin
		Writeln('Now we will need your credit card number.');
		Writeln('Please enter your credit card number without spaces: ');
		Readln(CreditCardNumber);
	end;

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Are you ready to provide your credit card number(1=Yes/0=No)? 1
Now we will need your credit card number.
Please enter your credit card number without spaces:
23423

Press any key to continue...

If you omit the begin and end combination, only the statement that immediately follows the condition would be executed.

6.2.3 Otherwise: if…else

The if condition is used to check one possibility and ignore anything else. Usually, other conditions should be considered. In this case, you can use more than one if statement. For example, on a program that asks a user to answer Yes or No, although the positive answer is the most expected, it is important to offer an alternate statement in case the user provides another answer. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Do you consider yourself a hot-tempered individual(y=Yes/n=No)? ');
	Readln(Answer);

	if Answer = 'y' then // First Condition
	begin
		Writeln('This job involves a high level of self-control.');
		Writeln('We will get back to you.');
	end;

	if Answer = 'n' then // Second Condition
		Write('You are hired!');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Do you consider yourself a hot-tempered individual(y=Yes/n=No)? y
This job involves a high level of self-control.
We will get back to you.

Press any key to continue...

The problem with the above program is that the second if is not an alternative to the first, it is just another condition that the program has to check and execute after executing the first. On that program, if the user provides y as the answer to the question, the compiler would execute the content of its statement and the compiler would execute the second if condition.

You can also ask the compiler to check a condition; if that condition is true, the compiler would execute the intended statement. Otherwise, the compiler would execute alternate statement. This is performed using the syntax:

if Condition then
	Statement1
else
	Statement2

The above program would better be written as:

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Do you consider yourself a hot-tempered individual(y=Yes/n=No)? ');
	Readln(Answer);

	if Answer = 'y' then // First Condition
		Writeln('This job involves a high level of self-control.')
	else // Any Alternate Condition
		Writeln('You are hired!');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Do you consider yourself a hot-tempered individual(y=Yes/n=No)? g
You are hired!

Press any key to continue...

Notice that the Statement in the if condition does not end with a semi-colon. If the Statement of the if condition spans more than one line using the begin and end combination, the last line of its statement and its end keywords should not use a semi-colon but the lines above them must. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Do you consider yourself a hot-tempered individual(y=Yes/n=No)? ');
	Readln(Answer);

	if Answer = 'y' then // First Condition
	begin
		Writeln('This job involves a high level of self-control.');
		Writeln('We will get back to you!')
	end
	else // Any Alternate Condition
		Writeln('You are hired!');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

The if…else if and if…else if…else Statements

The previous conditional formula is used to execute one of two alternatives. Sometimes, your program will need to check many more than that. The syntax for such a situation is:

if Condition1 then
	Statement1
else if Condition2 then
	Statement2;

An alternative syntax would add the last else as follows:

if Condition1 then Statement1else if Condition2 then Statement2else Statement-n; if Condition1 then Statement1else if Condition2 then Statement2else if Condition3 then Statement3else Statement-n;

The compiler will check the first condition. If Condition1 is true, it will execute Statement1. If Condition1 is false, then the compiler will check the second condition. If Condition2 is true, it will execute Statement2. When the compiler finds a Condition-n to be true, it will execute its corresponding statement. It that Condition-n is false, the compiler will check the subsequent condition. This means you can include as many conditions as you see fit using the else if statement. If after examining all the known possible conditions you still think that there might be an unexpected condition, you can use the optional single else.

A program we previously wrote was considering that any answer other than y was negative. It would be more professional to consider a negative answer because the program anticipated one. Therefore, here is a better version of the program:

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Do you consider yourself a hot-tempered individual(y=Yes/n=No)? ');
	Readln(Answer);

	if Answer = 'y' then // First Condition
	begin
		Writeln('This job involves a high level of self-control.');
		Writeln('We will get back to you!')
	end
	else if Answer = 'n' then // Alternate condition to the first
		Writeln('You are hired!')
	else
		Writeln('That''s not a valid answer!');

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Do you consider yourself a hot-tempered individual(y=Yes/n=No)? h
That's not a valid answer!

Press any key to continue...

The case...of Statement

When defining an expression whose result would lead to a specific program execution, the case...of statement considers that result and executes a statement based on the possible outcome of that expression. The different outcomes are listed in the body of the case...of statement and each case has its own execution, if necessary. The syntax of the case...of statement is:

case Expression of
case Choice1:
Statement1;
case Choice2:
Statement2;
case Choice-n:
Statement-n; 
end

The expression to examine can be an integer, a character, an enumeration type, or a predefined list of items. Here is an example of using the case…of statement:

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Type a number between 1 and 3: ');
	Readln(Number);

	case Number of
		1: Writeln('You typed 1');
		2: Writeln('You typed 2');
		3: Writeln('You typed 3');
	end;

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Type a number between 1 and 3: 3
You typed 3

Press any key to continue...

The program above would request a number from the user. If he types 1, it would execute the first statement. If the user types 2, it would execute the second statement, etc. If the user types any other number, no case would execute.

When establishing the possible outcomes that the case…of statement should consider, at times there will be other possibilities other than those listed and you will be likely to consider them. This special case is handled by the else keyword. The else case would be considered if none of the listed cases matches the supplied answer. The syntax of using the else condition and executing its statement is: 

case Expression of
case Choice1:
Statement1;
case Choice2:
Statement2;
case Choice-n:
Statement-n;
Else
ElseStatement;
end

Therefore another version of the program above would be

program Project1;

{$APPTYPE CONSOLE}

var
	Number: Integer;
	Answer: Char;

begin
	Write('Type a number between 1 and 3: ');
	Readln(Number);

	case Number of
		1: Writeln('You typed 1');
		2: Writeln('You typed 2');
		3: Writeln('You typed 3');
	else
		Writeln('Invalid Number!');
	end;

	Write(Chr(10), 'Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Type a number between 1 and 3: 8
Invalid Number!

Press any key to continue...

Previous Copyright © 2004 FunctionX, Inc. Next