What does this Fortran code mean:
IF (J1-3) 20, 20, 21
21 J1 = J1 - 3
20 IF (J2-3) 22, 22, 23
23 J2 = J2 - 3
22 CONTINUE
I've seen in old project and I don't have any idea what this IF with numbers (labels) means.
This is an arithmetic if statement from FORTRAN 77. Adapted from the FORTRAN 77 specification (emphasis mine):
The form of an arithmetic IF statement is:
IF (e) s1 , s2 , s2
where: e is an integer, real, or double precision expression
s1, s2, and s3 are each the statement label of an executable statement that appears in the same program unit as the arithmetic IF statement. The same statement label may appear more than once in the same arithmetic IF statement.
Execution of an arithmetic IF statement causes evaluation of the expression e followed by a transfer of control. The statement identified by s1, s2, or s3 is executed next as the value of e is less than zero, equal to zero, or greater than zero, respectively.
For the example in your question, from the last sentence above,
If J1-3 < 0 statement 20 will be executed
If J1-3 = 0 statement 20 will also be executed
If J1-3 > 0 statement 21 will be executed
Edit: A modern and much more readable way to write this would be:
if (J1-3 > 0) J1 = J1 - 3
if (J2-3 > 0) J2 = J2 - 3
Related
This question already has answers here:
Why is the Fortran DO loop index larger than the upper bound after the loop?
(2 answers)
Closed 5 years ago.
How do DO loops work exactly?
Let's say you have the following loop:
do i=1,10
...code...
end do
write(*,*)I
why is the printed I 11, and not 10?
But when the loop stops due to an
if(something) exit
the I is as expected (for example i=7, exit because some other value reached it's limit).
The value of i goes to 11 before the do loop determines that it must terminate. The value of 11 is the first value of i which causes the end condition of 1..10 to fail. So when the loop is done, the value of i is 11.
Put into pseudo-code form:
1) i <- 1
2) if i > 10 goto 6
3) ...code...
4) i <- i + 1
5) goto 2
6) print i
When it gets to step 6, the value of i is 11. When you put in your if statement, it becomes:
1) i <- 1
2) if i > 10 goto 7
3) ...code...
4) if i = 7 goto 7
5) i <- i + 1
6) goto 2
7) print i
So clearly i will be 7 in this case.
I want to emphasize that it is an iteration count that controls the number of times the range of the loop is executed. Please refer to Page 98-99 "Fortran 90 ISO/IEC 1539 : 1991 (E)" for more details.
The following steps are performed in sequence:
Loop initiation:
1.1 if loop-control is
[ , ] do-variable = scalar-numeric-expr1 , scalar-numeric-expr2 [ , scalar-numeric-expr3 ]
1.1.1 The initial parameter m1, the terminal parameter m2, and the incrementation parameter m3 are established by evaluating scalar-numeric-expr1, scalar-numeric-expr2, and scalar-numeric-expr3, respectively,
1.1.2 The do-variable becomes defined with the value of the initial parameter m1.
1.1.3 The iteration count is established and is the value of the expression
MAX(INT((m2 –m1+m3)/m3),0)
1.2 If loop-control is omitted, no iteration count is calculated.
1.3 At the completion of the execution of the DO statement, the execution cycle begins.
2.The execution cycle. The execution cycle of a DO construct consists of the following steps performed in sequence repeatedly until
termination:
2.1 The iteration count, if any, is tested. If the iteration count is zero, the loop terminates
2.2 If the iteration count is nonzero, the range of the loop is executed.
2.3 The iteration count, if any, is decremented by one. The DO variable, if any, is incremented by the value of the incrementation parameter m3.
I am having problems with a do while implementation for a sine taylor series. Editing the do loop to do bb = 1, 10 , 2 gives an expected result well within the margin of error, however when running the desired implementation of the do loop (do while(abs(sineseries) - accuracy > 0), will always give an answer equal to 1. So I have narrowed the possibilities down to the do while loop implementation being faulty.
program taylor
implicit none
real :: x
real :: sineseries, nfactsine
real, parameter :: accuracy = 1.e-10
integer :: signum, bb
nfactsine = 1
signum = 1
write(*,*) "Write your input value"
read(*,*) x
sineseries = 0
do while(abs(sineseries) - accuracy > 0)
sineseries = sineseries + (signum*x**bb)/nfactsine
nfactsine = nfactsine*(bb+1)*(bb+2)
signum = -signum
end do
write(*,*) sineseries, sin(x)
end program taylor
The two types of loops are not doing the same thing.
In the loop
do bb=1, 10, 2
...
end do
you have loop control with variable bb. This variable takes the values 1, 3, ..., 9 at iterations as the loop proceeds.
The do while does not have this control: you must replicate the increment of bb manually:
bb=1
do while (...)
...
bb=bb+2
end do
As Pierre de Buyl commented, you also have an error in the termination condition for the indefinite iteration count. The condition initially evaluates as false, so the loop body isn't executed even once.
#include <iostream>
int main() {
int nr;
std::cin>>nr;
while (nr > 0) {
int digit = nr % 10;
nr /= 10;
std::cout<<digit;
}
return 0;
}
Can someone please explain the workflow of this program, basically with the input "32" it outputs "23", that is good, thats my goal, my question is, why does it say "23" instead of just "2", why is the "3" being added in the end if i only said "cout digit". I get that the "3" comes from " nr /= 10", but why is it being outputed near the "2" to farm the answer "23"?
I get that the "3" comes from nr /= 10
Before the program gets to printing 3, it prints 2, which is a remainder you get after dividing 32 by 10.
The result of this division is, indeed, 3. Next loop iteration picks it up, and prints it, because 3 % 10 is 3.
The while makes two iterations. It tests if the condition is true, executes what is inside. Digit takes the value 2, nr becomes 3, it outputs 2. Then for the second iteration, nr is still bigger than 0, so digit becomes 3, nr becomes 0, it outputs 3. The condition is no longer met, so it exits the loop. (Using 23 as an input, that is)
Flow of your code
Your code works a follows (assuming you have entered the number 32, thus the variable nr is 32).
First iteration
Step 1
while(32 > 0)
result: true.
Step 2
int digit = nr % 10;
result: digit now contains the value 2 due to the remainder (%) operation.
Step 3
nr /= 10;
result: nr now contains the value 3, because 32 / 10 results in 3.2 which is a float, but since you are assigning this
number to an integer it implicit converts to the number 3.
Step 4
std::cout<<digit;
result: 2 (the variable digit is still unaffected since the remainder operation 2).
Second iteration
Step 1
while(3 > 0)
result: true (the condition in the while-loop get called again and the variable nr is 3).
Step 2
int digit = nr % 10;
result: digit now contains the value 3, because 3 % 10 = 3.
Step 3
nr /= 10;
Is not relevant anymore for any changes of the flow besides that the while-loop will terminate.
Step 4
std::cout<<digit;
result: 3 (since the digit variable is now 3).
So the complete output will be 23.
Because the while loop means the code within it is repeated until the condition is no longer satisfied. For a two digit number the print statement will therefore be executed twice.
What does this Fortran code mean:
IF (J1-3) 20, 20, 21
21 J1 = J1 - 3
20 IF (J2-3) 22, 22, 23
23 J2 = J2 - 3
22 CONTINUE
I've seen in old project and I don't have any idea what this IF with numbers (labels) means.
This is an arithmetic if statement from FORTRAN 77. Adapted from the FORTRAN 77 specification (emphasis mine):
The form of an arithmetic IF statement is:
IF (e) s1 , s2 , s2
where: e is an integer, real, or double precision expression
s1, s2, and s3 are each the statement label of an executable statement that appears in the same program unit as the arithmetic IF statement. The same statement label may appear more than once in the same arithmetic IF statement.
Execution of an arithmetic IF statement causes evaluation of the expression e followed by a transfer of control. The statement identified by s1, s2, or s3 is executed next as the value of e is less than zero, equal to zero, or greater than zero, respectively.
For the example in your question, from the last sentence above,
If J1-3 < 0 statement 20 will be executed
If J1-3 = 0 statement 20 will also be executed
If J1-3 > 0 statement 21 will be executed
Edit: A modern and much more readable way to write this would be:
if (J1-3 > 0) J1 = J1 - 3
if (J2-3 > 0) J2 = J2 - 3
What does this Fortran code mean:
IF (J1-3) 20, 20, 21
21 J1 = J1 - 3
20 IF (J2-3) 22, 22, 23
23 J2 = J2 - 3
22 CONTINUE
I've seen in old project and I don't have any idea what this IF with numbers (labels) means.
This is an arithmetic if statement from FORTRAN 77. Adapted from the FORTRAN 77 specification (emphasis mine):
The form of an arithmetic IF statement is:
IF (e) s1 , s2 , s2
where: e is an integer, real, or double precision expression
s1, s2, and s3 are each the statement label of an executable statement that appears in the same program unit as the arithmetic IF statement. The same statement label may appear more than once in the same arithmetic IF statement.
Execution of an arithmetic IF statement causes evaluation of the expression e followed by a transfer of control. The statement identified by s1, s2, or s3 is executed next as the value of e is less than zero, equal to zero, or greater than zero, respectively.
For the example in your question, from the last sentence above,
If J1-3 < 0 statement 20 will be executed
If J1-3 = 0 statement 20 will also be executed
If J1-3 > 0 statement 21 will be executed
Edit: A modern and much more readable way to write this would be:
if (J1-3 > 0) J1 = J1 - 3
if (J2-3 > 0) J2 = J2 - 3