Conditional statements are fundamental in programming as they allow the execution of code based on certain conditions. In ALGOL, conditional statements help control the flow of the program by making decisions. This section will cover the basic syntax and usage of conditional statements in ALGOL.
Key Concepts
- If Statement: Executes a block of code if a specified condition is true.
- If-Else Statement: Executes one block of code if a condition is true and another block if the condition is false.
- Nested If Statements: An if or if-else statement inside another if or if-else statement.
- Else If Ladder: A series of if-else statements to check multiple conditions.
If Statement
The if statement in ALGOL is used to execute a block of code only if a specified condition is true.
Syntax
Example
Explanation
x := 10;assigns the value 10 to the variablex.- The
ifstatement checks ifxis greater than 5. - If the condition is true, it executes the
printstatement.
If-Else Statement
The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.
Syntax
Example
begin
integer x;
x := 3;
if x > 5 then
print("x is greater than 5")
else
print("x is not greater than 5");
endExplanation
- The
ifstatement checks ifxis greater than 5. - If the condition is true, it executes the first
printstatement. - If the condition is false, it executes the
elseblock.
Nested If Statements
You can place an if or if-else statement inside another if or if-else statement to create complex decision-making structures.
Syntax
Example
begin
integer x, y;
x := 10;
y := 20;
if x > 5 then
if y > 15 then
print("x is greater than 5 and y is greater than 15")
else
print("x is greater than 5 but y is not greater than 15")
else
print("x is not greater than 5");
endExplanation
- The outer
ifstatement checks ifxis greater than 5. - If true, the inner
ifstatement checks ifyis greater than 15. - Depending on the conditions, the appropriate
printstatement is executed.
Else If Ladder
The else if ladder is used to check multiple conditions sequentially.
Syntax
if condition1 then
statement1
else if condition2 then
statement2
else if condition3 then
statement3
else
statement4;Example
begin
integer x;
x := 7;
if x > 10 then
print("x is greater than 10")
else if x > 5 then
print("x is greater than 5 but less than or equal to 10")
else if x > 0 then
print("x is greater than 0 but less than or equal to 5")
else
print("x is less than or equal to 0");
endExplanation
- The
ifstatement checks ifxis greater than 10. - If false, the first
else ifchecks ifxis greater than 5. - If false, the second
else ifchecks ifxis greater than 0. - If all conditions are false, the
elseblock is executed.
Practical Exercises
Exercise 1
Write an ALGOL program that checks if a number is positive, negative, or zero and prints the result.
Solution
begin
integer num;
num := -5;
if num > 0 then
print("The number is positive")
else if num < 0 then
print("The number is negative")
else
print("The number is zero");
endExercise 2
Write an ALGOL program that assigns a grade based on a score. The grading criteria are:
- Score >= 90: Grade A
- Score >= 80: Grade B
- Score >= 70: Grade C
- Score >= 60: Grade D
- Score < 60: Grade F
Solution
begin
integer score;
score := 85;
if score >= 90 then
print("Grade A")
else if score >= 80 then
print("Grade B")
else if score >= 70 then
print("Grade C")
else if score >= 60 then
print("Grade D")
else
print("Grade F");
endCommon Mistakes and Tips
- Missing Semicolons: Ensure each statement ends with a semicolon.
- Indentation: Proper indentation improves readability but is not syntactically required in ALGOL.
- Logical Errors: Double-check conditions to avoid logical errors in decision-making.
Conclusion
In this section, we covered the basics of conditional statements in ALGOL, including if, if-else, nested if, and else if ladders. Understanding these concepts is crucial for controlling the flow of your programs. Practice the exercises to reinforce your understanding and prepare for more advanced topics in the next module.
