COMPUTER SCIENCES II Spring Term 2017 Asst.Prof.Elgin KILIÇ TURBO PASCAL WEEK 2
DECLARATION BLOCKS in DETAIL Uses There default sub pascal programs called UNITS which are already embedded in pascal editor. To access these USES block is used to run these on editor of pascal. Generally it is given in first line after name of program. UNITS can not run (process) by themselves. They must be called by a program.
Example for Uses BLOCK CLRSCR = Clear Screen = This is used to clear the screen it is a reserved command of PASCAL itself. Uses crt;.. Begin {main program block} Clrscr;.. end. 3
Label BloCK In any program each expression is separated by ; from other expressions. PROGRAM FLOW is from TOP to BOTTOM. If there is a need to change the flow of the program, then the place where we want the program to flow must be declared. To do this we need LABEL to address or declare this flow place. GOTO command is used to change flow. LABEL names are defined like variables. Variable definition rules are also valid for label defining. Label etiket1,etiket 2;
Type BloCK This block is used to define USER DEFINED TYPES. Any programmer can define new types by using default types of PASCAL. Type name of the type that user defined=type style; Example: type St20=string[20]; var a:st20;
OPERATORS 1)Aritmetic operators 2) Relational Operators 3) Logical Operators
Assignment operator Assign a value to a variable <name of variable>:=<value of variable>; A:=6; expression1:=expression2 OPERATOR expression3; A:=A+1; A:=A*B; If there are same variables in both sides of a equality ; then values of these variables are NOT EQUAL.
we can use logic operators in relational expressions: 0<=x<=34 X is an element in interval [0,34] (x>=0) AND (x<=34)
OPERATORS in DETAIL 1)Aritmetic operators + addition - subtraction * multiplicaiton / division 2) Relational Operators < less than > Greater than <= Less than or equal >= greater than or equal <> Not equal = equal 3) Logical Operators NOT değil OR veya AND ve Ex: (A<B) AND (C<=8) (A>(C-4)) OR (C<(NOT(B)))
mod operator (operand1) mod ( operand2) Gives an integer result of remainder after dividing OPERAND1 by OPERAND2. 3mod5=3 11mod4=3 22mod6=4 (- 13)mod5=2
div operator (operand1) div ( operand2) Gives an integer result of after dividing OPERAND1 by OPERAND2. It is used for exact division. 4div13=0 18div5=3 16div(-3)=-5
Priority order in Arithmetic Operations Parenthesis operator ( ) Arithmetic functions(e,ln ) Power DIVISION multiplication div OPERATOR of PASCAL mod OPERATOR of PASCAL ADDITION SUBTRACTION RELATIONAL expressions(<,>, ) LOGICAL expressions
INPUT OUTPUT EXPRESSIONS INPUT (READ) EXPRESSION: General usage: Read(variable1,variable2,,variablen); (READLN) EXPRESSION: (readln=readline) After getting input computer sends the cursor to the beginning of next line General usage: Readln(variable1,variable2,,variablen);
Difference between read and readln Program source code Var a, b:integer; c: real; Screen Result 12,35,-264 CURSOR WAITS HERE begin read (a,b,c); End. Var a, b:integer; c: real; 12,35,-264 CURSOR WAITS HERE in NEW LINE begin readln(a,b,c); end.
Char Type INPUT expressions char type variables allow users to enter ONE CHARACTER. If there are more than one character type variable SPACE shouldn t be given between these characters. Var a, b: char; begin read(a); read(b); End. ZX we press ENTER after Z X???? Computer takes Z as input not X. When we enter word ELGIN for b it can only process E.
STRING TYPE INPUT expressions Var a, b:string[20]; begin read(a,b); End. a and b CAN CONTAIN 20 CHARACTERS AT MOST. SO THEY TAKE MUCH SPACE FROM MEMORY. Var a, b:string[20]; begin A:= Ege University ; B:= mathematics Department ; Writeln(a); Writeln(b); End. We can assign initial (başlangıç) values to string type variables.
OUTPUT EXPRESSION 1 These are expressions that show output values on SCREEN of computer *write deyimi: General usage; Write(varibale1,variable2,,variablen); CURSOR waits on screen after showing the values in this command. It waits at right side of output expression. Var a, b: byte; top: integer; begin read(a,b); top:=a+b; write(top); end.
OUTPUT EXPRESSION 2 Instead of values of variables if anyone wants to give some information must be used. Write( Information about some data in program ); CURSOR waits on screen after showing the values in this command. It waits at right side of output expression. Program source code Var a, b: byte; Output after run process This is our first program. begin write( This is our first program. ); end.
1 Example OUTPUT EXPRESSION Program source code Output after run process Var a, b: byte; begin A:=5; B:=7; write( This is our first program. ); Write( A=,A); Write( B=,B); end. This is our first program.a=5 B=7 Cursor waits
2Example OUTPUT EXPRESSION Program source code Var a, b: byte; begin A:=5; B:=7; writeln( This is our first program. ); Writeln( A=,A); Writeln( B=,B); Writeln( A and B are=,a,b ); Writeln(a,b); end. Output after run process This is our first program. A=5 B=7 Cursor waits here. A and B are=,a,b 57
3Example OUTPUT EXPRESSION Program source code Var a,b: byte; sum: integer; begin write( Enter 1. number ); readln(a); write( Enter 2. number ); readln(b); sum:=(a+b); writeln( result=,sum); end. Output after run process Enter 1. number 25 Enter 2. number 73 result=98 Cursor waits here.
Example: Write a Pascal Program which gives the following output. First Number=5 Second Number=8 - - - - - - - - - MULT=firstnumber*secondnumber=5*8=40 Program run process stopped
Example: Write a Pascal Program which gives the following output. Var a,b,top: integer; Begin a:=5;b:=8; Write( First number=,a); Write( Second number=,b); Writeln( Mult=firstnumber*secondnumber=,a*b); Write( Program runprocess stopped. ); End.
Program p1; Uses crt; BEGIN write( Ege University ); Write( Faculty of Science ); Write( Department of Maths ); Write( Computer Sciences II course ); Readln; END.
Program p1; Uses crt; BEGIN writeln( Ege University ); Writeln( Faculty of Science ); Writeln( Department of Maths ); Writeln( Computer Sciences II course ); Readln; END.
Program p2; Uses crt; Var a,b:integer; BEGIN clrscr; a:=5; b:=7; Writeln( a+b is, a+b); Writeln(a, +,b, =, a+b); Writeln( a*b is, a*b); Writeln(a, *,b, =, a*b); Readln; END.
Program p3; Uses crt; Var a,b,sum,mult:integer; BEGIN clrscr; Writeln( Enter first number= ); readln(a); Writeln( Enter second number= ); readln(b); sum:=a+b; Writeln( Sum of, a, and,b, is, sum); mult:=a*b; Writeln( Multiplication of, a, and,b, is, mult); Readln; END.
Program p4; Uses crt; Var a,b:char; BEGIN clrscr; Writeln( Enter first name= ); readln(a); Writeln( Enter surname= ); readln(b); Writeln( The first charcter is =, a); Writeln( The second charcter is =, b); Readln; END.
Program p5; Uses crt; Var a,b:string[5]; BEGIN clrscr; Write( Enter first name= ); readln(a); Write( Enter surname= ); readln(b); Writeln( first name is =, a); Writeln( surname is =, b); Readln; END.
Program p5; Uses crt; Var a:string[10]; b:string[20]; BEGIN clrscr; Write( Enter first name= ); readln(a); Write( Enter surname= ); readln(b); Writeln( first name is =, a); Writeln( surname is =, b); Readln; END.