A condition is the logic inside a program that picks one state out of several. Talking about conditions always brings in boolean expressions.
A condition holds the logic that evaluates a boolean expression (whether the result is true or false), and after that it runs the matching function.
This time we'll look at branching conditions that use the IF ELSE keyword.
How if else Branching Works in Pascal
As I mentioned earlier, a condition is program logic that evaluates a boolean expression.
Let's take a simple example.
One day, Budi's mother sends him to the shop to buy a few things. Budi gets these instructions.
"Bud, buy me 1 kilo of eggs, and a bottle of brand A soy sauce, and some salt while you're at it. If there's no brand A soy sauce, buy 2 bottles of brand B, but you don't need to buy the salt."
Off Budi goes to the shop, cheerfully. He picks up 1 kilo of eggs and carries them along. Then he walks over to the soy sauce shelf. On that shelf, brand A soy sauce turns out to be missing. Following his mother's orders, Budi takes brand B soy sauce and leaves the salt alone.
The branching logic sits in the choice between buying soy sauce A or B. The boolean expression sits in the check of whether the shelf still has any. You can picture the decision flow as a branching diagram of the shopping conditions.
📷 Illustration: a flowchart of the branching decision on whether brand A soy sauce is available, which then determines the next shopping action.
Writing if else Branching in Pascal
The format for writing branching code with if else is roughly as follows.
if (kondisi) then
begin
{program logic when the condition is met}
end
else
begin
{program logic when the condition is not met}
end;
In that format, the condition is a boolean expression, whether a comparison or a logical operation.
Now let's write a program that checks whether the number a is greater than the number b. Type the code below into Free Pascal.
program Kondisi;
uses crt;
var
a, b: integer;
begin
clrscr;
write('masukkan a: '); readln(a);
write('masukkan b: '); readln(b);
if a > b then
writeln('a lebih dari b')
else
writeln('a kurang dari b');
readln;
end.
If that's done, run the program and give it whatever input you like. In this example I entered 10 for a and 6 for b.
masukkan a: 10
masukkan b: 8
a lebih dari b
So the code compares whether the value of a is greater than the value of b. If it is (true), the output is "a lebih dari b". If not (false), the output is "a kurang dari b". That seems clear enough, hehe.
In Pascal, an if else condition is a single unit, so the semicolon only needs to be on the last statement, after else.
Next, how do you check whether the values of a and b are equal? Simple, add one more condition after the last else. The code looks roughly like this.
program Kondisi;
uses crt;
var
a, b: integer;
begin
clrscr;
write('masukkan a: '); readln(a);
write('masukkan b: '); readln(b);
if a > b then
writeln('a lebih dari b')
else if a < b then
writeln('a kurang dari b')
else
writeln('a sama dengan b');
readln;
end.
Run it again and enter the same number for a and b. The output will look roughly like this.
masukkan a: 10
masukkan b: 10
a sama dengan b
That code has 3 conditions. The first one checks whether a is greater than b. If it is, the program executes writeln('a lebih dari b'). If not, it moves on to the second condition.
In the second condition we check whether a is less than b. If it is, the program executes writeln('a kurang dari b'). If not, it executes the code after else, namely writeln('a sama dengan b').
Oh, right: because that code only executes one statement after if, begin and end aren't needed. But if you want to execute more than one statement, begin and end are required.
For example:
....
if (a > b) then
begin
writeln('a lebih dari b');
writeln('a menang');
end
else
begin
writeln('a kurang dari b');
writeln('b menang');
end;
....
Wrapping Up
Okay, that's probably enough for this tutorial, hope it helps :D Next up we'll cover loops with the for loop in Pascal.