Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Questions about do loop

Status
Not open for further replies.

FortranUser

Technical User
Feb 20, 2010
3
US
Hi, everyone;
I am trying to test a part of a program, and the result I get from a do loop really confuse me. For do loop3, if I add a write statement at line 29, when I run the program, the do loop will never end, and from the value returned, it seems that the program has never updated the value of p. If I remove this statement, the program will compile successfully, but this time it seems that both loop2 and loop3 has never been run. Can you tell me what has caused this please? Thank you very much.

Here is the code, I am very sorry I cann't upload files
!--------------------------------------------------------
program Testing
implicit none
integer :: maximum, temp, x=2**5 - 1, c, p, a
!--------------------------------------------------------
do while (x > 0)
write (*,*)x
maximum = x
x = (x+1)**2 - 1
end do
x = maximum
write(*,*)".......", x
!---------------------------------------------------------
loop1: do
loop2: do
c = 2
temp = mod(x,c)
if (temp /= 0) exit loop2
c = c + 1
end do loop2
write (*,*)"c is", c
write (*,*)"temp is", temp

loop3: do
p = 2
temp = mod(x,p)
write (*,*)"temp is:", temp !line 29
if (temp == 0) exit loop3
p = p + 1
end do loop3
write(*,*) "p is", p

temp = mod(x,4)
if (temp == 0) then
a = 8*p + 1
else
a = 2*p + 1
end if
write (*,*) "a is", a

if (a <=0) then
x = x - 1
end if
if (a> 0) exit

end do loop1
write(*,*)"The maximum value of x is", x

stop
end program Testing
 
The problem is this
Code:
  loop3: do
    [COLOR=red]p = 2[/color]
    temp = mod(x,p)
    write (*,*)"temp is:", temp !line 29
    if (temp == 0) exit loop3
    p = p + 1
  end do loop3
  write(*,*) "p is", p
In your loop, at the beginning you set p=2, then at the end you increase it p = p+1 = 3.
But in the next iteartion you reset it everytime at p = 2. So your variable temp will be computete everytime with temp = mod(x, 2)
 
Thank you for your reply, I think that's not the cause. Since loop3 will change the value of p, and in this case, because x is the largest number the computer can handle and is a prime number, so that at the end
p = x
and when p is used to calculate the value of a, because 2*p is larger than what the computer can handle, so that a negative value for a is returned
This means that the value of x should have been changed according to
if (a < 0) then
x = x - 1
 
If you believe or not it's the cause. I hope this explanation helps you to understand why it's so:

Your x is first initialized with 2**5-1 = 31
Then the program computes in the while loop the value of x and prints out
Code:
 31
 1023
 1048575
 ....... 1048575
The loop1 will be exited after first run because with c=2, you get temp = mod(1048575,2)=1, so the exit condition (temp <> 0) is fulfilled. The output is
Code:
 c is 2
 temp is 1
Then in the loop3 with p=2 you get again temp=mod(1048575,2)=1. The program prints
Code:
 temp is: 1
The exit condition (temp==0) is not fulfilled so the program computes p=p+1=3, but after that the loop goes in the next iteration and overwrites with p=2 the value of the variable incremented before (i.e. p=3).
This causes that the exact the same value of temp is computed (temp=mod(1048575,2)=1) as in the iteration before and according to the exit condition the loop will be never terminated and runs forever.
So the output will be forever:
Code:
 temp is: 1
 temp is: 1
 temp is: 1
 ....
 ....
 temp is: 1
 temp is: 1
 temp is: 1
Although I don't know what the program should do, IMHO it doesn't have sence to set p=2 and c= 2 inside of the loops , but you have to set it before the loops.
When I corrected it, then the program runs without infinite loops and outputs this:
Code:
$ testing2
 31
 1023
 1048575
 ....... 1048575
 c is 2
 temp is 1
 temp is: 1
 temp is: 0
 p is 3
 a is 7
 The maximum value of x is 1048575

Here is the source I corrected:
Code:
[COLOR=#0000ff]!--------------------------------------------------------[/color]
[COLOR=#a020f0]program[/color] Testing
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: maximum, temp, x[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]2[/color][COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]5[/color] [COLOR=#804040][b]-[/b][/color] [COLOR=#ff00ff]1[/color], c, p, a
[COLOR=#0000ff]!--------------------------------------------------------[/color]
[COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color] (x [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color])
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])x
  maximum [COLOR=#804040][b]=[/b][/color] x
  x [COLOR=#804040][b]=[/b][/color] (x[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]2[/color] [COLOR=#804040][b]-[/b][/color] [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]end do[/b][/color]
x [COLOR=#804040][b]=[/b][/color] maximum
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]"......."[/color], x
[COLOR=#0000ff]!---------------------------------------------------------[/color]
[COLOR=#008080]loop1[/color]: [COLOR=#804040][b]do[/b][/color]
  c [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color]
  [COLOR=#008080]loop2[/color]: [COLOR=#804040][b]do[/b][/color]
    temp [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]mod[/color](x,c)
    [COLOR=#804040][b]if[/b][/color] (temp [COLOR=#804040][b]/=[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]exit[/b][/color] loop2
    c [COLOR=#804040][b]=[/b][/color] c [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color] loop2
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]"c is"[/color], c 
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]"temp is"[/color], temp

  p [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color]
  [COLOR=#008080]loop3[/color]: [COLOR=#804040][b]do[/b][/color]
    temp [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]mod[/color](x,p)
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]"temp is:"[/color], temp [COLOR=#0000ff]!line 29[/color]
    [COLOR=#804040][b]if[/b][/color] (temp [COLOR=#804040][b]==[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]exit[/b][/color] loop3
    p [COLOR=#804040][b]=[/b][/color] p [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color] loop3
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"p is"[/color], p

  temp [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]mod[/color](x,[COLOR=#ff00ff]4[/color])
  [COLOR=#804040][b]if[/b][/color] (temp [COLOR=#804040][b]==[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    a [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]8[/color][COLOR=#804040][b]*[/b][/color]p [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]else[/b][/color]
    a [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b]*[/b][/color]p [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"a is"[/color], a

  [COLOR=#804040][b]if[/b][/color] (a [COLOR=#804040][b]<=[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    x [COLOR=#804040][b]=[/b][/color] x [COLOR=#804040][b]-[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]if[/b][/color] (a[COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]exit[/b][/color]

[COLOR=#804040][b]end do[/b][/color] loop1
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]"The maximum value of x is"[/color], x

[COLOR=#804040][b]stop[/b][/color]
[COLOR=#a020f0]end program[/color] Testing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top