In this section, we will cover the basics of input and output (I/O) operations in Fortran. Understanding how to read data from the user and display results is fundamental for any programming language. Fortran provides several ways to handle I/O operations, and we will explore the most commonly used methods.

Key Concepts

  1. READ Statement: Used to read input from the user or a file.
  2. WRITE Statement: Used to write output to the screen or a file.
  3. FORMAT Statement: Used to control the layout of the input and output.

READ Statement

The READ statement is used to read data from the standard input (usually the keyboard) or from a file. The basic syntax is:

READ(*, *) variable1, variable2, ...
  • The first asterisk * indicates the standard input.
  • The second asterisk * indicates list-directed input, which means the input format is determined by the data type of the variables.

Example

PROGRAM ReadExample
  IMPLICIT NONE
  INTEGER :: age
  REAL :: height

  PRINT *, 'Enter your age:'
  READ(*, *) age
  PRINT *, 'Enter your height in meters:'
  READ(*, *) height

  PRINT *, 'You are ', age, ' years old and ', height, ' meters tall.'
END PROGRAM ReadExample

Explanation

  • PRINT *, 'Enter your age:' displays a prompt to the user.
  • READ(*, *) age reads an integer value from the user and stores it in the variable age.
  • PRINT *, 'Enter your height in meters:' displays another prompt.
  • READ(*, *) height reads a real number from the user and stores it in the variable height.
  • Finally, the program prints the collected information.

WRITE Statement

The WRITE statement is used to write data to the standard output (usually the screen) or to a file. The basic syntax is:

WRITE(*, *) variable1, variable2, ...
  • The first asterisk * indicates the standard output.
  • The second asterisk * indicates list-directed output, which means the output format is determined by the data type of the variables.

Example

PROGRAM WriteExample
  IMPLICIT NONE
  INTEGER :: age
  REAL :: height

  age = 25
  height = 1.75

  WRITE(*, *) 'Age:', age
  WRITE(*, *) 'Height:', height
END PROGRAM WriteExample

Explanation

  • age = 25 assigns the value 25 to the variable age.
  • height = 1.75 assigns the value 1.75 to the variable height.
  • WRITE(*, *) 'Age:', age writes the string 'Age:' followed by the value of age to the standard output.
  • WRITE(*, *) 'Height:', height writes the string 'Height:' followed by the value of height to the standard output.

FORMAT Statement

The FORMAT statement is used to control the layout of the input and output. It allows you to specify the exact format in which data should be read or written.

Example

PROGRAM FormatExample
  IMPLICIT NONE
  INTEGER :: age
  REAL :: height

  age = 25
  height = 1.75

  WRITE(*, '(A, I3)') 'Age:', age
  WRITE(*, '(A, F5.2)') 'Height:', height
END PROGRAM FormatExample

Explanation

  • WRITE(*, '(A, I3)') 'Age:', age writes the string 'Age:' followed by the integer age in a field of width 3.
  • WRITE(*, '(A, F5.2)') 'Height:', height writes the string 'Height:' followed by the real number height in a field of width 5 with 2 decimal places.

Practical Exercises

Exercise 1: Simple Input and Output

Write a program that asks the user for their name and age, then prints a greeting message.

Solution

PROGRAM Greeting
  IMPLICIT NONE
  CHARACTER(LEN=50) :: name
  INTEGER :: age

  PRINT *, 'Enter your name:'
  READ(*, '(A)') name
  PRINT *, 'Enter your age:'
  READ(*, *) age

  PRINT *, 'Hello, ', TRIM(name), '! You are ', age, ' years old.'
END PROGRAM Greeting

Exercise 2: Formatted Output

Write a program that reads a real number from the user and prints it with 3 decimal places.

Solution

PROGRAM FormattedOutput
  IMPLICIT NONE
  REAL :: number

  PRINT *, 'Enter a real number:'
  READ(*, *) number

  WRITE(*, '(F7.3)') number
END PROGRAM FormattedOutput

Common Mistakes and Tips

  • Forgetting to declare variables: Always declare your variables with the appropriate data type.
  • Incorrect format specifiers: Ensure that the format specifiers in the FORMAT statement match the data types of the variables.
  • Using uninitialized variables: Make sure to initialize your variables before using them in I/O operations.

Conclusion

In this section, we covered the basics of input and output operations in Fortran using the READ and WRITE statements. We also explored how to control the layout of the input and output using the FORMAT statement. These fundamental skills are essential for interacting with users and handling data in your Fortran programs. In the next section, we will delve into control structures, starting with If statements.

© Copyright 2024. All rights reserved