Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of 'n = n'?

Tags:

fortran

I am reading some Fortran code, and every so often the previous programmer throws in the statement 'n = n'. What is the purpose of this? Some example code:

if (cmult.lt.5.) then
    kx = 0
    do k=ipd(ii),lpd(ii)
       kx = kx + 1
       p1(kx) = epp_rfc(ipp,k)
       epp_rfc(ipp,k) = cmult*epp_rfc(ipp,k) + x   
       zero(ix)
       p2(kx) = epp_rfc(ipp,k)
       n = n
    enddo

if (cmult.gt.0.) then
    n = n
endif

else
    nk = lpd(ii) - ipd(ii) + 1
    do k=ipd(ii),lpd(ii)
       kx = kx + 1
       p1(kx) = epp_rfc(ipp,k)
       epp_rfc(ipp,k) = pp(imem) + zero(ix)
       p2(kx) = epp_rfc(ipp,k)
       n = n
    enddo
endif
like image 908
Jim Ward Avatar asked May 25 '11 13:05

Jim Ward


People also ask

What is the purpose of the \n?

What is \n exactly? The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

What is \n called and what purpose does it serve?

\n is the newline or linefeed, other side \r is the carriage return.

What is \n used for in Python?

🔹 In Summary The new line character in Python is \n . It is used to indicate the end of a line of text.


2 Answers

Code like this is frequently used to allow the programmer to set a breakpoint in debuggers that don't support conditional breakpoints.

By setting a breakpoint on that line, it will only be hit if cmult.gt.0.

like image 167
SLaks Avatar answered Sep 21 '22 00:09

SLaks


The fact that n = n is used for checking the value of cmult.gt.0. and is used as 'condition breakpoint' is coincidental in a sense that it's not the reason why n = n appears in the above code snippet.

The real reason why n = n is used in this case lies in the fact that scope in the source code is never translated by the most popular compilers to assembly language (the computer is not smart enough to understand what 'scope' is unless its explicitly programmed by the programmer and if it did the programs will run 100s of time slower ... because of the extra instructions emitted for the logic to support scoping). It's only there to constraint the programmer in the source code and introduce a structure to the code so that linker & compiler can do their job - or issue error if violate scoping.

Because scope doesn't exist enddo cannot be paused at unless the comipler takes care to insert some instruction(like nop) and debug symbols for it to magically allow the debugger to stop at enddo. Without n = n the outcome of this line p2(kx) = epp_rfc(ipp,k) cannot be checked as the values reset at the top of the loop. This is why n = n is used to stop after the p2(kx) = epp_rfc(ipp,k) and check the final result on each itteration. After it when this code comes:

if (cmult.gt.0.) then
    n = n
endif

is used for convenience (again you cannot stop at endif or else) purposes and yes this is kind of conditional breakpoint. But n = n is again used because you cannot stop after checking if (cmult.gt.0.) - you can stop at it but not after it - the next instruction will be after the outer if else. I hope this makes sense.

Edit: If it still doesn't make too much sense here is additional info: in order to check the value of p2(kx) after p2(kx) = epp_rfc(ipp,k) has executed the debugger has to emit instructions to check/fetch it - hence it is required to know a) what is the size of p2(kx) b) it's location - remember the last instruction(s)! and c) emit the appropriate instructions to fetch the value p2(kx). All to complicated for the debugger as this is actually logic - the debugger has to be 'smart' (falls into AI domain), if a debugger could do this Terminator would have existed by now.

like image 27
selion Avatar answered Sep 22 '22 00:09

selion