In this section, we will explore the various operators available in Fortran and how to use them to create expressions. Understanding operators and expressions is fundamental to performing calculations and making decisions in your programs.
- Arithmetic Operators
Fortran supports the following arithmetic operators:
| Operator | Description | Example |
|---|---|---|
+ |
Addition | A + B |
- |
Subtraction | A - B |
* |
Multiplication | A * B |
/ |
Division | A / B |
** |
Exponentiation | A ** B |
Example:
PROGRAM ArithmeticExample
IMPLICIT NONE
INTEGER :: a, b, result
REAL :: x, y, result_real
! Assign values
a = 10
b = 3
x = 2.5
y = 4.0
! Perform arithmetic operations
result = a + b
PRINT *, 'Addition: ', result
result = a - b
PRINT *, 'Subtraction: ', result
result = a * b
PRINT *, 'Multiplication: ', result
result_real = x / y
PRINT *, 'Division: ', result_real
result_real = x ** y
PRINT *, 'Exponentiation: ', result_real
END PROGRAM ArithmeticExample
- Relational Operators
Relational operators are used to compare two values. The result of a relational operation is a logical value (.TRUE. or .FALSE.).
| Operator | Description | Example |
|---|---|---|
== |
Equal to | A == B |
/= |
Not equal to | A /= B |
> |
Greater than | A > B |
< |
Less than | A < B |
>= |
Greater than or equal to | A >= B |
<= |
Less than or equal to | A <= B |
Example:
PROGRAM RelationalExample
IMPLICIT NONE
INTEGER :: a, b
LOGICAL :: result
! Assign values
a = 10
b = 3
! Perform relational operations
result = a == b
PRINT *, 'Equal to: ', result
result = a /= b
PRINT *, 'Not equal to: ', result
result = a > b
PRINT *, 'Greater than: ', result
result = a < b
PRINT *, 'Less than: ', result
result = a >= b
PRINT *, 'Greater than or equal to: ', result
result = a <= b
PRINT *, 'Less than or equal to: ', result
END PROGRAM RelationalExample
- Logical Operators
Logical operators are used to combine logical expressions.
| Operator | Description | Example |
|---|---|---|
.AND. |
Logical AND | A .AND. B |
.OR. |
Logical OR | A .OR. B |
.NOT. |
Logical NOT | .NOT. A |
Example:
PROGRAM LogicalExample
IMPLICIT NONE
LOGICAL :: a, b, result
! Assign values
a = .TRUE.
b = .FALSE.
! Perform logical operations
result = a .AND. b
PRINT *, 'Logical AND: ', result
result = a .OR. b
PRINT *, 'Logical OR: ', result
result = .NOT. a
PRINT *, 'Logical NOT: ', result
END PROGRAM LogicalExample
- Assignment Operators
The assignment operator (=) is used to assign a value to a variable.
Example:
PROGRAM AssignmentExample
IMPLICIT NONE
INTEGER :: a, b
! Assign values
a = 10
b = 3
PRINT *, 'Value of a: ', a
PRINT *, 'Value of b: ', b
END PROGRAM AssignmentExample
- Combining Operators in Expressions
You can combine multiple operators in a single expression. Fortran follows the standard order of operations (PEMDAS/BODMAS).
Example:
PROGRAM CombinedExample
IMPLICIT NONE
INTEGER :: a, b, c, result
! Assign values
a = 10
b = 3
c = 5
! Combine operators in an expression
result = a + b * c - (a / b)
PRINT *, 'Combined expression result: ', result
END PROGRAM CombinedExamplePractical Exercises
Exercise 1: Basic Arithmetic Operations
Write a Fortran program that takes two integers as input and performs addition, subtraction, multiplication, and division. Print the results.
Solution:
PROGRAM BasicArithmetic
IMPLICIT NONE
INTEGER :: a, b, sum, difference, product
REAL :: quotient
! Input values
PRINT *, 'Enter two integers:'
READ *, a, b
! Perform arithmetic operations
sum = a + b
difference = a - b
product = a * b
quotient = REAL(a) / REAL(b)
! Print results
PRINT *, 'Sum: ', sum
PRINT *, 'Difference: ', difference
PRINT *, 'Product: ', product
PRINT *, 'Quotient: ', quotient
END PROGRAM BasicArithmeticExercise 2: Relational and Logical Operations
Write a Fortran program that takes two integers as input and checks if the first integer is greater than the second. Also, check if both integers are positive using logical operators.
Solution:
PROGRAM RelationalLogical
IMPLICIT NONE
INTEGER :: a, b
LOGICAL :: isGreater, bothPositive
! Input values
PRINT *, 'Enter two integers:'
READ *, a, b
! Perform relational and logical operations
isGreater = a > b
bothPositive = (a > 0) .AND. (b > 0)
! Print results
PRINT *, 'Is the first integer greater than the second? ', isGreater
PRINT *, 'Are both integers positive? ', bothPositive
END PROGRAM RelationalLogicalConclusion
In this section, we covered the various types of operators in Fortran, including arithmetic, relational, logical, and assignment operators. We also learned how to combine these operators in expressions and practiced with some exercises. Understanding these operators and how to use them in expressions is crucial for performing calculations and making decisions in your Fortran programs. In the next section, we will delve into input and output operations in Fortran.
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
