In this section, we will explore the basic structure of an ALGOL program. Understanding the program structure is crucial as it forms the foundation upon which more complex programs are built. We will cover the following key concepts:
- Basic Structure of an ALGOL Program
- Sections of an ALGOL Program
- Syntax Rules
- Example Program
- Exercises
- Basic Structure of an ALGOL Program
An ALGOL program typically consists of the following parts:
- Program Header: The name of the program.
- Declarations: Variables, constants, and types are declared here.
- Statements: The executable part of the program.
General Structure
- Sections of an ALGOL Program
Program Header
The program header is optional but can be used to name the program. It is usually written as:
Declarations
This section is used to declare variables, constants, and types. Declarations are made before any executable statements.
Statements
This section contains the executable code. Statements are executed sequentially unless control structures (like loops or conditionals) alter the flow.
- Syntax Rules
Keywords
ALGOL uses specific keywords that are reserved and cannot be used as identifiers. Some common keywords include begin
, end
, if
, then
, else
, for
, while
, do
, etc.
Identifiers
Identifiers are names given to variables, constants, and functions. They must start with a letter and can be followed by letters and digits.
Comments
Comments are used to explain the code and are ignored by the compiler. In ALGOL, comments are enclosed in comment ... ;
.
Example of Syntax
- Example Program
Let's look at a simple ALGOL program that calculates the sum of two numbers and prints the result.
Code Example
program SumExample; begin integer num1, num2, result; num1 := 15; num2 := 25; result := num1 + num2; print(result); end
Explanation
- Program Header:
program SumExample;
names the program. - Declarations:
integer num1, num2, result;
declares three integer variables. - Statements:
num1 := 15;
assigns the value 15 tonum1
.num2 := 25;
assigns the value 25 tonum2
.result := num1 + num2;
calculates the sum ofnum1
andnum2
and stores it inresult
.print(result);
prints the value ofresult
.
- Exercises
Exercise 1
Write an ALGOL program that calculates the product of two numbers and prints the result.
Solution
program ProductExample; begin integer num1, num2, product; num1 := 5; num2 := 4; product := num1 * num2; print(product); end
Exercise 2
Modify the above program to read the numbers from the user instead of assigning them directly.
Solution
program ProductExample; begin integer num1, num2, product; read(num1); read(num2); product := num1 * num2; print(product); end
Common Mistakes and Tips
- Forgetting to declare variables: Always declare your variables before using them.
- Incorrect syntax for assignment: Use
:=
for assignment, not=
. - Mismatched
begin
andend
: Ensure everybegin
has a correspondingend
.
Conclusion
In this section, we covered the basic structure of an ALGOL program, including the program header, declarations, and statements. We also looked at the syntax rules and provided practical examples and exercises to reinforce the concepts. Understanding the structure of an ALGOL program is essential for writing more complex and functional programs. In the next section, we will delve into variables and data types in ALGOL.