In this section, we will cover the fundamental syntax and structure of Fortran programs. Understanding these basics is crucial for writing and reading Fortran code effectively.
Key Concepts
- Program Structure
- Comments
- Identifiers
- Data Types
- Constants and Variables
- Basic Input/Output
- Program Structure
A Fortran program typically consists of the following parts:
- Program name
- Declarations
- Executable statements
- End statement
Example:
Explanation:
program HelloWorld
: Declares the start of the program namedHelloWorld
.implicit none
: Ensures all variables must be explicitly declared.print *, "Hello, World!"
: Prints the string "Hello, World!" to the console.end program HelloWorld
: Marks the end of the program.
- Comments
Comments are used to explain code and are ignored by the compiler. In Fortran, comments start with an exclamation mark (!
).
Example:
program CommentExample implicit none ! This is a comment print *, "Comments are ignored by the compiler" end program CommentExample
- Identifiers
Identifiers are names given to various program elements such as variables, constants, and functions. They must start with a letter and can be followed by letters, digits, and underscores.
Example:
program IdentifiersExample implicit none integer :: myVariable myVariable = 10 print *, myVariable end program IdentifiersExample
- Data Types
Fortran supports several data types, including:
- Integer
- Real
- Complex
- Logical
- Character
Example:
program DataTypesExample implicit none integer :: i real :: x complex :: z logical :: flag character(len=20) :: name i = 10 x = 3.14 z = (1.0, 2.0) flag = .true. name = "Fortran" print *, i, x, z, flag, name end program DataTypesExample
- Constants and Variables
Constants are fixed values, while variables can change during program execution. Constants are declared using the parameter
attribute.
Example:
program ConstantsVariablesExample implicit none integer, parameter :: max_value = 100 real :: radius, area radius = 5.0 area = 3.14 * radius**2 print *, "Max Value:", max_value print *, "Area:", area end program ConstantsVariablesExample
- Basic Input/Output
Fortran uses print
for output and read
for input.
Example:
program BasicIOExample implicit none integer :: num print *, "Enter a number:" read *, num print *, "You entered:", num end program BasicIOExample
Practical Exercises
Exercise 1: Simple Arithmetic
Write a Fortran program that reads two integers from the user, adds them, and prints the result.
Solution:
program AddTwoNumbers implicit none integer :: num1, num2, sum print *, "Enter first number:" read *, num1 print *, "Enter second number:" read *, num2 sum = num1 + num2 print *, "The sum is:", sum end program AddTwoNumbers
Exercise 2: Circle Area Calculation
Write a Fortran program that reads the radius of a circle from the user and calculates the area.
Solution:
program CircleArea implicit none real :: radius, area real, parameter :: pi = 3.14159 print *, "Enter the radius of the circle:" read *, radius area = pi * radius**2 print *, "The area of the circle is:", area end program CircleArea
Common Mistakes and Tips
- Forgetting
implicit none
: Always includeimplicit none
to avoid undeclared variables. - Incorrect variable names: Ensure variable names start with a letter and contain no spaces or special characters.
- Mismatched data types: Be careful with data types, especially when performing operations.
Conclusion
In this section, we covered the basic syntax and structure of Fortran programs, including program structure, comments, identifiers, data types, constants, variables, and basic input/output. These fundamentals are essential for writing and understanding Fortran code. In the next section, we will delve deeper into variables and data types.
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