In this section, we will cover the fundamental concepts of variables and data types in Fortran. Understanding these concepts is crucial as they form the building blocks for any Fortran program.

Key Concepts

  1. Variables: Named storage locations in memory that hold data.
  2. Data Types: Define the type of data a variable can hold, such as integers, real numbers, characters, etc.
  3. Declaration: The process of defining a variable's name and data type.
  4. Initialization: Assigning an initial value to a variable.

Variable Declaration and Initialization

In Fortran, variables must be declared before they are used. The declaration specifies the variable's name and data type. Optionally, variables can be initialized at the time of declaration.

Syntax

type :: variable_name
type :: variable_name = initial_value

Example

integer :: age
real :: height = 1.75
character(len=20) :: name = 'John Doe'

Explanation

  • integer :: age declares an integer variable named age.
  • real :: height = 1.75 declares a real (floating-point) variable named height and initializes it to 1.75.
  • character(len=20) :: name = 'John Doe' declares a character variable named name with a length of 20 characters and initializes it to 'John Doe'.

Data Types

Fortran supports several basic data types:

  1. Integer: Used for whole numbers.
  2. Real: Used for floating-point numbers.
  3. Double Precision: Used for double-precision floating-point numbers.
  4. Complex: Used for complex numbers.
  5. Character: Used for strings of characters.
  6. Logical: Used for boolean values (true/false).

Integer

integer :: count = 10

Real

real :: temperature = 36.6

Double Precision

double precision :: large_number = 1.0d+300

Complex

complex :: z = (3.0, 4.0)

Character

character(len=10) :: greeting = 'Hello'

Logical

logical :: is_valid = .true.

Practical Examples

Example 1: Basic Variable Declaration and Initialization

program basic_variables
    implicit none
    integer :: age = 25
    real :: salary = 50000.0
    character(len=15) :: name = 'Alice'
    logical :: is_employed = .true.

    print *, 'Name:', name
    print *, 'Age:', age
    print *, 'Salary:', salary
    print *, 'Employed:', is_employed
end program basic_variables

Explanation

  • The program declares and initializes variables of different types.
  • The print * statements output the values of the variables.

Example 2: Using Variables in Expressions

program variable_expressions
    implicit none
    integer :: a = 5, b = 10, sum
    real :: x = 2.5, y = 4.0, product

    sum = a + b
    product = x * y

    print *, 'Sum of a and b:', sum
    print *, 'Product of x and y:', product
end program variable_expressions

Explanation

  • The program performs arithmetic operations using variables.
  • The results are stored in new variables and printed.

Exercises

Exercise 1: Declare and Initialize Variables

  1. Declare an integer variable num_students and initialize it to 30.
  2. Declare a real variable average_score and initialize it to 85.5.
  3. Declare a character variable course_name with a length of 25 and initialize it to 'Fortran Programming'.
  4. Declare a logical variable is_course_active and initialize it to .true..

Solution

program exercise1
    implicit none
    integer :: num_students = 30
    real :: average_score = 85.5
    character(len=25) :: course_name = 'Fortran Programming'
    logical :: is_course_active = .true.

    print *, 'Number of Students:', num_students
    print *, 'Average Score:', average_score
    print *, 'Course Name:', course_name
    print *, 'Is Course Active:', is_course_active
end program exercise1

Exercise 2: Perform Arithmetic Operations

  1. Declare two integer variables a and b and initialize them to 15 and 25, respectively.
  2. Calculate their sum and store it in a variable sum.
  3. Declare two real variables p and q and initialize them to 3.5 and 2.0, respectively.
  4. Calculate their product and store it in a variable product.

Solution

program exercise2
    implicit none
    integer :: a = 15, b = 25, sum
    real :: p = 3.5, q = 2.0, product

    sum = a + b
    product = p * q

    print *, 'Sum of a and b:', sum
    print *, 'Product of p and q:', product
end program exercise2

Common Mistakes and Tips

  • Uninitialized Variables: Always initialize variables to avoid undefined behavior.
  • Type Mismatch: Ensure that operations are performed between compatible data types.
  • Implicit Typing: Use implicit none to avoid implicit typing and catch undeclared variables.

Conclusion

In this section, we covered the basics of variables and data types in Fortran. We learned how to declare and initialize variables, the different data types available, and how to use variables in expressions. These concepts are fundamental and will be used throughout the course. In the next section, we will explore operators and expressions in more detail.

© Copyright 2024. All rights reserved