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
- READ Statement: Used to read input from the user or a file.
- WRITE Statement: Used to write output to the screen or a file.
- 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:
- 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 variableage
.PRINT *, 'Enter your height in meters:'
displays another prompt.READ(*, *) height
reads a real number from the user and stores it in the variableheight
.- 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:
- 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 variableage
.height = 1.75
assigns the value 1.75 to the variableheight
.WRITE(*, *) 'Age:', age
writes the string 'Age:' followed by the value ofage
to the standard output.WRITE(*, *) 'Height:', height
writes the string 'Height:' followed by the value ofheight
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 integerage
in a field of width 3.WRITE(*, '(A, F5.2)') 'Height:', height
writes the string 'Height:' followed by the real numberheight
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.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability