Printing certain values from a DO loop in Fortran

In summary, to print specific values from a DO loop in Fortran, use the PRINT or WRITE statement within the loop. You can also use the FORMAT statement to specify the desired format for the printed values. To print values to a file, use the OPEN statement to open a file for writing and then use the PRINT or WRITE statement to write the values to the file. The difference between PRINT and WRITE statements is that PRINT prints to the screen while WRITE allows you to specify a file. To print values conditionally, use an IF statement inside the DO loop to specify a condition for printing.
  • #1
johnwalton84
16
0
I've written a program in Fortran which has a do loop of the form

do i=0,20000

(operations)

end do


I want the program to print the values of the do loop, but only want it to print every 100th value of i (i.e. 100,200,300,400...20000), can anyone suggest a way to do this?
 
Computer science news on Phys.org
  • #2
if (mod(i,100) .eq. 0) then
print stuff
endif
 
  • #3
Would that not only work for i=100 though? If i is 200 then the mod(200,100) wouldn't be 0 so it wouldn't print?
 
  • #4
Obviously not cos its worked :smile:

Thanks for that imabug!
 

1. How do I print specific values from a DO loop in Fortran?

To print specific values from a DO loop in Fortran, you can use the PRINT or WRITE statement inside the DO loop, specifying the desired variable or array element.

2. Can I print values from a DO loop in a specific format?

Yes, you can use the FORMAT statement in conjunction with the PRINT or WRITE statement to specify the desired format for the printed values.

3. How can I print values from a DO loop to a file?

You can use the OPEN statement to open a file for writing, and then use the PRINT or WRITE statement to write the values to the file instead of the screen.

4. What is the difference between PRINT and WRITE statements in Fortran?

The PRINT statement prints values to the screen, while the WRITE statement allows you to specify a file to write the values to.

5. Can I print values from a DO loop conditionally?

Yes, you can use an IF statement inside the DO loop to specify a condition for printing the values. Only values that meet the specified condition will be printed.

Similar threads

  • Computing and Technology
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
983
  • Computing and Technology
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Computing and Technology
Replies
9
Views
1K
  • Computing and Technology
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
636
  • Programming and Computer Science
Replies
14
Views
3K
Back
Top