Conditions

Contents
if then else
SELECT CASE
Nesting vs guards
Logical expressions

if then else

An if block runs one branch when the test is true and another branch when it is false.

Each block closes with end if, and else if adds further tests in the middle without extra nesting.

program ifdemo implicit none integer :: n n = 7 if (n > 10) then print *, "large" else if (n > 5) then print *, "medium" else print *, "small" end if end program ifdemo

SELECT CASE

SELECT CASE compares one value against a list of clear alternatives, which reads better than long else if chains.

Each case lists values or ranges, and case default catches everything else.

program casedemo implicit none integer :: day day = 3 select case (day) case (1) print *, "Monday" case (2, 3, 4) print *, "midweek" case default print *, "other day" end select end program casedemo

Nesting vs guards

Deep nesting with if inside if is hard to read, so test bad input first and leave the main path flat.

Each guard handles one simple case and stops that branch, which keeps the happy path at the lowest indent level.

program guarddemo implicit none integer :: n n = -2 if (n < 0) then print *, "negative, stop" else print *, "square =", n * n end if end program guarddemo

Logical expressions

Combine comparisons with .and., .or., and .not. to describe the exact condition in one place.

Parentheses make the intent clear even when the default priority would give the same answer.

program condemo implicit none integer :: age age = 20 if ((age >= 18) .and. (age < 65)) then print *, "working age" end if end program condemo

Next: DO loops

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: