Fortran IF loop [duplicate] - if-statement

This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 9 years ago.
I have a question about some code I'm looking at written in Fortran. The section of the code I'm confused about is written below.
DO 40 LL=1,N
DO 40 I=1,N-1,2
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
NODO=LL-I+1
IF((LL.EQ.1) .AND. (I.EQ.N-1)) NODO=NODO+N
I don't understand the condition for the first IF statement. It just looks like numbers are being multiplied together but that number isn't checked against anything. Then 3 line numbers are written after the IF statement. Do anyone know what this IF statement is doing? The last IF statement makes sense as a condition is actually being checked. Thanks.

The line
IF((LL-I)*(LL-I-1)*(LL-I*2)*(LL-I+N-2)) 22,21,22
is an arithmetic if statement, which is certainly obsolescent (the Fortran standard term for deprecated) and may even have been removed in the latest language standard(s). If the condition evaluates to a negative number program control branches to the line with the first label (ie 22), if it evaluates to 0 to the second label (21), if to a positive value to the third label (22). As you see the three labels need not all be different.

Related

What is for(;;) loop in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My friend showed me this and I have no idea how it works and what it's called. Can someone explain to me how it loops the way it does? For example:
for(;;){
cout << "loop" << endl;
}
It will just keep looping the string forever. This kind of loop can be used for anything. How does this work?
According tho the language specification, empty condition in for iteration statament is equivalent to true condition.
6.5.3 The for statement
1 The for statement
for ( for-init-statement conditionopt; expressionopt) statement
is equivalent to
{
for-init-statement
while ( condition ) {
statement
expression ;
}
}
...
2 Either or both of the condition and the expression can be omitted. A missing condition makes the implied while clause equivalent to while(true).
So, the loop loops forever. That's all there is to it.
It loops infinitely as no initialization, conditional and increment values are passed in the parameters of the loop. A typical for loop takes parameters as follows: (<initialization>;<conditional>;<increment>)
This post explains it quite well in my opinion. See the answer by spex:
Why can the condition of a for-loop be left empty?
With the structure of the for loop being for(clause; expression-2; expression-3){}, when expression-2 is left out it is replaced with a nonzero constant. This is the part of the loop that determines whether it should keep looping or not. As a nonzero constant evaluates to true, it becomes an infinite loop.
That for loop essentially says the following three things (each separated by the semicolons in your for loop "header?"):
Don't initialize anything.
Don't break from the loop.
Perform no afterthoughts for each loop iteration.
Wikipedia's for loop page actually has a section about this.
As many have pointed out, it is equivalent to while (1).
When is it useful? Wherever you need an infinite loop such as:
A game loop - would be kinda useful to have the game, loop indefinitely until the user decides to quit the game.
OS scheduler - The scheduler needs to loop indefinitely, scheduling processes according to some algorithm until the OS stops
An intepreter - If you have ever programmed in python, you may have come across the interpreter which lets you type some command and then executes it. This is also implemented using a similar infinite loop
In all those examples, the common factor that leads to using an infinite loop is that the terminating condition is not known or the terminating condition is complex (game loops for example)

Strange Fortran instruction [duplicate]

This question already has answers here:
Strange label usage for an IF condition in a DO loop [duplicate]
(2 answers)
Closed 7 years ago.
Learning old Fortran77-codes full of "goto-spagetti", I met with the following instruction:
if(condition) label1, label2, label3
where labels are just three numbers. Has anybody any ideas what it means?
Arithmetic if. It goes to
label1 if the expression is negative
label2 if the expression is 0
label3 if the expression is positive.
Caveat: If you use it with floating point expressions, note that a result that should be 0 might be "a small number close to 0" due to rounding errors. This limits the usefulness of the arithmetic if.

Odd variable assignment [duplicate]

This question already has answers here:
Uses of C comma operator [duplicate]
(20 answers)
Closed 9 years ago.
Good day, everyone.
I've come across a peculiar piece of code today, which I don't quite understand.
I don't even know how to search for this particular problem.
In this code, which works, a variable assignment is done like this:
if(condition) {
Var1 = false, Var2 = false;
}
Now, I was under the impression, that ALL commands need to be terminated by a semicolon instead of a comma. I am familiar with the syntax
Var1 = Var2 = false;
but not with the one posted above. The compiler (g++) doesn't even throw me a warning or anything...am I missing something from the specification here?
Or is the compiler generous with me and just replaces the , with a ; internally? If so, shouldn't he at least throw a warning?
Thank you for your time.
am I missing something from the specification here?
Yes, it's the "comma operator", specified by C++11 5.18. It evaluates the sub-expression to the left, then the one to the right, and the overall result is that of the right-hand one.
In this case, it's equivalent to two expression statements separated by ;
It's useful in places like if/while/for where you're only allowed one expression, but might want to do more than one thing:
while (++i, --j != 0)
and also if you like to jam multiple statements together to make life difficult for whoever has to read your code.
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). (read more)
As Alexandru Barbarosie pointed out, there's quite a thorough explanation on what's happening at https://stackoverflow.com/questions/1613230/uses-of-c-comma-operator
To quickly summarize it for whomever stumbles across this post: When used outside of for loops and stuff, the , actually has the same effect as the ;.
For more information, please visit the link.

DATA declaration in Fortran [duplicate]

This question already has an answer here:
what does DATA TKX/2HKX/ mean in fortran?
(1 answer)
Closed 1 year ago.
Does anyone know the meaning of 4HEND in the following line which comes from an old Fortran code?
DATA XHEND / 4HEND /
4HEND is a Hollerith code to represent the character string "END ". In very old FORTRAN, the variable XHEND might even be a 4-byte integer or real used to hold character data. If implicit typing was in effect in this program, XHEND would have been a real. Another recent question with Hollerith codes: Writing both characters and digits in an array
It replaces assignment
XHEND='END '

Meaning of a comma in a number [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Comma Operator
that probably a trivial question, but I don't know the answer. And this has been troubling me this afternoon.
I was just writing a function to convert RVB to YUV. Nothing really special, but mistakenly used the comma (,) instead of a dot in my numbers.
It compiles but the result was not what I expected, for example "-3713796" instead of a 0-255 range number.
(0,615*(double) 61) - (0,51498*(double) 61) - (0,10001*(double) 61)
So what does it mean ?
If it's not a compilation error, it's probably usefull for something but what?
Ps: I was using C++ with Qt.
You've accidentally used the comma operator. It evaluates its first operand and discards the result, and then evaluates the second operand and returns its value and type.
In my experience, it's most commonly used in loop statements. For other uses, see Uses of C comma operator
It's the comma operator which evaluates each operand and returns the rightmost. In this case it evaluated 0, then 615*(double) 61 and resulted in a value of that product.