If Else in Pascal
Our first example demonstrates branching with if
and else
statements in Pascal. Here’s the full source code:
To run the program, save it as if_else_example.pas
and compile it using a Pascal compiler such as Free Pascal:
Note that in Pascal, you need to use then
after the condition in an if
statement, and begin
/end
blocks are required for multiple statements within a branch. The else if
is written as else if
in Pascal, not as a single keyword like in some other languages.
Pascal uses and
and or
for logical operators instead of &&
and ||
. The modulo operator is mod
in Pascal.
Unlike some modern languages, Pascal doesn’t have a ternary operator, so you’ll need to use a full if
statement even for basic conditions.
Pascal’s WriteLn
function is used for console output, similar to fmt.Println
in the original example.
Remember that Pascal is case-insensitive, so WriteLn
and writeln
are equivalent. However, it’s good practice to maintain consistent capitalization for readability.