110 likes | 119 Views
Learn about user-defined and standard identifiers, variables, arithmetic operators, relational operators, and more in Pascal programming.
E N D
CH 2 – INTRODUCTION TO PASCAL PROGRAMMING User-Defined Identifier –defined by users. Test Score Test _Score TestScore2 testscore Test Score TestScore% 2TestScore if OK NOT OK Reserved words –words having special meanings in Pascal. program CheckPass; var TestScore, ExamScore:integer; FinalScore:real; Status :string; beginwrite(‘Enter the Test score:’);readln(Testscore); write(‘Enter the Examination score:’); readln(Examscore); Finalscore:=TestScore* 0.3 +Examscore* 0.7; ifFinalscore >= 50then Status:= ‘Pass’ else Status:= ‘FAIL’; writeln(Status) end. Standard Identifier –predefined by Pascal.
:=assigns values to variables program CheckPass; var TestScore ,ExamScore:integer; FinalScore:real; Status:string; beginwrite(‘Enter the Test score:’); readln(Testscore); write(‘Enter the Examination score:’); readln(Examscore); Finalscore:=TestScore*0.3+Examscore*0.7; if Finalscore >=50 then Status:= ‘Pass’ else Status:= ‘FAIL’; writeln(‘The result is’,Status) end. :separate variables/data type identifier ,separate items in a list +,-, *,/arithmetic operators =,<>,<,>,>=,<= relational operators ()enclose arguments of procedures, functions .enclose arguments of procedures, functions ; separate statements, added at the end except then, else , end until ‘’enclose characters or string
program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status: string; begin(* Get Score *) write(‘Enter the Test score:’); readln(Testscore); write(‘Enter the Examination score:’); readln(Examscore); (* Calculate the final Score *) Finalscore := TestScore * 0.3 + Examscore * 0.7; (* Check Status *) if Finalscore >= 50 then Status := ‘Pass’ else Status := ‘FAIL’; (*Display Status *) writeln(Status) end. • Comments (* *) • remarks – • improve readability • Ignored by computers during compilation or executed
BASIC STRUCTURE OF A PASCAL PROGRAM PROGRAM HEADING PROGRAM AreaCircle; CONSTANT DECLARATION CONST PI = 3.1416; VARIABLE DECLARATION VAR R, A : real; PROCEDURE AREA; BEGIN A := PI * R * R; END; PROCEDURE DECLARATION BEGIN WRITELN(‘Enter Radius’); READLN(R); AREA; WRITELN(‘The circle area is ’, A:0:2); END. PROGRAM BODY
BASIC STRUCTURE OF A PASCAL PROGRAM PROGRAM HEADING program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; VARIABLE DECLARATION begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter the Examination score:’); readln(Examscore); Finalscore := TestScore * 0.3 + Examscore * 0.7; if Finalscore >= 50 then Status := ‘Pass’ else Status := ‘FAIL’; writeln(Status) end. PROGRAM BODY
32 21 45 ………. 54 23 TestScore 2.3 BASIC DATA TYPE IN PASCAL INTEGER -32728 , -32727, …. -2, -1, 0, 1, 2, ……… , 32726, 32727 123.45 0.00123 -12345.0 -0.00000123 REAL ‘E’ ‘A’ ‘1’, ‘2’ ‘%’ ‘□’ CHAR STRING ‘PASS’ ‘FAIL’ ‘Enter Test Score’ ’45’ ‘Our Lady’’s College’ ‘’ BOOLEAN TRUE FALSE ARRAY
PROGRAM STUDENTRECORD; VAR AGE: INTEGER; HEIGHT : REAL; CONDUCT : CHAR; NAME , CLASSNO, ADDRESS : STRING; TESTS : ARRAY[1..6] OF INTEGER; BEGIN AGE := 15; HEIGHT := 5.7; CONDUCT := ‘B’; NAME := ‘PETER CHAN’ ; CLASSNO := ‘4C’ ; ADDRESS := ’4/F, 23, NATHEN ROAD, MONGKOK’ ; TESTS[1] := 56; TESTS[2] :=78; TESTS[3] := 45; TESTS[4] := 67; END.
program CheckPass; var TestScore1, TestScore2, TestScore3, , TestScore4 : integer; Status1, Status2, Status3, Status4: string; begin readln(Testscore1); readln(Testscore2); readln(Testscore3); readln(Testscore4); if Testscore1 >= 50 then Status1 := ‘pass’ else Status1 := ‘fail’; if Testscore2 >= 50 then Status2 := ‘pass’ else Status2 := ‘fail’; if Testscore3 >= 50 then Status3 := ‘pass’ else Status3 := ‘fail’; if Testscore4 >= 50 then Status4 := ‘pass’ else Status4 := ‘fail’; end. WHY ARRAY? • Supposed five students in the class. • need to declare 5 variables to store their test scores! • And 5 input statements to read their scores! • And 5 IF statements to test their status!
program CheckPass; var TestScore: array[1..4] of integer; Status: array[1..4] of string; I : integer; begin for I := 1 to 4 do readln(Testscore[I]) for I := 1 to 4 do if Testscore[I]>= 50 then Status[I] := ‘pass’ else Status[I] := ‘fail’; end. 21 45 54 23 TestScore fail fail pass fail Status
DECLARE VARIABLE OF ARRAY DATA TYPE 21 45 54 23 TestScore fail fail pass fail Status 5.3 6.2 5.9 5.1 Studentheight B C A D Grade 34 20 12 ……. 3 LetterCount VARIABLE NAME: array[LOWER BOUND .. UPPER BOUND ]of <element type>; Ex 1 TestScore :array[1..4]ofinteger; Ex 2 Status :array[1..4]ofstring; Ex 3 Studentheight :array[1..4]ofreal; Ex 4 Grade :array[1..4]ofchar; LetterCount :array[‘A’..’Z’]ofinteger; Ex 5