Computer Science II TURBO PASCAL WEEK4 Program Flow Commands Dr.ELGİN KILIÇ
IF THEN ELSE expression: If <case> then Begin <Processes when CASE is TRUE> End else Begin < Processes when CASE is FALSE> End;
Example: For given 100 integers by keyboard control whether number is negative positive or zero. Label A1; Var a,t: integer; begin T:=0; A1: write( Enter a number please. ); readln(a); t:=t+1; if (a>0) then writeln( The given number is positive. ); else if (a<>0) then writeln( The given number is negative. ); If (t<100) then goto A1; readln; {Let us to see the output on screen} end.
Example: For any person weight and height values are given. And ideal weight is calculated as follows; For Women and For men ideal weight=height-108 ideal weight=height-110, Write a pascal program to show the output below; ideal weight> weight You should gain weight. ideal weight< weight You should loose weight. ideal weight = weight You are on ideal weight.
Label bas; Var SAY,w,h,ideal:byte; gender,answer:char; Begin SAY:=0; bas: SAY:=SAY+1; Writeln( Enter gender ); readln(gender); writeln( Weight= ); readln(w); writeln( Height= ); readln(h); if (gender= F ) or (gender= f ) then ideal:=h-110 else ideal:=h-108 if ideal>w then writeln ( You should gain, ideal-w) else if ideal<w then writeln ( loose, w-ideal ) else writeln( You are at ideal weight. ); {SAY:=SAY+1;} If (SAY<50) THEN GOTO BAS; Readln;
Example: In a laboratory there are 50 researchers. Each researcher does an experiment 5 times per day for 30 days. Write a Pascal Program to find; a) Daily Average of each researcher b) Monthly average of each researcher c) Maximum of daily averages of researchers and the day in which this maximum value is obtained.
Label 5,10,20; Var kisi,gun,ds,gun1: byte; ort,top1,enb: real; d,top: integer; Begin kisi:=0; 5: kisi:=kisi+1; gun:=0; top1:=0; 10: gun:=gun+1; ds:=0; top:=0; 20: ds:=ds+1; writeln( result of,ds, expr of,kisi, researcher in,gun, day = ); readln(d); top:=top+d;
if ds<5 then goto 20; ort:=top/5; writeln( Average=, ort); if gun=1 then begin enb:=ort; gun1:=gun; end else if enb<ort then begin enb:=ort; gun1:=gun; end; top1:=top1+ort; if gun<30 then goto 10; writeln( Answer of b =,enb,gun1); writeln( Answer of a =, top1/30); if kisi<50 then goto 5; end.
EXAMPLE: b n 1 x 2n 1 (2n 1)! Write a Pascal program to find the result of formula above for given only one x, b pair.
Label 5,10,20; Var x,b,n,i,k: integer; car,carp1: longint; t,top: real; Begin writeln( Enter x b values ); readln(x,b); n:=0; top:=0.0; 5: n:=n+1;
i:=0; carp:=1; 20: i:=i+1; carp:=carp*x; if (i<2*n-1) then goto 20; {power process} k:=0; carp1:=1; 10: k:=k+1; carp1:=carp1*k; if (k<2*n-1) then goto10; {factorial process} end. t:=carp/carp1; top:=top+t; if n<b then goto 5; writeln( sonuç=, top);
Homework: For given 100 a, b, c a b, M=a+b+3c a > b, M=5a+2b+4c Write a Pascal program to find triples M is calculated as follows; a) How many M values are divisible by 4 when a>b? b) What is the minimum M value when a<b and a, b, c triple giving this minimum value? c) First calculated M value when a=b. Z
CASE ----OF This structure is used to change the flow of program depending on many cases of one variable (input data). Type of input can be numeric or character based. Program process the part when the constant value is equal to given input. If no case is satisfied then ELSE part runs. It is not compulsory to use else part.
Case EXPRESSION OF const value1: Process / Begin Processes End; const value2: Process / Begin Processes End;... const value n: Process / Begin Processes End; [ELSE Process/ Processes] End;
Example: In a shop there 5 types of products are sold. This sale process stops when product type is given as a negative input. Write a Pascal Program to find HOW MANY OF EACH TYPE OF PRODUCT is sold when sales process stopped.
Label A1; Var t1,t2,t3,t4,t5,type:integer; Begin t1:=0;t2:=0;t3:=0;t4:=0;t5:=0; A1: Writeln( Enter product type as 1/2/3/4/5. ); Readln(type); If (type=1) then t1:=t1+1; If (type=2) then t2:=t2+1; If (type=3) then t3:=t3+1; If (type=4) then t4:=t4+1; If (type=5) then t5:=t5+1; end; If (type>0) then goto a1; Writeln( Total amounts= ); Writeln(t1,t2,t3,t4,t5); readln; end.
Label A1; Var t1,t2,t3,t4,t5,type:integer; Begin t1:=0;t2:=0;t3:=0;t4:=0;t5:=0; A1: Writeln( Enter product type as 1/2/3/4/5. ); Readln(type); Case type of 1: t1:=t1+1; 2: t2:=t2+1; 3: t3:=t3+1; 4: t4:=t4+1; 5: t5:=t5+1; end; If (type>0) then goto a1; Writeln( Total amounts= ); Writeln( type1=,t1,t2,t3,t4,t5); readln; end.
Example: In a shop there 5 types of products are sold. This sale process stops when product type is given as Z. Write a Pascal Program to find HOW MANY OF EACH TYPE OF PRODUCT is sold when sales process stopped.
Label A1; Var t1,t2,t3,t4,t5:integer; type:char; Begin t1:=0;t2:=0;t3:=0;t4:=0;t5:=0; A1: Writeln( Enter product type as A/B/C/D/E. ); Readln(type); If (type= A ) then t1:=t1+1; If (type= B ) then t2:=t2+1; If (type= C ) then t3:=t3+1; If (type= D ) then t4:=t4+1; If (type= E ) then t5:=t5+1; If (type<> Z ) then goto a1; Writeln( Total amounts= ); Writeln(t1,t2,t3,t4,t5); readln; end.
Label A1; Var t1,t2,t3,t4,t5 :integer; type:char; Begin t1:=0;t2:=0;t3:=0;t4:=0;t5:=0; A1: Writeln( Enter product type as A/B/C/D/E. ); Readln(type); Case type of A : t1:=t1+1; B : t2:=t2+1; C : t3:=t3+1; D : t4:=t4+1; E : t5:=t5+1; end; If (type<> Z ) then goto a1; Writeln( Total amounts= ); Writeln( pro1=,t1, pro2=,t2, prod3=,t3, p4=,t4, P5=,t5); readln; end.
Example: Write a Pascal Program to find LETTER notation of a student s grade according to intervals below. 86-100 AA 79-85 BA 71-78 BB 60-70 CC 46-59 DD 0-45 FD
Var g:integer; Begin Writeln( Enter student s grade. ); Readln(g); Case g of 0..45 :writeln( FD ); 46..59 : writeln( DD ); 60..70 : writeln( CC ); 71..78 : writeln( BB ); 79..85 : writeln( BA ); 86..100 : writeln( AA ); end; Readln; END.
Label A1; Var i,g:integer; Begin i:=0; A1: Writeln( Enter student s grade. ); Readln(g); i:=i+1; {ANA SAYAÇ} Case g of 0..45 :writeln( FD ); 46..59 : writeln( DD ); 60..70 : writeln( CC ); 71..78 : writeln( BB ); 79..85 : writeln( BA ); 86..100 : writeln( AA ); end; If (i<100) then goto a1; Readln; END. If there have been 100 students in the class, then solution will be like shown in left side.
Example: Write a Pascal Program to find LETTER notation of a student s grade according to intervals below in a class of 100 students and how many students are there in each INTERVAL. 86-100 AA 79-85 BA 71-78 BB 60-70 CC 46-59 DD 0-45 FD
Label A1; Var i,x,y,z,t,w,v,g:integer; Begin i:=0;x:=0;y:=0;z:=0;t:=0;w:=0;v:=0; A1: Writeln( Enter student s grade. ); Readln(g); i:=i+1; Case g of 0..45 :begin writeln( FD ); x:=x+1;end; 46..59 : begin writeln( DD ); y:=y+1;end; 60..70 : begin writeln( CC ); z:=z+1;end; 71..78 : begin writeln( BB ); t:=t+1;end; 79..85 : begin writeln( BA ); w:=w+1;end; 86..100 : begin writeln( AA ); v:=v+1;end; end; If (i<100) then goto a1;
Writeln( There are,x, students in FD ); Writeln( There are,y, students in DD ); Writeln( There are,z, students in CC ); Writeln( There are,w, students in BB ); Writeln( There are,t, students in BA ); Writeln( There are,v, students in AA ); readln; end.
Example: The amount of products that an employee produced in one is given day by day. Employee earns 25TL per each item of product at the first half of the month and 15TL per item at the last 15 days of the month. Write a Pascal Program to find the total amount of products and earning of this employee at the end of the month.
Label bas; Var ps,gun,topu,topps,ucret:integer; Begin Gun:=0;topu:=0; topps:=0; bas: Writeln( Enter number (amount) of products ); Readln(ps); Gun:=gun+1; Case gun of 1..15 :ucret:=ps*25; 16..30:ucret:=ps*15; end; topu:=topu+ucret; topps:=topps+ps; if gun<30 then goto bas; writeln (topu, topps); end.