Fortran 77 Do Loop block clarification - fortran

Both NUM and ARRAY double precision variables, not sure how the if block will execute. When will it stop? What is it actually doing?, If we go to 8, then are we exiting the do loop?
Thanks
DO 7 I = 1,28
IF (NUM - ARRAY(I)) 8,7,7
7 CONTINUE
I=29
8 NUM = ARRAY(I)
....
....
....
....

As previously stated the conditional in the loop is an arithmetic if statement.
We know (and that's explicitly stated in the previous answer here) that if num is less than array(i) the label 8 is chosen, otherwise label 7 is chosen. It's also stated in that other answer that these have the effect of exiting the loop or cycling it. To be precise, I'll continue.
A DO construct has a range. A nonblock DO construct like the one in the question has range consisting of the statements between and including the do statement and the DO termination statement (in this case 7 continue). The DO termination statement is a valid target for a jump from within the range of the construct.
When the DO termination statement is jumped to, execution remains within the scope of the construct. That termination statement is executed (in this case, continue, doing nothing) and the loop iteration condition is again tested. That is, the loop cycles.
From within a DO construct, a jump to a statement outside the range of the construct terminates execution of the construct: like an exit.
This example, then, has the equivalent form using an IF construct (with go tos - bear with me)
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
GO TO 7
END IF
7 CONTINUE
I=29
8 NUM = ARRAY(I)
Now, because the statement labelled 7 is a continue statement, we can write this as
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
CYCLE
END IF
7 CONTINUE
I=29
8 NUM = ARRAY(I)
That's still pretty ugly (and not just because of all the upper case). Fortunately, we can make this prettier. The i=29 statement will be executed only when the loop terminates without the statement labelled 8 being jumped to. Now, the loop index i has control I = 1,28 so when the loop terminates naturally the index already has the value 29. That assignment does nothing (in modern Fortran) so we can remove it. Which leaves us with
DO 7 I = 1,28
IF (NUM < ARRAY(I)) THEN
GO TO 8
ELSE
CYCLE
END IF
7 CONTINUE
8 NUM = ARRAY(I)
When we also note that the IF construct is immediately followed by the end of the loop (and so we don't need to explicitly cycle) we have
DO 7 I = 1,28
IF (NUM < ARRAY(I)) EXIT
7 CONTINUE
NUM = ARRAY(I)
or (more nicely)
DO I = 1,28
IF (NUM < ARRAY(I)) EXIT
END DO
NUM = ARRAY(I)
All this example is doing is finding the value of the earliest element in array(1:28) which is larger than num, or array(29) if none is.

That is called arithmetic if: if(a) 1,2,3 . and it means: if a<0 it goes to 1, if(a==0) it goes to 2 and if (a>0) it goes to 3.
in your code if( num-array(i)<0 ) it goes to 8 (exit the loop and skip another line), else it goes to 7 (cycle).

Related

Is the value of a Fortran DO loop counter variable guaranteed to be persistent after the loop ends? [duplicate]

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.

Incomprehensible infinite backward loop C++

I am new to C++ and I am trying to achieve a backward for loop, I founded solutions which works very well but I want to know what my version is not correct and making an infinite loop.
Here is a working version that I founded (I don't understand how we can decrease i in the condition..) :
for (unsigned i = size ; i-- > 0 ; )
{
// do stuff with i
}
Here is a version I wrote which works but don't go down to 0 (this way seems more logical to me) :
for (unsigned i = size-1 ; i > 0 ; i--)
{
// do stuff with i
}
If I say for exemple n=10, I will get this if I print i in the loop :
9
8
7
6
5
4
3
2
1
And here is the version which for me is the more logical and should go down to zero but is providing an infinite loop.
for (unsigned i = size-1 ; i >= 0 ; i--)
{
// do stuff with i
}
Could someone explain to me why the last version isn't working and what is the best choice to make ?
An unsigned number is always >= 0. (When it reaches zero a further decrement sets it to std::numeric_limits<unsigned>::max())
So your final for loop is equivalent to
for (unsigned i = size-1 ; true ; i--)
which, of course, loops forever.
In your first loop, you have i-- > 0 as the stopping condition. When i is zero, i-- is an expression with value zero (so the loop halts), despite the fact that i is then set to std::numeric_limits<unsigned>::max(). Some folk like (me included; cue the downvotes) to write i-->0 and regard --> as the slide operator. See What is the "-->" operator in C++?
The statement i >= 0 is always true because i is unsigned which means that is never below zero. If you decrease the value while i is zero, there will occur a so-called underflow and it will have a very high number.
The first version certainly gets the job done, so I would stick to it.

Having trouble understanding a portion of code (bit operation)

I can't understand how to count number of 1's in binary representation.
I have my code, and I hope someone can explain it for me.
Code:
int count (int x)
{
int nr=0;
while(x != 0)
{
nr+=x%2;
x/=2;
}
return nr;
}
Why while ? For example if i have 1011, it wouldn't stop at 0?
Why nr += x%2 ?
Why x/=2 ?!
First:
nr += x % 2;
Imagine x in binary:
...1001101
The Modulo operator returns the remainder from a / b.
Now the last bit of x is either a 0, in which case 2 will always go into x with 0 remainder, or a 1, in which case it returns a 1.
As you can see x % 2 will return (if the last bit is a one) a one, thus incrementing nr by one, or not, in which case nr is unchanged.
x /= 2;
This divides x by two, and because it is a integer, drops the remainder. What this means is is the binary was
....10
It will find out how many times 2 would go into it, in this case 1. It effectively drops the last digit of the binary number because in base 2 (binary) the number of times 2 goes into a number is just the same as 'shifting' everything down a space (This is a poor explanation, please ask if you need elaboration). This effectively 'iterates' through the binary number, allowing the line about to check the next bit.
This will iterate until the binary is just 1 and then half that, drop the remainder and x will equal 0,
while (x != 0)
in which case exit the loop, you have checked every bit.
Also:
'count`is possibly not the most descriptive name for a function, consider naming it something more descriptive of its purpose.
nr will always be a integer greater or equal to zero, so you should probably have the return type unsigned int
int count (int x)
{
int nr=0;
while(x != 0)
{
nr+=x%2;
x/=2;
}
return nr;
}
This program basically gives the numbers of set bits in a given integer.
For instance, lets start with the example integer 11 ( binary representation - 1011).
First flow will enter the while loop and check for the number, if it is equal to zero.
while(11 != 0)
Since 11 is not equal to zero it enter the while loop and nr is assigned the value 1 (11%2 = 1).nr += 11%2;
Then it executes the second line inside the loop (x = x/2). This line of code assigns the value 5 (11/2 = 5 ) to x.
Once done with the body of the while loop, it then again checks if x ie 5 is equal to zero.
while( 5 != 0).
Since it is not the case,the flow goes inside the while loop for the second time and nr is assigned the value 2 ( 1+ 5%2).
After that the value of x is divided by 2 (x/2, 5/2 = 2 )and it assigns 2 to x.
Similarly in the next loop, while (2 != 0 ), nr adds (2 + 2%2), since 2%2 is 0, value of nr remains 2 and value of x is decreased to 1 (2/2) in the next line.
1 is not eqaul to 0 so it enters the while loop for the third time.
In the third execution of the while loop nr value is increased to 3 (2 + 1%2).
After that value of x is reduced to 0 ( x = 1/2 which is 0).
Since it fails the check (while x != 0), the flow comes out of the loop.
At the end the value of nr (Which is the number of bits set in a given integer) is returned to the calling function.
Best way to understand the flow of a program is executing the program through a debugger. I strongly suggest you to execute the program once through a debugger.It will help you to understand the flow completely.

Is it prime? TI-BASIC

Hi Im trying to translate this code to TI-BASIC. Im having problems with how to change for loop into while loop and also with incrementing a number in TI-BASIC.
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}
You can efficiently use a While loop in this situation:
Input "NUMBER: ",A
1->B
3->I
√(A->D
If not(fPart(A/2
DelVar BWhile I<=D and B
fPart(A/I->B
I+2->I
End
If not(B
Disp "NOT
Disp "PRIME
In TI-Basic a While loop works as you would expect and you can have conditions for it.
Incrementing a number is as simple as saying
X+i->X
Where 'i' is the incrementer.
To change a For loop into a While loop, you'll have to set up the While loop to constantly check to see if the number and increment have passed the upper bound while increasing the increment each run through.
If you wanted to mimic i++ or ++i in TI-Basic (Using a While loop), all you would have to change would be the arrangement of the code. Please note that TI-Basic For statements always operates under ++i.
Example (i++):
0->X
While X<10
Disp X
X+1->X
End
This will display (With each number on a new line)
0 1 2 3 4 5 6 7 8 9
Example (++i):
0->X
While X<10
X+1->X
Disp X
End
This will display (With each number on a new line)
1 2 3 4 5 6 7 8 9 10
Let it be noted that TI-Basic For statements are much much faster than While loops when it comes to incrementing and should almost always be considered superior for the task.
Integrating Timtech's idea to skip even numbers effectively halves the required time to check the primality of the number with the addition of only a few extra lines.
I expanded the idea to skip multiples of two and multiples of three.
Input "Number:",X:abs(X->X
0
If not(fPart(X/2)) or not(fPart(X/3:Return
For(B,5,sqrt(X),6)
If not(fPart(X/B)) or not(fPart((X+2)/B:Return
End
1
Test Number: 1003001
Time Required: ~4 Seconds (So much better than 15 :D)
Size: 65 Bytes
I dont see why you would want to use a while loop as ti-basic has for loops:
0->F
Input "ENTER NUMBER:",N
For(I,2,int(N/2
If N/I=int(N/I
Then
int(N/2->I
1->F
End
End
If F
Then
Disp "NUMBER IS PRIME
Else
Disp "NUMBER IS NOT PRIME
End
N/I=int(N/I is a way to check for a number's remainder (another way of saying N%I==0 but ti basic does not have modulus). Another trick here is setting I to its maximum bound (int(N/2) as a sort of "break" like other languages would have

Do while with a boolean as the condition in c++?

so i want to know how a boolean acts in a condition statement in the following code
bool flag = true;
do {
d += data[i];
if (d > 15 || i == 3) {
flag = false;
}
i = i + 1;
} while (flag);
when will it exit the dowhile loop?
If either d > 15 or i == 3 evaluates to true, i will get incremented and the loop will stop.
In other words, flag is only checked at the end of each iteration, even though it might be set to false in the middle of one.
It will exit when (d > 15 || i == 3) which means (d > 15 or i == 3).
i is incremented at each iteration therefore if i is < 3 at the beginning of the program we are sure that at a certain point it will reach i == 3 and break the loop.
On d we can't tell much since we don't know it initial value nor its behavior inside the loop since we don't know anything about data.
Depends on your values of d and i...
As soon as d is greater than 15 or i is equal to 3, flag becomes false and the loop will end.
This might not happen in the same iteration, though. For example if i is incremented to 3 in a loop, it will be evaluated in the following loop first and flag might be set to false.
It will break the while when SUM of first 3 values in array Data[] be greater than "15", or break if does not be greater than 15 the SUM of first 3 values.
[It's depend on initial value of "i"]