The case Statement


The case statement is a Delphi language construct that can be used to test multiple conditions. In fact, the if-then statement is suitable only for a small number of tests because it soon becomes hard to both read and write. This is especially true for nested if-then statements or multiple if-then statements that involve Boolean operators. The case statement is more structured and more readable and tends to execute a bit faster than if-then statements.

Let's say that we have to make an application that outputs the name of the month depending on user input. Writing this test using the if-then statement is really time-consuming and long, as shown in Listing 3-10.

Listing 3-10: A long if-then statement

image from book
var   monthNum: Integer; begin   Write('Enter a number from 1 to 12: ');   ReadLn(monthNum);   if monthNum = 1 then     WriteLn('January')   else if monthNum = 2 then     WriteLn('February')   else if monthNum = 3 then     WriteLn('March')   else if monthNum = 4 then     WriteLn('April')   else if monthNum = 5 then     WriteLn('May')   else if monthNum = 6 then     WriteLn('June')   else if monthNum = 7 then     WriteLn('July')   else if monthNum = 8 then     WriteLn('August')   else if monthNum = 9 then     WriteLn('September')   else if monthNum = 10 then     WriteLn('October')   else if monthNum = 11 then     WriteLn('November')   else if monthNum = 12 then     WriteLn('December');   ReadLn; end.
image from book

Simple case Statements

The syntax of the case statement is:

case expression of   case_1: statement_1;   case_2: statement_2;   case_n: statement_n; end;

If you want to execute more statements in response to a condition, you have to write them inside a block. The syntax of the case statement that can execute more than one statement in response to a certain condition is:

case expression of   case_1: begin     statement_1;     statement_2;     statement_n;   end;   case_2: begin     statement_1;     statement_n;   end;   case_n: begin     statement_1;     statement_n;   end; end;

The application in Listing 3-10 that displays the names of the months is noticeably smaller and more readable when written using the case statement.

Listing 3-11: A simple case statement

image from book
var   monthNum: Integer; begin   Write('Enter a number from 1 to 12: ');   ReadLn(monthNum);   case monthNum of     1: WriteLn('January');     2: WriteLn('February');     3: WriteLn('March');     4: WriteLn('April');     5: WriteLn('May');     6: WriteLn('June');     7: WriteLn('July');     8: WriteLn('August');     9: WriteLn('September');     10: WriteLn('October');     11: WriteLn('November');     12: WriteLn('December');   end;   ReadLn; end.
image from book

Value Ranges

The case statement can also be used to test a range of values. The syntax for testing a range of values is:

case expression of   x_low..x_high: statement;   y_low..y_high: statement; end;

Listing 3-12: Testing a range of values

image from book
var   x: Integer; begin   Write('Enter a number from 0 to 200: ');   ReadLn(x);   case x of     0..99: WriteLn('Less than 100.');     100: WriteLn('100');     101..200: WriteLn('More than 100.');   end;   ReadLn; end.
image from book

Value Lists

You can use the case statement to test a list of values. The syntax for testing a list of values is:

case expression   value_1, value_2, value_n: statement;   value_3, value_4, value_n: statement; end; 

Listing 3-13: Testing a list of values

image from book
var   x: Integer; begin   Write('Enter a number from 1 to 10: ');   ReadLn(x);   case x of     1, 3, 5, 7, 9: WriteLn('Number ', x, ' is odd.');     2, 4, 6, 8, 10: WriteLn('Number ', x, ' is even.');   end;   ReadLn; end.
image from book

Case Statement Comparison

There are two major differences between the Delphi case statement and case statements in other popular programming languages, such as C++ and Visual Basic. The difference between Delphi and C++ is that the C++ switch statement enables the application to fall through conditions (for more details on fall-through, see "The switch Statement" section at the end of this chapter).

The difference between the Delphi case statement and the Visual Basic/VB.NET select statement is that the Delphi case statement uses only ordinal types, whereas the Visual Basic select statement can also use string values. Since Delphi uses only ordinal values, the execution of the Delphi case statement is much faster than that of the Visual Basic select statement. To see how you can use string values in the Delphi case statement, see Chapter 6.



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net