260 likes | 274 Views
Discover the power and versatility of program loops, a fundamental concept in programming. Learn to repeat sequences of statements with precision and efficiency using F. Explore various repetition techniques, from counter-controlled to logical expression-controlled loops. Dive deep into nested loops and understand how to control loop iterations effectively.
E N D
Week 6 Repeating parts of your program
Repeating parts of your program A very large proportion of mathematical techniques rely on some form of iterative process, while the processing of most types of data requires the same or similar actions to be carried out repeatedly for each set of data. One of the most important of all programming concepts, therefore, is the ability to repeat sequences of statements, either a predetermined number of times or until some condition is satisfied. F has a very powerful, simple to use, facility for controlling the repetition of blocks of code. The use of repetitive techniques, however, often leads to situations in which it is requested to end the repetition earlier than had been anticipated.
Repeating parts of your program A “repetitive structure” or “loop” makes possible the repeated execution of one or more statements called “body of the loop”. There are two basic types of repetition : ·“repetition controlled by a counter” , in which the body of the loop is executed once for each value of some control variable in a specified range of values. ·“repetition controlled by a logical expression”, in which the decision to continue or to terminate the repetition, is determined by the value of some logical expression.
Program repetition • In many cases, we have to repeat certain sections of a program • Many numerical methods involve repetition/iteration • We have to have construct to repeat a group of statements, to end the repetition, or to restart it when certain condition is fulfilled.
EXAMPLEfor a TYPICAL CYCLE : Repeat the following 3 steps 21 times considering 5oC intervals from 0oC till to 100oC without the need for any data to be read at all : 1.step : read the initial and last Celcius temperature 2.step : calculate the corresponding Fahrenheit temperature for 5oC intervals 3.step : print both tempratures A sequence of statements which are repeated is called a “loop.“ The do – constructs provides the means for controlling the repetition of statementswithin a loop.
General DO LOOP – TYPE 1 (controlled by logical expression) The number of iterations cannot be determined in advance, and a more general repetition structure is required. do Block of statements_1 if ( logical_expression) then exit else block of statements_2 end do
Count – controlled DO LOOP – TYPE 2 In this count-controlled do loop, the do variable is incremented by the unit value “1” on each pass starting from the initial_value (inclusive) till to the final_value (inclusive). do count_variable = initial_value, final_value Block of statements end do count_variable : must be an integer initial_value, final_value : are arbitrary expressions of integer type step_size = 1 (default)
do construct do count = initial, final, inc block of statements end do loop count_variable : must be an integer initial_value, final_value : are arbitrary expressions of integer type selected step_size = inc : must be an integer
Examples do statement iteration count do variable values do i = 1,10 10 1,2,3,4,5,6,7,8,9,10 do j = 20, 50, 5 7 20,25,30,35,40,45,50 do p = 7, 19, 4 4 7,11,15,19 do q = 4, 5, 6 1 4 do r = 6, 5, 4 0 (6) do x = -20,20, 6 7 -20,-14,-8,-2,4,10,16 do n = 25, 0, -5 6 25,20,15,10,5,0 do m = 20, -20, -6 7 20,14,8,2,-4,-10,-16
Example do number = 1, 10, 2 print *, number, number **2 end do OUTPUT produced will display following results : 11 3 9 525 7 49 9 81
Count-controlled do loops docount = initial, final, inc docount = initial, final (inc = 1) do Iteration count: How many times we will go through the loop? max((final-initial+inc)/inc, 0) Integer variable
NESTED DO - LOOPS The body of a do loop may contain another do loop. In this case,the second do loop is said to be “nested” within the first do loop. EXAMPLE : dom = 1, 4 do n = 1, 3 product = m * n print *, m, n, product end do end do OUTPUT 1 1 1 1 2 2 1 3 3 2 1 2 2 2 4 2 3 6 3 1 3 3 2 6 3 3 9 4 1 4 4 2 8 4 3 12
More flexibility... For do – loops especially defined as Type 1, it is needed a control statement in order to control the cycles, otherwise it is possible to have an “infinite loop”. Using the command “ exit “ all the remaining statements in the loop are omitted and thus, a transfer of control following the “ end do ” statement is obtained. Thus, an “exit“ statement can cause termination of a current/indexeddo - construct before the do variable value goes beyond the final or limit value.
Some flexibility... do . . . if (condition) then exit end if . . . end do .
Some more flexibility... do . . . if (condition) then cycle end if . . . end do .
Some more flexibility... As mentioned before, exit statement causes repetition of a loop to terminate by transfering control to the statement following the “end do”. On the other hand, sometimes it is necessary to terminate only the current repetition and then jump ahead to the next one. F provides the “cycle” statement for this purpose.
Example PROBLEM : Suppose that in the temprature-conversion only temprature of 0oc or above values are wanted to convert. do . if ( Celcius < 0.0 ) then print *, “ given temprature must be 0.0 or above” else cycle ! go and convert the given temprature end if . end do
Naming your do constructs Especially in “nested do–loops” it’s very difficult to control the transfer of the program. As mentioned before an “exit“ statement in the example seen below, will transfer control to the first executable statement following the second “ end do ”.On the other hand, there will be occasions when it is required to exit from all of the enclosing loops; or even from more than the immediately enclosing or current loop, but not from all of them.For this reason it is strongly recommended to use “named do– constructs” by preceding the do statement by a name as is seen below : block_name:do . . end doblock_name
Naming your do constructs outer: do . . inner: do . . . end doinner . end doouter
Dealing with exceptional situations: There are occasionally situations in which the statements used before are inconvenient or make programming very difficult.Thus, two additional statements exist to help us in these exceptional situations. These are, STOP:this statement terminates the execution without to need to find a way of reaching the “end” statement of the main program unit. This word “stop” causes execution of the program to be terminated immediately. RETURN : this statement causes a return from a procedure without the need to find a way of reaching the “end” statement of the procedure. This word “return” causes execution of the procedure to be terminated immediately and control transferred back to the program unit which called or referenced the procedure.
Dealing with exceptional situations: • stop statement • simply terminates execution • return statement • causes execution of the procedure to be terminated immediately and control transferred back to the program unit which called or referenced the procedure
program can_pressure ! This program calculates the pressure inside the can real :: T,pressure T=15.0 do T=T+1 pressure=(0.00105*(T**2))+(0.0042*T)+1.352 if (pressure>3.2) then exit end if print *,"The pressure inside the can is",pressure," atm", c " at",T," degree C" end do end program can_pressure
program examination_marks ! This program prints statistics about a set of exam results ! variable declerations integer :: i,number,mark,maximum,minimum,total real :: average ! initialize variable total = 0 ! read number of marks , and then the marks print *,"how many marks are there" read *,number print *," please type ",number," marks, one per line" ! Loop to read and process marks do i = 1 , number read *, mark ! initialize max. and min. marks for only the first loop. if (i==1) then ! this if construct is executed for the case only i=1. maximum = mark minimum = mark end if ! on each pass ,update sum,maximum and minimum total = total + mark if (mark > maximum)then maximum = mark else if (mark < minimum)then minimum = mark end if end do !! calculate average mark and print out results average = real(total) / number print *,"highest mark is",maximum,"lowest mark is",minimum,"average mark is",average end program examination_marks
program lever ! This program calculates the effort required for levers of lengths ! differing in steps 2 metres integer ,parameter :: load=2000,d2=2 integer :: d1,n,m real :: effort print *,"please type the minumum limit of distance n and max limit c m" read *,n,m do d1 = n , m , 2 effort = real(load)*real(d2)/real(d1) print *,"The required effort when d1=",d1," m."," is", c effort," kg" end do end program lever
program loop_test1 integer :: i,j,k,l,m,n i=1 j=2 k=4 l=8 m=0 n=0 do i=j,k,l k=i do j=l,m,k n=j do k=l,n do l=i,k m=k*l end do end do end do end do print *,i,j,k,l,m,n end program loop_test1
program paper_size integer :: n real :: cm,inch,p1,p2 ! a program to calculate the international paper sizes do n = 0,6 p1 = 0.25 - n/2.0 p2 = -0.25 - n/2.0 cm = (2.0**p1 * 2.0**p2)*100.0 inch = cm/2.54 print *,"A",n," is",cm," cm"," and",inch," inch" end do end program paper_size