简体   繁体   English

Fortran 77 Do Loop块说明

[英]Fortran 77 Do Loop block clarification

Both NUM and ARRAY double precision variables, not sure how the if block will execute. NUM和ARRAY都是双精度变量,不确定if块将如何执行。 When will it stop? 什么时候停止? What is it actually doing?, If we go to 8, then are we exiting the do loop? 它实际上在做什么?,如果转到8,那么是否要退出do循环?

Thanks 谢谢

DO 7 I = 1,28                                                   
IF (NUM - ARRAY(I)) 8,7,7                                    
7 CONTINUE                                                         
I=29                                                             
8 NUM = ARRAY(I) 
....
....
....
....

That is called arithmetic if: if(a) 1,2,3 . 这称为算术条件: 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. 这意味着:如果a<0它将变为1,如果(a==0)它将变为2,如果(a>0)它将变为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). 在您的代码中if(num-array(i)<0)达到8(退出循环并跳过另一行),否则达到7(循环)。

As previously stated the conditional in the loop is an arithmetic if statement . 如前所述,循环中的条件是算术if语句

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. 我们知道(在前面的答案中已明确声明),如果num小于array(i) ,则选择标签8 ,否则选择标签7 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. DO构造具有范围。 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 ). 一个像问题中那样的非块DO构造,其范围由do语句和DO终止语句之间的语句组成,包括do语句和DO终止语句(在这种情况下为7 continue )。 The DO termination statement is a valid target for a jump from within the range of the construct. DO终止语句是从构造范围内跳转的有效目标。

When the DO termination statement is jumped to, execution remains within the scope of the construct. 当DO终止语句跳转到时,执行仍在构造范围之内。 That termination statement is executed (in this case, continue , doing nothing) and the loop iteration condition is again tested. 执行该终止语句(在这种情况下, continue ,不执行任何操作),然后再次测试循环迭代条件。 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. 从DO构造内部,跳转到构造范围之外的语句会终止构造的执行:就像退出一样。

This example, then, has the equivalent form using an IF construct (with go to s - bear with me) 然后,此示例具有使用IF构造的等效形式( go to s-耐心接受)

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 现在,由于标记为7的语句是一个continue语句,我们可以将其写为

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. 仅当循环终止且未跳转到标记为8的语句时,才执行i=29语句。 Now, the loop index i has control I = 1,28 so when the loop terminates naturally the index already has the value 29 . 现在,循环索引i控制权I = 1,28因此当循环自然终止时,索引已经具有值 29 That assignment does nothing (in modern Fortran) so we can remove it. 该分配不执行任何操作(在现代Fortran中),因此我们可以将其删除。 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 当我们还注意到IF构造紧随循环结束之后(因此我们不需要显式循环),我们有

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. 此示例所做的全部工作就是查找array(1:28)最早的元素的值,该值大于num ,如果没有,则返回array(29)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM