
- Pascal Tutorial
- Pascal - Home
- Pascal - Overview
- Pascal - Environment Setup
- Pascal - Program Structure
- Pascal - Basic Syntax
- Pascal - Data Types
- Pascal - Variable Types
- Pascal - Constants
- Pascal - Operators
- Pascal - Decision Making
- Pascal - Loops
- Pascal - Functions
- Pascal - Procedures
- Pascal - Variable Scope
- Pascal - Strings
- Pascal - Booleans
- Pascal - Arrays
- Pascal - Pointers
- Pascal - Records
- Pascal - Variants
- Pascal - Sets
- Pascal - File Handling
- Pascal - Memory
- Pascal - Units
- Pascal - Date & Time
- Pascal - Objects
- Pascal - Classes
- Pascal Useful Resources
- Pascal - Quick Guide
- Pascal - Useful Resources
- Pascal - Discussion
Pascal - if then else statement
An if-then statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Syntax
Syntax for the if-then-else statement is −
if condition then S1 else S2;
Where, S1 and S2 are different statements. Please note that the statement S1 is not followed by a semicolon. In the if-then-else statements, when the test condition is true, the statement S1 is executed and S2 is skipped; when the test condition is false, then S1 is bypassed and statement S2 is executed.
For example,
if color = red then writeln('You have chosen a red car') else writeln('Please choose a color for your car');
If the boolean expression condition evaluates to true, then the if-then block of code will be executed, otherwise the else block of code will be executed.
Pascal assumes any non-zero and non-nil values as true, and if it is either zero or nil, then it is assumed as false value.
Flow Diagram

Example
Let us try a complete example that would illustrate the concept −
program ifelseChecking; var { local variable definition } a : integer; begin a := 100; (* check the boolean condition *) if( a < 20 ) then (* if condition is true then print the following *) writeln('a is less than 20' ) else (* if condition is false then print the following *) writeln('a is not less than 20' ); writeln('value of a is : ', a); end.
When the above code is compiled and executed, it produces the following result −
a is not less than 20 value of a is : 100
The if-then-else if-then-else Statement
An if-then statement can be followed by an optional else if-then-else statement, which is very useful to test various conditions using single if-then-else if statement.
When using if-then , else if-then , else statements there are few points to keep in mind.
An if-then statement can have zero or one else's and it must come after any else if's.
An if-then statement can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of the remaining else if's or else's will be tested.
No semicolon (;) is given before the last else keyword, but all statements can be compound statements.
Syntax
The syntax of an if-then-else if-then-else statement in Pascal programming language is −
if(boolean_expression 1)then S1 (* Executes when the boolean expression 1 is true *) else if( boolean_expression 2) then S2 (* Executes when the boolean expression 2 is true *) else if( boolean_expression 3) then S3 (* Executes when the boolean expression 3 is true *) else S4; ( * executes when the none of the above condition is true *)
Example
The following example illustrates the concept −
program ifelse_ifelseChecking; var { local variable definition } a : integer; begin a := 100; (* check the boolean condition *) if (a = 10) then (* if condition is true then print the following *) writeln('Value of a is 10' ) else if ( a = 20 ) then (* if else if condition is true *) writeln('Value of a is 20' ) else if( a = 30 ) then (* if else if condition is true *) writeln('Value of a is 30' ) else (* if none of the conditions is true *) writeln('None of the values is matching' ); writeln('Exact value of a is: ', a ); end.
When the above code is compiled and executed, it produces the following result −
None of the values is matching Exact value of a is: 100