Case statements are similar to If statements. The difference is that the case statement can make the code simpler to read and work with. In some computer languages and computer systems, the case statements run faster than the If statements.
Creating a case statement is simple to do. The next program will prompt the user to select the key A-D and the program will respond by telling the user what key was entered.
program CaseExample;
{$mode objfpc}{$H+}
uses
sysutils,
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
sel:char;
begin
write('Please Select a Key (A, B, C, D): ');
readln(sel);
writeln;
case sel of
'A', 'a': begin
writeln('A Was Selected');
end;
'B', 'b': begin
writeln('B Was Selected');
end;; ‘
'C', 'c': begin
writeln('C Was Selected');
end;
'D', 'd': begin
writeln('D Was Selected');
end;
end; {End of Case}
writeln; writeln;
writeln('Press <Enter> To Quit');
readln;
end.
This next program uses an if statement to print a grade based upon a score entered into the computer. As you can see, reading it can be a little difficult. After reviewing the code of this program, take a look at the program that uses case statements. The program with the case statement will be easier to read when compared to the program with the if statement. Both programs do the same thing.
program GradeIf;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
score:integer;
begin
write('Enter Score: ');
readln(score);
writeln;
if score <= 59 then
begin
writeln('Grade F');
end
else if (score < 63) and (score >= 60) then
begin
writeln('Grade D-');
end
else if (score < 67) and (score >= 63) then
begin
writeln('Grade D');
end
else if (score < 70) and (score >= 67) then
begin
writeln('Grade D+');
end
else if (score < 73) and (score >= 70) then
begin
writeln('Grade C-');
end
else if (score < 77) and (score >= 73) then
begin
writeln('Grade C');
end
else if (score < 80) and (score >= 77) then
begin
writeln('Grade C+');
end
else if (score < 83) and (score >= 80) then
begin
writeln('Grade B-');
end
else if (score < 87) and (score >= 83) then
begin
writeln('Grade B');
end
else if (score < 90) and (score >= 87) then
begin
writeln('Grade B+');
end
else if (score < 93) and (score >= 90) then
begin
writeln('Grade A-');
end
else if (score < 97) and (score >= 93) then
begin
writeln('Grade A');
end
else
begin
writeln('Grade A+');
end;
writeln;
writeln;
writeln('Press <Enter> To Quit');
readln;
end.
This next program uses a case statement to print a grade based upon a score entered into the computer. This program does the same thing that the if statement program does.
program GradeCase;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes
{ you can add units after this };
var
score:integer;
begin
write('Enter A Score: ');
readln(score);
writeln;
case score of
0..59: begin
writeln('Grade F');
end;
60..62: begin
writeln('Grade D-');
end;
63..66: begin
writeln('Grade D');
end;
67..69: begin
writeln('Grade D+');
end;
70..72: begin
writeln('Grade C-');
end;
73..76: begin
writeln('Grade C');
end;
77..79: begin
writeln('Grade C+');
end;
80..82: begin
writeln('Grade B-');
end;
83..86: begin
writeln('Grade B');
end;
87..89: begin
writeln('Grade B+');
end;
90..92: begin
writeln('Grade A-');
end;
93..96: begin
writeln('Grade A');
end;
else
writeln('Grade A+');
end; {ends the case statement}
writeln;
writeln;
writeln('Press <Enter> To Quit');
readln;
end.
Video Tutorial: Free Pascal Tutorial 8 - Case Statements - Lazarus (5:13)