Operators and Expressions in Fortran

In programming, operators and expressions form the foundation of computations. They allow us to manipulate data, make decisions, and control the flow of a program. Fortran, being one of the oldest high-level programming languages, provides a robust set of operators and expression handling mechanisms. This post will explore these in detail with examples and explanations.

1. Introduction to Operators

An operator is a symbol that tells the compiler to perform specific mathematical, relational, or logical operations. Operators work on variables and constants called operands. In Fortran, operators are divided into several categories, including arithmetic, relational, and logical operators. Expressions are combinations of operators and operands that evaluate to a value.

Understanding operators is crucial because they allow programmers to implement logic, perform calculations, and control program behavior.

2. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and exponentiation. Fortran provides the following arithmetic operators:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • ** : Exponentiation (power)

2.1 Addition (+)

The addition operator + is used to add two numbers. For example:

program addition_example
  integer :: a, b, sum
  a = 10
  b = 5
  sum = a + b
  print *, "Sum:", sum
end program addition_example

Output:

Sum: 15

2.2 Subtraction (-)

The subtraction operator - subtracts one number from another:

program subtraction_example
  integer :: a, b, difference
  a = 10
  b = 4
  difference = a - b
  print *, "Difference:", difference
end program subtraction_example

Output:

Difference: 6

2.3 Multiplication (*)

Multiplication is done using the * operator:

program multiplication_example
  integer :: a, b, product
  a = 6
  b = 7
  product = a * b
  print *, "Product:", product
end program multiplication_example

Output:

Product: 42

2.4 Division (/)

Division is represented by /. It is important to note that integer division truncates the decimal part:

program division_example
  integer :: a, b, quotient
  a = 10
  b = 3
  quotient = a / b
  print *, "Quotient:", quotient
end program division_example

Output:

Quotient: 3

For floating-point division, use real variables:

program real_division
  real :: x, y, quotient
  x = 10.0
  y = 3.0
  quotient = x / y
  print *, "Quotient:", quotient
end program real_division

Output:

Quotient: 3.333333

2.5 Exponentiation (**)

Exponentiation is performed using the ** operator:

program exponentiation_example
  integer :: base, exponent, result
  base = 2
  exponent = 5
  result = base ** exponent
  print *, "Result:", result
end program exponentiation_example

Output:

Result: 32

3. Relational Operators

Relational operators are used to compare two values. The result of a relational operation is a logical value, either .true. or .false.. These operators help in decision-making processes and are commonly used in conditional statements such as if statements.

Fortran provides the following relational operators:

  • == : Equal to
  • /= : Not equal to
  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to

3.1 Equal to (==)

Checks if two values are equal:

program equal_example
  integer :: a, b
  a = 5
  b = 5
  print *, "Is a equal to b?", a == b
end program equal_example

Output:

Is a equal to b? T

3.2 Not equal to (/=)

Checks if two values are not equal:

program not_equal_example
  integer :: a, b
  a = 5
  b = 3
  print *, "Is a not equal to b?", a /= b
end program not_equal_example

Output:

Is a not equal to b? T

3.3 Less than (<)

Checks if the first value is smaller than the second:

program less_than_example
  integer :: a, b
  a = 3
  b = 7
  print *, "Is a less than b?", a < b
end program less_than_example

Output:

Is a less than b? T

3.4 Greater than (>)

Checks if the first value is greater than the second:

program greater_than_example
  integer :: a, b
  a = 10
  b = 4
  print *, "Is a greater than b?", a > b
end program greater_than_example

Output:

Is a greater than b? T

3.5 Less than or equal to (<=)

Checks if the first value is smaller than or equal to the second:

program less_equal_example
  integer :: a, b
  a = 5
  b = 5
  print *, "Is a less than or equal to b?", a <= b
end program less_equal_example

Output:

Is a less than or equal to b? T

3.6 Greater than or equal to (>=)

Checks if the first value is greater than or equal to the second:

program greater_equal_example
  integer :: a, b
  a = 7
  b = 5
  print *, "Is a greater than or equal to b?", a >= b
end program greater_equal_example

Output:

Is a greater than or equal to b? T

4. Logical Operators

Logical operators are used to perform operations on logical values .true. or .false.. These are commonly used in decision-making structures like if, do while, or loops. The primary logical operators in Fortran are:

  • .and. : Logical AND
  • .or. : Logical OR
  • .not. : Logical NOT

4.1 Logical AND (.and.)

Returns .true. if both operands are .true.:

program logical_and_example
  logical :: a, b
  a = .true.
  b = .false.
  print *, "a .and. b =", a .and. b
end program logical_and_example

Output:

a .and. b = F

4.2 Logical OR (.or.)

Returns .true. if at least one operand is .true.:

program logical_or_example
  logical :: a, b
  a = .true.
  b = .false.
  print *, "a .or. b =", a .or. b
end program logical_or_example

Output:

a .or. b = T

4.3 Logical NOT (.not.)

Negates a logical value:

program logical_not_example
  logical :: a
  a = .true.
  print *, ".not. a =", .not. a
end program logical_not_example

Output:

.not. a = F

5. Combining Operators in Expressions

Expressions in Fortran can combine multiple operators. The order of evaluation follows standard precedence rules:

  1. Parentheses () have the highest precedence.
  2. Exponentiation **
  3. Multiplication * and Division /
  4. Addition + and Subtraction -
  5. Relational operators (==, /=, <, >, <=, >=)
  6. Logical operators (.not., .and., .or.)

5.1 Example of Combined Expression

program combined_expression
  integer :: a, b, c, result
  logical :: check
  a = 5
  b = 3
  c = 2
  result = a + b * c
  check = (a + b) > c .and. result == 11
  print *, "Result:", result
  print *, "Check:", check
end program combined_expression

Output:

Result: 11
Check: T

In this example, multiplication occurs before addition, and the logical expression combines relational and logical operators.

6. Practical Applications

Operators and expressions are used everywhere in Fortran programs:

  1. Arithmetic calculations: Scientific computations, finance, engineering.
  2. Decision-making: Using relational and logical operators in if or select case statements.
  3. Loops and conditions: Controlling do loops or terminating conditions.
  4. Complex expressions: Combining arithmetic, relational, and logical operators to create meaningful program logic.

6.1 Example: Conditional Statement

program conditional_example
  integer :: marks
  marks = 85
  if (marks >= 90) then
print *, "Grade: A"
else if (marks >= 75) then
print *, "Grade: B"
else
print *, "Grade: C"
end if end program conditional_example

6.2 Example: Loop with Expressions

program loop_example
  integer :: i, sum
  sum = 0
  do i = 1, 5
sum = sum + i**2
end do print *, "Sum of squares from 1 to 5:", sum end program loop_example

Output:

Sum of squares from 1 to 5: 55

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *