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

  1. Input and Output Statements: Learn the basic statements used for I/O operations.
  2. Reading Input: Understand how to read different types of data from the user.
  3. Writing Output: Learn how to display data to the user.
  4. 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 input
  • write 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

  1. Variable Declaration:

    integer a, b;
    real sum;
    

    We declare two integer variables a and b, and one real variable sum.

  2. Output Statement:

    write("Enter two integers: ");
    

    The write statement displays the prompt to the user.

  3. Input Statement:

    read(a, b);
    

    The read statement takes two integers as input from the user and stores them in a and b.

  4. Calculation:

    sum := a + b;
    

    We calculate the sum of a and b and store it in sum.

  5. 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

integer num;
write("Enter an integer: ");
read(num);
write("You entered: ", num);

Reading a Real Number

real num;
write("Enter a real number: ");
read(num);
write("You entered: ", num);

Reading Multiple Values

integer x, y;
write("Enter two integers: ");
read(x, y);
write("You entered: ", x, " and ", y);

Writing Output

The write statement can be used to display various types of data. Here are some examples:

Writing an Integer

integer num;
num := 10;
write("The integer is: ", num);

Writing a Real Number

real num;
num := 3.14;
write("The real number is: ", num);

Writing Multiple Values

integer x, y;
x := 5;
y := 10;
write("The values are: ", x, " and ", y);

Formatting Output

Formatting output can make it more readable. ALGOL provides ways to format the output.

Example: Formatting Output

real num;
num := 123.456789;
write("Formatted number: ", num:8:2);

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.

© Copyright 2024. All rights reserved