Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.

Similar presentations


Presentation on theme: "Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal."— Presentation transcript:

1 Pascal language Slides of Omar Al-Nahal

2 Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal Digits. - Special Characters. 2. Pascal Reserved Words: Program, Begin, Array, Var, Case, If, Else, End, For, File, Function, Procedure, Record, Mod, Const, Repeat, Then,.........., ets.

3 3. Identifiers : - Standard Identifiers - Use Defined Identifiers  Standard Constants. * Subrange.  Standard Data Types. * Enumerated.  Standard Files.  Standard Functions.  Standard Procedures. 4. Numeric Constants: - Integer Numbers - Integer Numbers - Real Numbers - Real Numbers - E- Notation - E- Notation

4 5. Variables : - Integer - Integer - Real - Real - Boolean - Boolean - Char - Char 6. Comment Statement (* *) Or { }. FLOWCHARTS - Program Flowcharts: - System Flowcharts - Simple Sequential Flowcharts. - Simple Sequential Flowcharts. - Branched Flowcharts. - Branched Flowcharts. - Simple – Loop Flowcharts. - Simple – Loop Flowcharts. - Multi – Loop Flowcharts. - Multi – Loop Flowcharts.

5 Program Execution Program On Paper Correct Errors Checking Error Editor Source Program Compiler start Yes No Machine Code Run Program

6 Flowcharts - Programs Question: Finds the area and circumference of a circle? Start Read R Pi=3.14 A=Pi(R) 2 C= 2 Pi(R) Print R, A, C Stop

7 8 - Declared of Variables : Value of type Integer must be between -32,768 and 32,767. - Assignment Statements: An assignment statement assigns a value to a variable. For exam: Feet:=6 ; { Variable:= Expression;} - Writeln Statements: a writeln statement is used to send information to the screen or printer. For Exam: writeln( ‘ Hello ’ );

8 Punctuation & Style  Period: The period after the last end in a program is used to mark the end of the entire program.  Semicolons: A semicolons is used to separate two consecutive statement in a Pascal Program.  Commas: Commas are used to separate items in a list, Such as the variables of one type in a declaration statement.  Reserved words versus standard identifiers: Keywords are words with a built-in meaning in Pascal. There are two kinds of keywords: 1- Reserved words. 2- Standard identifiers  Comments: Comments are simply message to who ever is reading the program.

9 Memory Cells Changing the value of a variable: The memory cell of a variable will hold just the current value of the variable. Example: What will be output by the following program ? Program drill; Var x : integer; Begin X:=6; X:=8; Writeln( ‘ x is ‘, x) ; End. X is 8 Output 6868 Memory

10 Interactive Programs  Readln Statement.  Prompts.  Input List.  Read statement. Example: Program feet to inches; Var feet, inches : integer; Begin Write( ‘ Enter number of feet: ’ ) ; Readln (feet); Inches := feet * 12; Writeln (inches, ‘ inches ‘ ); End. Enter number of feet: 6 72 inches Output Exercises chapter1 : 2, 3, 4, 6

11 The Elements of Pascal The Elements of Pascal 1. Syntax Errors: If you try to compile a program that contains one or more syntax error. Program dril1; Var n: interger; Begin Writeln( ‘ Hello ‘ ) Writeln( ‘ How are you ) ; n:=2; Writeln( ‘ N Equals ‘, n); End. Misspelled Keyword Integer Missing Semicolon Missing quote mark Not a syntax error

12 2- The Real Data Type: A variable of type real may be assigned integer values, but it also may be assigned fractional values or large value like. 4.3 8/3 -52.1 4375200  Exponential Notation : Reading Page 36.  Output in ordinary decimal form : Output in exponential form is hard for human's to read.  The Real Data Type Writeln( Real Expr : Width : P ) ; X:=78.291; Writeln(X:0:1); Writeln(x:0:2); Writeln(X:0:0); Example : Output 78.3 78.29 78

13 Example: Example: What will be output by the following program ? Program avg01; Var a, b, c, d, Avg : Real ; Begin Writeln( ‘ Input Real Value : ‘ ) ; Readln( a, b, c, d ); Avg:= ( ( a + b + c+ d ) / 4); Writeln( ‘ The average is : ‘, Avg : 0 : 2); Readln; End. Output Input Real Value : 4 5 3 5 The average is : 4.25

14 3- Additional Integer Data Types: The most useful additional integer type is longint, which can handle integer up to 2.197.483.647 as opposed to 32.767 for the data type integer. For Example: Profit $ 4000000 Output Var profit : Longint;. Profit:=2000 * 2000 ; Writeln ( ‘ Profit $ ‘, Profit); The Five Integer Data Types: Data TypesRange Byte Shorting Integer Word Longint 0 to 255 - 128 to 127 - 32,768 to 32,767 0 to 65,535 -2,147,483,648 to 2,147,483,648

15 4- Numerical Operators: - The operator +, -, *, Pascal use + for addition, - for subtraction, and * for multiplication with both integer and real variables. - The three division operators, Turbo Pascal contains three different division operators: Division operator : Operator Example / 9/2 = 4.5 Div 9 div 4 =2 Mod 9 mod 2=1 Example: Evaluate each of the following: 1- a) 26 mod 4 b) 26 div 4 c) 26/4 2 6 6.5 2- a) 1+2 * 3 + 4 ---  11 b) 6 + 4 /2 +3 ---  11 c) 2 / 3 * 4 ---  1/6 or 8/3 Exercises chapter 3 : 2, 3, 4, 6, 7, 9, 1 1, 13 5- Constants: A constant is something that has a Fixed value. Example: Const Pi=3.14;

16 Example: Example: Consider the following program ? Program Circle (Input, Output) ; Const pi = 3.14; Var Radius, { Input – Radius of a circle } area : Real ; { output – Area of a circle } Begin { Read the circle radius } Writeln( ‘ Enter Radius : ‘ ) ; Readln( Radius); { Find the area } Area := Pi * Radius * Radius ; { Print the area } Writeln( ‘ The area is : ‘, area : 0 : 2) ; Readln; End. Enter Radius: 7 The area is : 153.86 Output

17 Ch4 IF – Then – Else and Top Down Design  IF – Then IF Condition then Statement If age >= 18 then writeln( ‘ May Vote ‘ ) ; Relational operators: There are six relational operator: >, =, Example: Example: When the fragment : If age >= 18 then writeln ( ‘ Of age ‘ ) Writeln ( ‘ Good Luck ’ ) ; Is executed, what will be printed for each given value of age ? a) Age =25 b) age =14 c) age=18

18 2- Selecting from two alternatives  IF – Then – Else IF Condition then Statement Example : When the fragment: If Score >=60 then writeln ( ‘ You Pass ‘ ) Else Writeln ( ‘ You Fail ’ ) ; Is executed, what will be printed for each given value of score ? a) Score = 54 b) Score = 73 Else Statement You FailYou Pass

19 IF – Then – Else - Flowcharts IF – Then – Else  IF – Then IF – Then – Else Boolean Expression Execute Statement T or F Continue With program T F Execute IF-Then Statement T or F Continue With program T F Boolean Expression Execute Else Statement

20 3- Boolean Expression and operator: - Boolean Expression: a Boolean expression is something that is either True or False. - Boolean Operator : And, Or, Not, Xor  And Operator: Example: write ( ‘ Enter two Score: ‘ ); readln(score1,score2); if (score 1>= 65 ) and ( score 2>= 65 ) then Writeln( ‘ Pass ’ ) else Writeln ( ‘ Fail ’ ) ; Or, Not, Xor - Reading Page 54.  Using Procedure to Implementation top down design: Procedure Procedure Name; Begin Statement (s) ;  ( Body of procedure ) End ; Exercises chapter 4 : 2, 3, 7, 12 Page 59 Example (Smiling or Frowning Face ).

21 4 - A simple Calculation Example: program calc; USES FDELAY, CRT; var Number1,Number2, Addresult,Multresult : REAL ; begin write( ‘ Input a Number1 :') ; readln(number1) ; write( ‘ Input a Number 2: ') ; readln(number2) ; addresult :=Number1 + Number2 ; multresult :=number1 * number2 ; writeln; writeln ( ‘ number1 + number2 = ‘, addresult:0:2) ; writeln ( ‘ number1 * number2 = ‘, multresult:0:2) ; writeln; writeln; writeln; readln; end. Enter a number: 6 Enter another number: 3 Output

22 Ch12 1- Nested IF Statement Ch12 1- Nested IF Statement Example: program avg02 ; uses fdelay, crt; Const numberofexam=3 ; Var score1, score2, score3, sum, average :real; begin clrscr; writeln ( ‘ Please enter the score of the three exam : ‘ ); readln(score1, score2, score3); sum:=(score1+score2+score3); Average:= sum /numberofexam; writeln;writeln; If condition 1 Then Statement1 else If condition 2 Then Statement2 Else If condition 3 Then Statement 3 else Statement4

23 2 if (average >= 90) and (Average < 100) then writeln (' Excellent Student ‘ :42); if (average >= 80) and (Average < 90) then writeln (' VG ':42) ; if (average >= 70) and (Average < 80) then writeln (' G ':42) ; if (average >= 60) and (Average < 70) then writeln (' P ‘ :42) ; If (average < 60 ) then writeln (' F ‘ :42); Writeln ; writeln ; write ( ' Press Enter To Repeat Score.. ‘ :48); readln; end. Please enter the score of the three exam : 90 88 94 Excellent Student Output

24 2 - Case – Statement : - To avoid using complicated nested – if statement, case – Statement can be used to make the program to be clear and to reduce logical errors.  Syntax: Case Expression of Value1 : Statement1; Value2 : Statement2; Value3 : Statement3; - Value N : Statement N Else : Statement; End; 3- GOTO Statement GOTO n IF Condition Then GOTO n

25 Example: Case – Statement Exercises chapter 12 : 1, 2, 5, 18 Example: Program output_day; Var index : integer; Begin Gotoxy(28,8); Writeln ( ‘ Please input the number (1-7) ‘ ); Readln(index); Writeln;gotoxy(34,8);writeln; Case index of 1: writeln ( ‘ Today is Saturday ‘ ); 2: writeln ( ‘ Today is Sunday ‘ ); 3: writeln ( ‘ Today is Monday ‘ ); 4: writeln ( ‘ Today is Tuesday ‘ ); 5: writeln ( ‘ Today is Wednesday ’ ); 6: writeln ( ‘ Today is Thursday ‘ ); 7: writeln ( ‘ Today is Friday ‘ ); Else Writeln( ‘ Input Error ! ’ ) End; readln; End.

26 Ch5 Char & String Data Types 1.Variables of type char: - a variable of type char can store any single character value. -Character value in a program are enclose in single quote mark the same single quotes that are used for verbatim message in writeln statement. Example: In program letter grade, grade is a variable of type char. Program Lettergrade; Var grade : char; Begin Write ( ‘ Enter your grade : ’ ); Readln (grade); Writeln ( ‘ You received the grade of ’, grade); Readln; end. Enter your grade Enter your grade : B Output

27 22 2. Variables of type String: 0 -- 255 0 -- 255Example: Var Last_name: string ; The assignment statement : Last_name := ‘ Pascal ‘ ; lacsaP 6 – Memory cells0.7lacsaP 10 – Memory cells

28 The Length Function - The turbo length function returns the length of the current value stored in a string variable. - The turbo length function returns the length of the current value stored in a string variable.Example: Program how_long ; Var message : string [100] ; Begin Message := ‘ So Long ; Writeln ( Length (Message)) ; ABCDEFGH now nowPASCAL Output 3- Formatting output using zone width specifies: 1)Formatting String 2) Format Integer 3) Format Real Example: writeln ( ‘ abcdefgh ’ ); Writeln( ‘ Now ‘ :6 ); Writeln( ‘ Pascal ‘ :6 ); Output How_long is 7 How_long is 7

29 4- Built-in arithmetic functions: - The turbo Pascal also provides a number of purely arithmetic built-in functions.  SQRT and SQR Functions: Question: Give the output for the following fragment? Hint : to three decimal places is 1.414 Writeln( sqrt(2) :5 :3) ; Writeln ( sqrt(9) ) ; Writeln ( sqr(9)) ; Output 1.414 1.414 3.00000000 E+00 3.00000000 E+00 81 81

30 Example:Example:  C 2 = A 2 + B 2  C 2 = A 2 + B 2 Program square; Var a,b,c : real ; Begin Write ( ‘ Enter the length of the two legs ‘ ); Readln (a,b) ; C:= sqrt (a * a + b * B ) ; {OR} C:= sqrt ( Sqr (a) + Sqr (B) ) ; Writeln( ‘ C is ‘, C :2:2) ; Readln; End.

31 4- 2 :- 4- 2 Arithmetic Functions :- -Abs (x), arctan(x), cos (x), exp(x), ln(x), -Random, round(x), sin(x), sqr(x), sqrt(x), trunc(x). Example:- Its Value Expression434.0000.5Round(3.8)Trunc(3.8)Sqrt(abs(-16)) Sin (pi) / 6 ) 5- Char functions: Ord, Chr, Upcase - ASCII Code numbers: (Look in appendix B)

32  Ord, Chr Functions: -The Ord functions is applied to a char value and returns the characters ASCII number. For Example: Ord ( ‘ R ’ ) equals 82, Ord ( ‘ A ’ ) =65. Chr (78) equals N, Chr ( 97) = a. Upcase Functions : Upcase Functions : the upcase functions when applied to letter with return the upper case of the letter. For Example: Upcase ( ‘ g ’ ) returns G. Upcase ( ‘ G ’ ) returns G. Upcase ( ‘ 4 ’ ) returns 4. Exercises chapter 5 : 2, 3, 4, 7, 8

33 Ch6 For Loops: For … To & For … Downto (Reserved words) The For statement causes the (or several statement enclosed by begin and end ) following DO to be execute once for each value of the control variable in the range from the to the. Syntax: For …. To statement For := to Do Example: var count: integer; begin For count := 1 to 5 do Writeln ( ‘ Hello of CIT ’ ) ; end. For …. downto statement For := Downto Do

34 2- Processing input groups of data -Counting and summing variables : The following questions and examples show how counting and summing variables are implemented. Question:- what is the output for each of the following fragments : a) count:=0 b) sum:=0; count:=count+1; sum:= sum+8; count:=count+1; sum:= sum+3; count:=count+1; sum:= sum+11; Writeln (count); Writeln (sum); Output: 3 22 Output: Ex- Ch6 1 4 16

35 Ch 7 While Loop & Repeat - Until - While Do : a while statement contains an expression that controls the repeated execution of a statement. Syntax : While Do Example: var count, Limit : integer; Begin Count := 0; Limit:=15; While count <= limit do Begin Count := count+1; Write (count:3) End; end.

36 Repeat - Until - Repeat … Until : The statement between Repeat and Until are execute in sequence until, at the end of the sequence, the Boolean expression is TRUE Syntax : Repeat ; …… ; Until ; Example: N:= 7; Repeat Writeln (n); N:= n-5 ; Writeln ( ‘ Hi ‘, n ) Until N< 0 Controlled Loops Page 109 - Program Find Sum Exercises chapter 7 : 1, 4, 8

37 Example: For Loop ---------- * ) For Loop ( *------ program prog008 ; uses crt ; var I:integer ; sum,sum_even,sum_odd:integer ; begin sum:=0 ; sum_even:=0 ; sum_odd:=0 ; for I:=1 to 5 do begin writeln('I is ',I ) ; sum:=sum+I ; if i mod 2 = 0 then sum_even:=sum_even+I else sum_odd:= sum_odd+I ; End ; writeln('The sum of all number is ',sum) ; writeln('The sum of even number is ',sum_even ); writeln('The sum of odd number is ',sum_odd);readln; end.

38 Example: While ---------- * ) While Loop ( *------ program prog008 ; uses crt ; var I:integer ; sum,sum_even,sum_odd:integer ; begin sum:=0 ; sum_even:=0 ; sum_odd:=0 ; I:=1; While I <= 6 do Begin Writeln( ‘ I is ‘, I ) ; Sum:= sum + I ; If I mod 2 = 0 then sum_even:= sum_even+I else sum_odd:= sum_odd+I; Inc (i); end; writeln('The sum of all number is ',sum) ; writeln('The sum of even number is ',sum_even ); writeln('The sum of odd number is ',sum_odd);readln; end.

39 Example: Repeat ---------- * ) Repeat Loop ( *------ program prog008 ; uses crt ; var I:integer ; sum,sum_even,sum_odd:integer ; begin sum:=0 ; sum_even:=0 ; sum_odd:=0 ; i:= 1; Repeat Writeln( ‘ I is ‘, I ); Sum := sum + I ; If I mod 2 = 0 then sum_even:=sum_even+I else sum_odd:= sum_odd+I ; End ; inc ( I ) ; Until I > 6 ; writeln('The sum of all number is ',sum) ; writeln('The sum of even number is ',sum_even ); writeln('The sum of odd number is ',sum_odd);readln; end.


Download ppt "Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal."

Similar presentations


Ads by Google