What Is Logical Data Type in Fortran?

//

Scott Campbell

In Fortran, a logical data type is used to represent logical values, which are either true or false. The logical data type is often used in conditional statements and logical operations to control the flow of a program.

Declaring Logical Variables

To declare a variable with the logical data type in Fortran, you need to specify its name and the keyword “logical”. For example:

logical :: isTrue

This declares a variable named “isTrue” of type logical. By default, this variable will be initialized to false.

Assigning Logical Values

You can assign a value to a logical variable using the assignment operator (=). The values true and false are represented by the keywords .true.

and .false., respectively. For example:

isTrue = .

This assigns the value true to the variable “isTrue”. Similarly, you can assign false as follows:

isTrue = .

Logical Expressions

Logical expressions are used to perform comparisons or combine logical values using operators such as AND, OR, and NOT. These expressions evaluate to either true or false.

Comparison Operators

  • .eqv.: Tests if two values are equivalent.
  • .neqv.: Tests if two values are not equivalent.gt.: Tests if one value is greater than another.lt.: Tests if one value is less than another.ge.: Tests if one value is greater than or equal to another.le.: Tests if one value is less than or equal to another.

Logical Operators

  • .and.: Performs a logical AND operation on two values.or.: Performs a logical OR operation on two values.not.: Performs a logical NOT operation on a value.

Control Structures

The logical data type is commonly used in control structures such as conditional statements and loops to control the flow of a program based on certain conditions. For example, the IF statement:

if (isTrue) then
    ! Code block executed when isTrue is true
else
    ! Code block executed when isTrue is false
end if

In this example, the code block following the “then” keyword will be executed if “isTrue” evaluates to true. Otherwise, the code block following the “else” keyword will be executed. The “end if” statement marks the end of the IF statement block.

Conclusion

The logical data type in Fortran allows you to work with boolean values, making it easier to implement conditional statements and control structures in your programs. By understanding how to declare logical variables, assign logical values, and use logical expressions and control structures, you can effectively manipulate boolean data in your Fortran programs.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy