FortranUser
Technical User
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
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