Tuesday, March 22, 2011

Branching

A branching mechanism in a programming language is simply a method to make path-dependent choices. Three different branching mechanisms are introduced here; the case statement, the if-then compound statement and the if-then-else compound statement.
The case statement simply requests the program evaluates an expression according to label lists. When those labels equate with the expression then the a statement is carried out. The case statement concludes with an end.

Here's a simple example.

program LeapYear; {Decides whether an entered year might be a leap year}
var Year: integer;
procedure NonCenturyYear (Year : integer);
begin
case (Year mod 4) of
0: writeln('That year is a leap year');
3: writeln('Next year might be a leap year!');
2, 1: writeln('That is not a leap year');
end {case}
end;

procedure CenturyYear (Year : integer);
begin
if (Year mod 400) = 0
then writeln('That is a leap year')
else writeln('That is not a leap year')
end;

begin {program}
writeln('Please enter a year you wish to test');
readln(Year);
if (Year mod 100) = 0
then CenturyYear (Year)
else NonCenturyYear (Year)
end.


You will note that there are a lot of conditional statements in the text of this program. The entire point of a program is to calculate these! To improve the program we have the opportunity to combine the case branching mechanism with the if-then and if-then-else branching mechanisms.

With an if-then branching mechanism a statement is evaluated. If it is true, then a statement is followed. If the statement is false it simply carries on to the next instruction. With an if-then-else statement the same applies except there is a specific statement when the initial statement evaluates as false.

Thus the leap year program can be expanded to the following, illustrating the use of case and the if-then-else branching mechanisms.


program LeapYear; {Decides whether an entered year might be a leap year}
var Year: integer;
procedure NonCenturyYear (Year : integer);
 begin
  case (Year mod 4) of
   0: writeln('That year is a leap year');
   3: writeln('Next year might be a leap year!');
   2, 1: writeln('That is not a leap year');
  end {case}
 end;
procedure CenturyYear (Year : integer);

begin
  if (Year mod 400) = 0
  then writeln('That is a leap year')
  else writeln('That is not a leap year')
end;
begin {program}
  writeln('Please enter a year you wish to test');
  readln(Year);
  if (Year mod 100) = 0
  then CenturyYear (Year)
  else NonCenturyYear (Year)
end.


The following is the compilation and some tests.


lev@squirrel:~/programming/pascal$ fpc leap.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling leap.pas
Linking leap
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
23 lines compiled, 0.2 sec
lev@squirrel:~/programming/pascal$ ./leap
Please enter a year you wish to test
0
That is a leap year
lev@squirrel:~/programming/pascal$ ./leap
Please enter a year you wish to test
1000
That is not a leap year
lev@squirrel:~/programming/pascal$ ./leap
Please enter a year you wish to test
2000
That is a leap year
lev@squirrel:~/programming/pascal$ ./leap
Please enter a year you wish to test
2011
Next year might be a leap year!

No comments: