Sunday 16 May 2010

Multiple Conditional Statements in Delphi ( Delphi Tutorial # 10 )

You can use if statement for multiple conditional statement but it will be little bit messy.

For example :

program multiple_cond_if;
{$APPTYPE CONSOLE}
uses
 SysUtils;

var
 mark : integer;
begin
 write('Your mark : ');
 readln(mark);
 write('Your grade is ');
 if mark >= 90 then
  writeln('A')
 else
  if mark >= 70 then
   writeln('B')
  else
   if mark >= 60 then
    writeln('C')
   else
    if mark >= 40 then
     writeln('D')
    else
     writeln('E');
 readln;
end.

Instead of using if … then statement, it will be easier using case … of statement :

program multiple_cond_case;
{$APPTYPE CONSOLE}
uses
 SysUtils;

var
 mark : integer;
begin
 write('Your mark : ');
 readln(mark);
 write('Your grade is ');
 case mark of
  0..39   : writeln('E');
  40..59  : writeln('D');
  60..69  : writeln('C');
  70..89  : writeln('B');
  90..100 : writeln('A');
 end;
readln;
end.

Download the complete code here.

Go to Previous Tutorial or Next Tutorial

0 comments:

Post a Comment

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.