Computer Science II TURBO PASCAL WEEK6 LOOP structures Dr.ELGİN KILIÇ
Loop Type Description while-do loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. for-do loop (for-down to) repeat-until loop nested loops Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. Like a while statement, except that it tests the condition at the end of the loop body. You can use one or more loop inside any another while, for or repeat until loop.
Loop Control Statements Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Pascal supports the following control statements. Click the following links to check their details.
Control Statement Description break statement continue statement Terminates the loop or case statement and transfers execution to the statement immediately following the loop or case statement. Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. goto statement Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.
For-do Loop A for-do loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax The syntax for the for-do loop in Pascal is as follows for < variable > := < initial > to [down to] < final > do S ; name value value For example; for i:= 1 to 10 do writeln(i);
If there are more than one process to be repated. For-Do Loop is as follows FOR< variable > := < initial > to [down to] < final > do name value value BEGIN Processes; END; For example; FOR i:= 1 to 10 DO BEGIN writeln( Value of i is ); writeln(i); END;
Where, the variable-name specifies a variable of ordinal type, called control variable or index variable; initial_value and final_value values are values that the control variable can take; and S is the body of the for-do loop that could be a simple statement or a group of statements.
Here is the flow of control in a for-do loop The initial step is executed first, and only once. This step allows you to declare and initialize any loop control variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for-do loop. After the body of the for-do loop executes, the value of the variable is either increased or decreased. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for-do loop terminates.
Example: a) for i:=1 to 10 do writeln( Bilgisayar ); b) for i:=1 to 10 do; 10 defa döngü boşuna writeln( Bilgisayar ); döner ve döngüden sonra 1 tane Bilgisayar yazar!
Example: Write a Pascal Program to show ASCII (0-255 tane) codes on screen. Program w6_p2; Var i:char; begin writeln ( ASCII kod tablosu ); for i:= #0 to #255 do begin writeln (i); readln; end.
Computer Sciences II Dr.Elgin KILIÇ
EXAMPLE: For given 100 a, b integer pairs Y is calculated as Y=2a-3b Write a pascal program to find i) Average of positive Y values when a>=b ii) The minimum negative Y value and a,b pair giving this value when a<b
var k,p,n,a,b,tp,y,min,mina,minb: integer; avg: real; Begin p:=0; tp:=0; n:=0; for k:= 1 to 100 do BEGIN writeln( Enter, k,. a,b pair---------> );readln (a,b); y:=2*a-3*b; if (a > = b) and (y>0) then begin p: = p+1; tp: = tp+y;
if (a < b) and (y<0) then begin n: = n+1; if (n=1) then begin min:=y; mina:=a; minb:=b; if (y<min) then begin min:=y; mina:=a; minb:=b; END; {for için} write ( Answer of i =, p, tp/p); write ( Answer of ii=, min,mina,minb); End. readln;
NESTED For-Do LOOPS for variable1:=initial_value1 to [downto] final_value1 do begin for variable2:=initial_value2 to [downto] final_value2 do begin statement(s); FOR i:=1 to N DO BEGIN.. FOR j:=1 to M DO BEGIN statements END; END; One loop executes inside of another loop(s). Example structure: Outer loop (runs n times) Inner loop (runs m times) Body of inner loop (runs n x m times)
INITIAL VALUE of LOOP VARIABLE DOES NOT NEED TO START FROM ZERO or ONE. Example: for i : = a to b do İki for arasında başka for j : = c to d do işlem yoksa begin begin. Process / Processes; koymaya gerek yok.
What is the output of following source code? BEGIN for i:=1 to 4 do begin for j:=1 to 5 do begin writeln(i,' ',j); if j=3 then break; writeln('next i loop'); writeln('finished!'); END.
It yields : 1 1 1 2 1 3 Next i loop 2 1 2 2 2 3 Next i loop 3 1 3 2 3 3 Next i loop 4 1 4 2 4 3 Next i loop Finished!
What is the output of following source code? BEGIN for i:=1 to 5 do begin for j:=1 to 3 do begin writeln(i,' ',j); if i=2 then break; writeln('finished!'); END.
It yields : 1 1 1 2 1 3 2 1 2 2 2 3 Finished!
Example: Write a Pascal Program to find the PRIME NUMBERS betwen 2 to 50.
Program nestedprime; var number, j:integer; begin for number := 2 to 50 do begin for j := 2 to number do if (number mod j)=0 then break; {* if factor found, not prime *} if(j = number) then writeln(inumber, ' is prime' ); end.
Example: 480 integers are given in 20 groups each containing 24 integers. Write a Pascal Program to find a) Product of elements in each group which are >5 b) Average of positive values in each group c) The maximum of all PRODUCTS of Groups and the group which gives this MAXIMUM VALUE. PS: The solution in next slide based on a assumption that we have numbers which are greater than 5 in EACH GROUP. If not solution MUST CHANGE.
Var H, i, j, SS:byte; Enb, x,c,t,k:integer; ort:real; Begin for i :=1 to 20 DO begin c:=1; t:=0; ss:=0; for k:=1 to 24 DO Begin writeln( Enter numbers of group,i, element, k ); Read (x); if x > 5 then c: = c * x; if x > 0 then begin T: =T+x; SS: = SS + 1; {for k için}
if i = 1 then begin enb:= C; H : = i; if enb < c then begin enb: = c ;H: = i; ORT: = T/ss; write( answer of a =,c); write( answer of b =,ORT); {for i için} write ( Answer of c =,enb,h); Readln; End.
Example: 480 tourists came to İzmir by ship to visit Ephesus. There are 20 busses to carry them. Write a Pascal Program to find a) Average age of each bus b) The youngest tourists and oldest tourist in each BUS c) The maximum of all AVERAGES of BUSES and the BUS which gives this MAXIMUM VALUE. d) The youngest and oldest tourists and the number of the buses they travel in.
Homework: For given 10 a,b and n integers write a Pascal Program to calculate the following formula and find the following Y= i) Biggest Y value and a,b,n pair giving this. ii) n i 1 a a b b i i 1 i 3 Average of Y values when a>b