In this section, we will explore how to handle input and output (I/O) operations in REXX. Understanding I/O is crucial for interacting with users and other systems, making your programs more dynamic and useful.

Key Concepts

  1. Reading Input from the User
  2. Displaying Output to the User
  3. File I/O Basics

  1. Reading Input from the User

REXX provides a straightforward way to read input from the user using the PULL instruction.

Example: Reading User Input

/* REXX program to read user input */
say "Please enter your name:"
pull name
say "Hello, " name "!"

Explanation:

  • say "Please enter your name:" displays a prompt to the user.
  • pull name reads the input from the user and stores it in the variable name.
  • say "Hello, " name "!" outputs a greeting message using the input.

Exercise: Reading and Displaying User Input

Task: Write a REXX program that asks the user for their age and then displays a message indicating how old they will be in 5 years.

Solution:

/* REXX program to calculate age in 5 years */
say "Please enter your age:"
pull age
future_age = age + 5
say "In 5 years, you will be " future_age " years old."

  1. Displaying Output to the User

The SAY instruction is used to display output to the user. It can be used to print strings, variables, and expressions.

Example: Displaying Output

/* REXX program to display output */
name = "Alice"
age = 30
say "Name: " name
say "Age: " age

Explanation:

  • name = "Alice" assigns the string "Alice" to the variable name.
  • age = 30 assigns the number 30 to the variable age.
  • say "Name: " name and say "Age: " age display the values of name and age.

Exercise: Displaying Calculated Output

Task: Write a REXX program that calculates the sum of two numbers and displays the result.

Solution:

/* REXX program to calculate and display the sum of two numbers */
num1 = 10
num2 = 20
sum = num1 + num2
say "The sum of " num1 " and " num2 " is " sum "."

  1. File I/O Basics

REXX allows you to read from and write to files, which is essential for handling larger datasets and persistent storage.

Writing to a File

To write to a file, you use the CALL LINEOUT instruction.

Example: Writing to a File

/* REXX program to write to a file */
filename = "output.txt"
call lineout filename, "This is a line of text."
call lineout filename, "This is another line of text."

Explanation:

  • filename = "output.txt" specifies the name of the file.
  • call lineout filename, "This is a line of text." writes a line of text to the file.
  • Each call lineout appends a new line to the file.

Reading from a File

To read from a file, you use the CALL LINEIN instruction.

Example: Reading from a File

/* REXX program to read from a file */
filename = "output.txt"
line = linein(filename)
do while line \= ""
    say line
    line = linein(filename)
end

Explanation:

  • filename = "output.txt" specifies the name of the file.
  • line = linein(filename) reads the first line from the file.
  • The do while loop continues to read and display lines until the end of the file is reached.

Exercise: File I/O

Task: Write a REXX program that writes three lines of text to a file and then reads and displays the content of the file.

Solution:

/* REXX program to write and read from a file */
filename = "example.txt"
call lineout filename, "First line of text."
call lineout filename, "Second line of text."
call lineout filename, "Third line of text."

/* Reading from the file */
line = linein(filename)
do while line \= ""
    say line
    line = linein(filename)
end

Summary

In this section, we covered the basics of input and output in REXX:

  • Reading user input using PULL.
  • Displaying output using SAY.
  • Writing to and reading from files using CALL LINEOUT and CALL LINEIN.

Understanding these concepts is essential for creating interactive and data-driven REXX programs. In the next module, we will delve into more complex programming concepts, such as functions and subroutines.

© Copyright 2024. All rights reserved