Friday, May 17, 2013

Nested Do Fortran



 Credits


Written By: Prof. Martha Selby
Authored By: Mark Sobek
Revised By: John Even
HTML Conversion By: Michelle Thayer


 Form and Function: DO Loops

DO loop is a special statement that constructs an iterative loop (a loop that is repeated a specified number of times).
General Form of a DO Statement:

DO n I = INITIAL, LIMIT, STEP
  • n = Statement number to identify end of the loop
  • I = Index or control variable
  • INITIAL = Initial value given to control variable
  • LIMIT = Limiting value of control variable
  • STEP = Increment added to control variable each loop repetition

 Steps in Executing a DO Loop 

  1. Control variable assigned initial value.
  2. Check if value of control variable exceeds the limiting value; if it does, do NOT execute loop.
  3. Execute body of loop.
  4. Change value of control variable by adding the value of STEP.
  5. Repeat steps 2 and 3.

 DO Loop Rules

DO n I = INITIAL, LIMIT, STEP
  1. INITIAL, LIMIT, and STEP can be:
    • positive or negative,
    • integer or real,
    • constants, variables, or expressions.
  2. Do not change the value of I, the control variable, inside the loop.
  3. Changing the value of INITIAL, LIMIT, or STEP inside the loop has no effect on the number of times the loop is executed.
  4. The CONTINUE statement is strongly encouraged as the last statement in the loop, but most other executable statements are allowed. (See textbook for a specific list.)
  5. After the DO loop is completed, the control variable (I) contains the last value that exceeded the limit.
  6. You can transfer out of a DO loop before it is completed.
  7. You can only enter a DO loop through the DO statement.
  8. If a step size is not given, 1 is assumed.

 Nested DO Loops

One DO loop may be completely contained within another DO loop. The two DO loops must use different control variables.
  • It is allowed, but not recommended, to use one CONTINUE to end two DO loops.


     Flowcharting DO Loops

    There are a number of different ways to represent DO loops in a flowchart. Four ways to flowchart the following DO loop are shown:
      DO 70 I = 1, 30, 2
         SUM = SUM + 1
         70  CONTINUE
    
       
    Example 1 - Example 2 - Example 3 - Example 4



     Examples of DO Loops

    Example 1:
      DO 100 NUMBER = 1,5
         PRINT*, NUMBER
         100 CONTINUE
       Output:
       1
       2
       3
       4
       5
    
    Example 2:
      DO 100 NUMBER = 10,1,-2
         PRINT*, NUMBER
         100 CONTINUE
       Output:
       10
       8
       6
       4
       2
    
    Example 3:
      DO 50 N = 10,1
         50 PRINT*, N
          50 CONTINUE
       Output:
      None - loop not executed



  • Subscribe to Our Blog Updates!




    Share this article!

    No comments:

    Post a Comment

    =(*_*)------------------------(^_^)=
    :::::|berkomentar dengan sopan adalah akhlak kemulian|:::::

    Return to top of page
    Powered ByBlogger | Design by PARMAN | Blogger Template by UKK As-Siraaj