Simple do-loop in Fortran. Please help

  • Fortran
  • Thread starter onlybarca6
  • Start date
  • Tags
    Fortran
In summary, the conversation discusses how to create a do-loop in Fortran using variables to print out 10 fields with specific values. The solution provided involves using a do-loop and updating the value with each iteration.
  • #1
onlybarca6
5
0
I can't figure out how to make a do-loop in Fortran to do the following:

I have 2 variables. Let's say:

REAL :: level, step

level = 2429.8
step = 1159.8

and I need 10 fields printed out.

The first field will have the value:

level + step
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>
<then the value of the one above + step>

So I'm sure it's simple but I can't figure out how to use the values of variables in do-loops instead of typing the numbers in.

Thanks in advance
 
Technology news on Phys.org
  • #2
I'm a little rusty with Fortran, but this should do what you're asking.
Code:
value = level + step
do i = 1, 10
  write *,* value
  value = value + step
end do
 
  • #3
Thanks so much! It works.
 

1. What is a "do-loop" in Fortran?

A "do-loop" in Fortran is a type of control structure that allows a set of statements to be repeatedly executed based on a specified condition or range of values. It is similar to a "for loop" in other programming languages.

2. How do you write a simple do-loop in Fortran?

To write a simple do-loop in Fortran, you need to specify the loop control variable, the starting and ending values, and the increment or decrement value. For example:
do i = 1, 10, 2
    print*, i
enddo
This loop will start at 1, increment by 2, and end at 10, printing out the value of i each time.

3. Can you have multiple do-loops in a Fortran program?

Yes, you can have multiple do-loops in a Fortran program. Each loop must have a unique loop control variable and can be nested within each other.

4. What is the difference between a do-loop and a do-while loop in Fortran?

The main difference between a do-loop and a do-while loop in Fortran is that in a do-loop, the loop control variable is incremented or decremented each time the loop is executed, while in a do-while loop, the loop continues as long as a specified condition is true.

5. How do you exit a do-loop in Fortran?

To exit a do-loop in Fortran, you can use the "exit" statement. This statement will immediately terminate the loop and the program will continue with the statement following the enddo statement.

Similar threads

  • Programming and Computer Science
Replies
3
Views
659
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
19
Views
909
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
840
  • Programming and Computer Science
Replies
4
Views
463
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top