- 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 Statement
The if-then statement is the simplest form of control statement, frequently used in decision making and changing the control flow of the program execution.
Syntax
Syntax for if-then statement is −
if condition then S
Where condition is a Boolean or relational condition and S is a simple or compound statement. Example of an if-then statement is −
if (a <= 20) then c:= c+1;
If the boolean expression condition evaluates to true, then the block of code inside the if statement will be executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing end;) 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 ifChecking; var { local variable declaration } a:integer; begin a:= 10; (* check the boolean condition using if statement *) if( a < 20 ) then (* if condition is true then print the following *) writeln('a is 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 less than 20 value of a is : 10