If I...

 

Hi ! Nice to meet you again. And now, we're getting closer, right ? We will begin our lesson with conditional branching. What is conditional branching anyway ? Well, it's a if not this, do this, else do that ... ummm similar things like that, you know. We have the conditions, and if the conditions are satisfied, do some statements, otherwise do something else.

Pascal provides two ways to implement conditional branching. One uses if, the other uses case ... of. Well, let's discuss the first part : if The structure of if in Pascal is like this :

if condition then
begin
  :
  :
end;

OR, if you add the else part :

if condition then
begin
  :
  :
end         <-- notice that the semicolon disappeared now !
else
begin
  :
  :
end;

Now, you ask : How could Pascal determine the process ? If the condition(s) beside the word if is met, then the block begin...end -- just below the if -- will be executed, otherwise, then the block begin..end after the else will be executed. If we have the if structure as the first one -- that is, no else block -- the program execution will continue after the word end. Let us examine the excerpt of the code below:


if counter<10 then
begin
  counter:=counter+1;
  writeln(counter);
end;
writeln('Done');

If the variable counter has the value less than 10, the commands between begin and end will be executed. If it has the value 10 or more, it skips the process just after the end. It means that it does the writeln('Done');. But, don't be mistaken. Even the counter has the value less than 10, after it has done the commands between begin and end, it continues the process to the next statement -- that is writeln('Done');. How about adding else block ? It just as the same as above, but of course with some differences. See the examples, and run them ! Input some numbers and see how it works. To clarify how the if works, refer to these programs


program if1;
var
  i : byte;
begin
  writeln('Enter a number = '); readln(i);
  if i <= 10 then
  begin
    writeln('It is done if i <= 10 only');
    writeln('Or you just can omit the begin..end block');
    writeln('if there is only ONE command inside (See IF2.PAS)');
  end;
  writeln('We always do this');
end.

program if2;
var
  i : byte;
begin
  writeln('Enter a number = '); readln(i);
  if i <= 10 then writeln('It is done if i <= 10 only');
  writeln('We always do this');
end.

program if3;
var
  i : byte;
begin
  writeln('Enter a number = '); readln(i);
  if i <= 10 then
     writeln('It is done if i <= 10 only') { Omit the semicolon ; }
  else
     writeln('It is done if i > 10 only'); { There is a semicolon }
  writeln('We always do this');
end.

program if4;
var
  i : byte;
begin
  writeln('Enter a number = '); readln(i);
  if i <= 10 then
  begin
    writeln('It is done if i <= 10 only');
    writeln('Yeah, you can add begin..end block to else part too !');
  end   { Omit the semicolon BEFORE the end keyword }
  else writeln('It is done if i > 10 only.');
  writeln('We always do this');
end.

program if5;
var
  i : byte;
begin
  writeln('Enter a number = '); readln(i);
  if i <= 10 then
  begin
    writeln('It is done if i <= 10 only');
    writeln('Yeah, you can add begin..end block to else part too !');
  end   { Omit the semicolon BEFORE the end keyword }
  else
  begin
     writeln('It is done if i > 10 only.');
     writeln('Look !');
  end;
  writeln('We always do this');
end.

Nested if

Now how to have some ifs inside an if ? Yeah, right ! That's perfectly legal provided if you don't forget to wrap the begin..end (if there are two or more statements inside). Here is one example:


if i<10 then
begin
  writeln('i is more than 10.');
  if j>3 then writeln('j is more than 3.');
end;

Yes, yes ! You may do if inside the begin..end block after the else too. Why don't you do some experiments ?

Combining the conditions

Pascal lets us combine the conditions. Say that if a salesman is qualified to get the project if he has sold 7000 pieces of products and he has at least 3 years of experience. This is the example :


if (productsold>=7000) and (experienceyear>=3) then qualified;

Yes, this cannot be run of course. In this example I would like to introduce the and, or, and xor keywords. The keyword and means that it will return true if both conditions are met. It means that the first begin..end block will be executed only if both conditions are true.

The keyword or means that if there is at least one of the conditions is met (or both), then the first begin..end block will be executed. The keyword xor means that if there is ONLY one condition is met (not both), the first begin..end block will be executed. The keyword not means negating all conditions. Then if the condition is true, it will be considered false and vice versa. Now, take a look at these codes :


program if6;
var
  i, j : byte;
begin
  write('Enter a value for i = '); readln(i);
  write('Enter a value for j = '); readln(j);
  if (i>3) and (j>4) then
  begin
    writeln('This will be done if i>3 and j>4');
    writeln('Now change the "and" with "or" and "xor"');
  end;
end.

program if7;
var
  i : byte;
begin
  write('Enter a value for i = '); readln(i);
  if not (i>3) then
  begin
    writeln('This will be done if i is NOT more than 3');
    writeln('So, do you understand ?');
  end;
end.

How will it be if the conditions are more than 2 ? Simply wraps them with brackets, two by two. Note : The conditions inside the brackets will be done first. Example, look at this :


program if8;
var
  i, j, k : byte;
begin
  write('Enter a value for i = '); readln(i);
  write('Enter a value for j = '); readln(j);
  write('Enter a value for k = '); readln(k);
  if ((i>3) and (j>4)) or (k>5) then
  begin
    writeln('Yeah !!');
    writeln('Now change the bracket orders and run it again !');
  end;
end.

Case..of

Sometimes, you really need to select conditions according some criteria. You're clever ! Use some ifs and let it be done ! But how if the criterium was quite long and complex ? I'm sure that it would be a daunting task to have an if for each. How could we categorize the criteria with some similarities and simplify the problem.

Suppose we have to do the grading system for our campus. Say, if the student has 80 or more deserves an A. 70 to 79 is for B, 60 to 69 is C, 50 to 59 is D, and 49 or below is E. The if example would be like this : (Note that mark is a byte)


if mark>=80 then
  grade:='A'
else   { 79 or below goes here }
  if mark>=70 then
    grade:='B'
  else   { 69 or below goes here }
    if mark>=60 then
      grade:='C'
    else   { 59 or below goes here }
      if mark>=50 then
        grade:='D'
      else   { 49 or below goes here }
        grade:='E';

Wow, that's pretty long. Now see this :


    case mark of
      80..100: grade:='A';
      70..79:  grade:='B';
      60..69:  grade:='C';
      50..59:  grade:='D';
      else     grade:='E';
    end;

Simple and elegant ! Now, let's learn about readkey statement. It is one of the commands included in crt unit, like clrscr. What it does ? It receives the ASCII code of pressed keyboard button. Well, what is ASCII code anyway? It is some sort of codes that is defined for computers. If we pressed the keyboard, it produces some sort of code, and then the computer translates it into the ASCII code, the code we commonly (programmers) know.

Note for advanced programmers :
Don't laugh at this, I have to explain what ASCII code exactly to the non-programmer folks in a practical and easy way without going into the complicated talks ! Yes, we know the "origin" keyboard codes. I do too.)

Readkey is a function, returning a char value. OK, let's see how it works !


program test_readkey;
uses crt;
var
  c : char;
begin
  c:=readkey;
  case c of
    #8 : writeln('You presses backspace');
    #9 : writeln('You presses tab');
    #13: writeln('You presses enter');
    #27: writeln('You presses escape');
    #32: writeln('You presses space');
    else writeln('You presses other key');
  end;
end.

The program simply waits for a keypress then detects it and quits. Now you asked : Why do we use the # sign ? Because it denotes a character with a code. Suppose #8 means a character with a code of 8. Why don't we use the .. like the previous example ? Because in this case we don't specify ranges of values while the first example did. Like 80..100 means from 80 to 100.

Let us detect the arrow keys. Arrow keys, just like function keys, are extended. It means it generates a special codes. Now, here is the code to trap them :


program trap_arrow;
uses crt;
var
  c : char;
begin
  c:=readkey;
  if c=#0 then   { If extended codes, }
  begin
    c:=readkey;  { read the code once more }
    case c of
      #72: writeln('Up arrow');
      #75: writeln('Left arrow');
      #77: writeln('Right arrow');
      #80: writeln('Down arrow');
    end;
  end;
end.

How to detect the ASCII number ? How do you know that ? Easy, with ord. Suppose you want to know the ASCII codes for each keyboard keys. Do this :


program trap_key;
uses crt;
var
  c : char;
begin
  c:=#0;
  while c<>#27 do
  begin
    c:=readkey;
    if c=#0 then   { If extended codes, }
    begin
      c:=readkey;  { read the code once more }
      writeln('Extended : ',ord(c));
    end
    else writeln(ord(c));
  end;
end.

Now, press keyboard keys, and see the codes. Combine it with Ctrl, Alt, and Shift and see what happens. To understand the program above, you need to know what while means. The statements inside the while will be repeated on and on as long as the condition (c<>#27) is met. It means the c:= readkey; if c=#0 ... will be repeated as long as c is not equal to character 27. Since character 27 is escape, then the program simply runs until user presses Esc. More about while and other repetitional commands will be explained thoroughly in chapter 5.

Well, that's all for now. Practice hard to get to the top ! If there are some questions, direct it to me


Where to go ?

Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 2 about extending first program
To Chapter 4 about constants in Pascal
My page of programming link
Contact me


By : Roby Joehanes, © 1996, 2000