Repetition Execution Examples

  1. Example of a simple 'do loop'. The following code produces points on a damped vibration curve \(y=e^{-x}sinx\)
  2. program dvib c This program produces points on a curve defined by y=e^-x * sinx implicit none c Declaring the variables real x, y, xstart, xend, delx c Ask for user inputs write(*,*) 'Enter the lower and upper limits on x and step size' read(*,*) xstart, xend, delx c Print the results write(*,*) ' X Y' write(*,*) '=======================' c The do loop that actually calcuates the values do 10 x=xstart, xend, delx y=exp(-x)*sin(x) write(*,*) x,y 10 continue end program dvib



    Sample Run:
    Enter the lower and upper limits on x and step size
    1, 2, 0.25
    
         X            Y
     =============================
       1.00000000      0.309559882    
       1.25000000      0.271888644    
       1.50000000      0.222571224    
       1.75000000      0.170991123    
       2.00000000      0.123060025    
    
    

  3. Example of a simple 'while loop'
  4. program mnfail c This program computes the time to failure implicit none integer count real failtm, enddta, sum, mean parameter (enddta = -1.0) c Initialize the variables count = 0 failtm = 0 sum = 0 mean = 0 c Read the first failure data write(*,*) 'Enter the failure time of',enddta,'or less to stop' write(*,*) 'Enter failure time' read(*,*) failtm c Read the data and keep updating the sum till the user doesn't c invoke the end-of-data number do while (failtm .ne. enddta) count = count + 1 sum = sum + failtm c Read the data again write(*,*) 'Enter failure time' read(*,*) failtm end do c Calculate and display the mean time to failure if (count .ne. 0) then mean = sum/count c Display the results write(*,*) '' write(*,*) 'Number of failure times recorded:', count write(*,*) 'mean time to failure:', mean else write(*,*) 'No failure times were entered' end if end program mnfail

    Sample Run:
    
       Enter the failure time of -1.00000000 or less to stop
       Enter failure time
       127
       Enter failure time
       123.5
       Enter failure time
       155.4
       Enter failure time
       99
       Enter failure time
       117.3
       Enter failure time
       201.5
       Enter failure time
       -1.0
    
       Number of failure times recorded: 6
       mean time to failure: 137.283340