In this section, we will cover the fundamental concepts of input and output (I/O) in ALGOL. Understanding how to handle I/O is crucial for interacting with users and processing data. We will explore how to read data from the user and display results.
Key Concepts
- Input and Output Statements: Learn the basic statements used for I/O operations.
- Reading Input: Understand how to read different types of data from the user.
- Writing Output: Learn how to display data to the user.
- Formatting Output: Explore ways to format the output for better readability.
Input and Output Statements
ALGOL uses specific statements for input and output operations. The primary statements are:
read
for inputwrite
for output
Example: Basic Input and Output
begin integer a, b; real sum; write("Enter two integers: "); read(a, b); sum := a + b; write("The sum of ", a, " and ", b, " is ", sum); end
Explanation
-
Variable Declaration:
integer a, b; real sum;
We declare two integer variables
a
andb
, and one real variablesum
. -
Output Statement:
write("Enter two integers: ");
The
write
statement displays the prompt to the user. -
Input Statement:
read(a, b);
The
read
statement takes two integers as input from the user and stores them ina
andb
. -
Calculation:
sum := a + b;
We calculate the sum of
a
andb
and store it insum
. -
Output Result:
write("The sum of ", a, " and ", b, " is ", sum);
The
write
statement displays the result.
Reading Input
The read
statement can be used to read various types of data. Here are some examples:
Reading an Integer
Reading a Real Number
Reading Multiple Values
Writing Output
The write
statement can be used to display various types of data. Here are some examples:
Writing an Integer
Writing a Real Number
Writing Multiple Values
Formatting Output
Formatting output can make it more readable. ALGOL provides ways to format the output.
Example: Formatting Output
Explanation
num:8:2
specifies that the number should be displayed in a field of width 8 with 2 decimal places.
Practical Exercises
Exercise 1: Simple Input and Output
Task: Write a program that reads an integer from the user and prints whether it is even or odd.
Solution:
begin integer num; write("Enter an integer: "); read(num); if num mod 2 = 0 then write(num, " is even") else write(num, " is odd"); end
Exercise 2: Sum of Two Real Numbers
Task: Write a program that reads two real numbers from the user and prints their sum.
Solution:
begin real a, b, sum; write("Enter two real numbers: "); read(a, b); sum := a + b; write("The sum of ", a, " and ", b, " is ", sum:8:2); end
Common Mistakes and Tips
- Forgetting to Declare Variables: Always declare your variables before using them.
- Incorrect Data Types: Ensure the data type of the variable matches the type of data you are reading.
- Formatting Issues: Use appropriate formatting to make the output readable.
Conclusion
In this section, we covered the basics of input and output in ALGOL. You learned how to use the read
and write
statements to interact with the user, and how to format the output for better readability. Practice the exercises to reinforce your understanding, and you'll be ready to move on to more complex topics in ALGOL programming.