In this section, we will explore the concept of loops in Fortran. Loops are fundamental control structures that allow you to execute a block of code multiple times. Fortran provides several types of loops, each suited for different scenarios.
Types of Loops in Fortran
- DO Loop: The most common loop in Fortran, used for a fixed number of iterations.
- DO WHILE Loop: Executes as long as a specified condition is true.
- INFINITE Loop: A loop that runs indefinitely until explicitly terminated.
DO Loop
The DO
loop is used when you know in advance how many times you want to execute a block of code. The syntax is as follows:
variable
: The loop control variable.start
: The initial value of the loop control variable.end
: The final value of the loop control variable.step
: The increment (or decrement) for each iteration (optional, default is 1).
Example
PROGRAM DoLoopExample INTEGER :: i DO i = 1, 10 PRINT *, "Iteration: ", i END DO END PROGRAM DoLoopExample
Explanation: This program prints the numbers from 1 to 10. The loop control variable i
starts at 1 and increments by 1 until it reaches 10.
DO WHILE Loop
The DO WHILE
loop continues to execute as long as a specified condition is true. The syntax is:
condition
: A logical expression that is evaluated before each iteration.
Example
PROGRAM DoWhileExample INTEGER :: i i = 1 DO WHILE (i <= 10) PRINT *, "Iteration: ", i i = i + 1 END DO END PROGRAM DoWhileExample
Explanation: This program also prints the numbers from 1 to 10. The loop continues as long as i
is less than or equal to 10.
INFINITE Loop
An infinite loop runs indefinitely until it is explicitly terminated using an EXIT
statement. The syntax is:
condition
: A logical expression that, when true, causes the loop to terminate.
Example
PROGRAM InfiniteLoopExample INTEGER :: i i = 1 DO PRINT *, "Iteration: ", i i = i + 1 IF (i > 10) EXIT END DO END PROGRAM InfiniteLoopExample
Explanation: This program prints the numbers from 1 to 10. The loop runs indefinitely but exits when i
becomes greater than 10.
Practical Exercises
Exercise 1: Sum of First N Natural Numbers
Write a Fortran program to calculate the sum of the first N natural numbers using a DO
loop.
Solution
PROGRAM SumOfNaturalNumbers INTEGER :: i, N, sum sum = 0 PRINT *, "Enter the value of N: " READ *, N DO i = 1, N sum = sum + i END DO PRINT *, "The sum of the first ", N, " natural numbers is: ", sum END PROGRAM SumOfNaturalNumbers
Exercise 2: Factorial of a Number
Write a Fortran program to calculate the factorial of a given number using a DO WHILE
loop.
Solution
PROGRAM Factorial INTEGER :: i, N, factorial factorial = 1 PRINT *, "Enter a number: " READ *, N i = 1 DO WHILE (i <= N) factorial = factorial * i i = i + 1 END DO PRINT *, "The factorial of ", N, " is: ", factorial END PROGRAM Factorial
Exercise 3: Infinite Loop with User Input
Write a Fortran program that continuously asks the user to enter a number and prints it. The loop should terminate when the user enters a negative number.
Solution
PROGRAM InfiniteLoopWithExit INTEGER :: number DO PRINT *, "Enter a number (negative to exit): " READ *, number IF (number < 0) EXIT PRINT *, "You entered: ", number END DO PRINT *, "Program terminated." END PROGRAM InfiniteLoopWithExit
Common Mistakes and Tips
- Off-by-One Errors: Ensure that your loop boundaries are correctly set to avoid iterating one time too many or too few.
- Infinite Loops: Be cautious with conditions in
DO WHILE
loops to avoid creating unintended infinite loops. - Variable Initialization: Always initialize your loop control variables to avoid unexpected behavior.
Conclusion
In this section, we covered the different types of loops in Fortran, including DO
, DO WHILE
, and infinite loops. We also provided practical examples and exercises to help you understand how to use loops effectively. Mastering loops is essential for writing efficient and effective Fortran programs. In the next section, we will delve into arrays and strings, which are crucial for handling collections of data.
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