pascal language slides of omar al-nahal. components of pascal language components of pascal language...
TRANSCRIPT
Pascal languagePascal language
Slides of Omar Al-Nahal
Components of Pascal LanguageComponents of Pascal Language Components of Pascal LanguageComponents of Pascal Language
1. Pascal Character set:1. Pascal Character set:
- English Letters.
- Decimal Digits.
- Special Characters.
2. Pascal Reserved Words:2. Pascal Reserved Words:
Program , Begin , Array , Var , Case , If , Else , End , For , File, Function , Procedure, Record, Mod, Const , Repeat , Then , .......... , ets.
5. Variables :5. Variables : - - Integer Integer
- Real - Real
- Boolean- Boolean
- Char- Char
6. Comment Statement (* *) Or { }.6. Comment Statement (* *) Or { }.
FLOWCHARTSFLOWCHARTS
- - Program Flowcharts: - System FlowchartsProgram 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.
Program ExecutionProgram Execution
Program On Paper
Correct Errors
Checking Error
EditorSource
Program
Compiler
start
Yes
No
Machine Code
Run Program
Flowcharts - ProgramsFlowcharts - Programs
Question: Finds the area and circumference of a circle?
Start
ReadR
Pi=3.14
A=Pi(R)2
C= 2 Pi(R)
Print R, A, C
Stop
88- 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’);
Punctuation & StylePunctuation & 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.
Memory Cells 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 8Output
68
Memory
Interactive ProgramsInteractive Programs Readln Statement. Prompts.Input List. Read statement.
Example:
Program feet to inches;Var feet , inches : integer;BeginWrite(‘ Enter number of feet: ’ ) ;Readln (feet);Inches := feet * 12;Writeln (inches , ‘ inches ‘ );End.
Enter number of feet: 672 inches
Output
Exercises chapter1: 2 , 3 , 4 , 6
The Elements of PascalThe Elements of Pascal The Elements of PascalThe Elements of Pascal 1. Syntax Errors: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
2- The Real Data Type: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
Example: 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
3- Additional Integer Data Types: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 Types
Range
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
4- Numerical Operators: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;
Example: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
Ch4 Ch4 IF – Then – Else and Top Down DesignIF – 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
2- Selecting from two alternatives2- 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 Fail You Pass
IF – Then – Else - FlowchartsIF – Then – Else - Flowcharts
IF – Then IF – Then – ElseIF – Then – Else
Boolean Expression
ExecuteStatement
T or F
Continue With program
T
FExecuteIF-Then
Statement
T or F
Continue With program
TF
Boolean Expression
ExecuteElse
Statement
3- Boolean Expression and operator: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 ).
4 - A simple Calculation 4 - A simple Calculation Example:program calc;USES FDELAY , CRT;varNumber1 ,Number2 ,Addresult ,Multresult : REAL ;beginwrite( ‘ 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
Ch12 Ch12 1- Nested IF Statement 1- Nested IF Statement
Example:program avg02 ;uses fdelay , crt;Const numberofexam=3 ;Var score1 , score2 , score3 , sum , average :real;beginclrscr;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
22if (average >= 90) and (Average < 100) thenwriteln (' Excellent Student ‘ :42);
if (average >= 80) and (Average < 90) thenwriteln (' VG ':42) ;
if (average >= 70) and (Average < 80) thenwriteln (' G ':42) ;
if (average >= 60) and (Average < 70) thenwriteln (' P ‘ :42) ;
If (average < 60 ) thenwriteln (' 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
2 - Case – Statement :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 Statement3- GOTO StatementGOTO n
IF Condition Then GOTO n
Example:Case – StatementCase – 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.
Ch5 Ch5 Char & String Data Types Char & String Data Types Ch5 Ch5 Char & String Data Types Char & String Data Types
1.1.Variables of type char:Variables of type char:- a variable of type char can store any single character - a variable of type char can store any single character
value.value.
- Character value in a program are enclose in single quote Character value in a program are enclose in single quote mark the same single quotes that are used for verbatim mark the same single quotes that are used for verbatim message in writeln statement.message in writeln statement.
Example:Example:
In program letter grade , grade is a variable of type char.In program letter grade , grade is a variable of type char.
ProgramProgram Lettergrade; Lettergrade;
Var grade Var grade : char: char; ;
BeginBegin
Write (‘ Enter your grade :’);Write (‘ Enter your grade :’);
Readln (grade);Readln (grade);
Writeln (‘ You received the grade of ’ , grade); Readln; end. Writeln (‘ You received the grade of ’ , grade); Readln; end.
Enter your gradeEnter your grade :B
Output
2222
2. Variables of type String:2. Variables of type String: 0 -- 255 0 -- 255
Example:Example:Var Last_name: string ;Var Last_name: string ;
The assignment statement :The assignment statement :
Last_name := ‘ Pascal ‘ ;Last_name := ‘ Pascal ‘ ;
PPaassccaall 6 – Memory cells
PPaassccaall77..00
10 – Memory cells
The Length Function The Length Function The Length Function The Length Function - The turbo length function returns the length of the - The turbo length function returns the length of the
current value stored in a string variable.current value stored in a string variable.
Example:Example:
Program how_long ;Program how_long ;
Var message : string [100] ;Var message : string [100] ;
Begin Begin
Message := ‘ So Long ;Message := ‘ So Long ;
Writeln ( Length (Message)) ;Writeln ( Length (Message)) ;
ABCDEFGHABCDEFGH nownowPASCALPASCAL
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 7How_long is 7
4- Built-in arithmetic functions: 4- Built-in arithmetic functions: 4- Built-in arithmetic functions: 4- Built-in arithmetic functions: - The turbo Pascal also provides a number of purely - The turbo Pascal also provides a number of purely
arithmetic built-in functions.arithmetic built-in functions.
SQRT and SQR Functions:SQRT and SQR Functions:
Question: Question: Give the output for the following Give the output for the following fragment?fragment?
Hint : to three decimal places is 1.414 Hint : to three decimal places is 1.414 2Writeln( sqrt(2) :5 :3) ;
Writeln ( sqrt(9) ) ;
Writeln ( sqr(9)) ;
Output
1.4141.414 3.00000000 3.00000000 E+00E+00 8181
Example:Example:Example:Example:
CC22 = A = A22 + B + B22
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.
4- 2 4- 2 Arithmetic Functions:-:-4- 2 4- 2 Arithmetic Functions:-:-
- Abs (x) , arctan(x) , cos (x) , exp(x) , ln(x) , Abs (x) , arctan(x) , cos (x) , exp(x) , ln(x) ,
- Random , round(x) , sin(x) , sqr(x) , sqrt(x) , Random , round(x) , sin(x) , sqr(x) , sqrt(x) , trunc(x).trunc(x).
Example:-
ExpressionExpressionIts ValueIts Value
Round(3.8)Round(3.8)
Trunc(3.8)Trunc(3.8)
Sqrt(abs(-16))Sqrt(abs(-16))
Sin (pi) / 6 )Sin (pi) / 6 )
44
33
4.0004.000
0.50.5
5- Char functions:Ord , Chr , Upcase
- ASCII Code numbers: (Look in appendix B)
Ord , Chr Functions: Ord , Chr Functions: Ord , Chr Functions: 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 FunctionsUpcase 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
Ch6Ch6 For Loops:For Loops: Ch6Ch6 For Loops:For Loops: For… To & For … Downto (Reserved words)
The For statement causes the <Statement> (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 <initial values > to the <final value>. Syntax:
For …. To statement
For <control variable> := <initial value> to <final value > Do <Statement>
Example: var count: integer; begin
For count := 1 to 5 do
Writeln (‘ Hello of CIT ’) ; end.For …. downto statement For <control variable> := <initial value> Downto <final value > Do <Statement>
2- Processing input groups of data2- Processing input groups of data 2- Processing input groups of data2- 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 22Output:
Ex- Ch6
14
16
Ch 7Ch 7 While Loop & Repeat - Until While Loop & Repeat - UntilCh 7Ch 7 While Loop & Repeat - Until While Loop & Repeat - Until -While Do : a while statement contains an expression
that controls the repeated execution of a statement.
Syntax : While <Boolean – Expression > Do <Statement>
Example: var count , Limit : integer;
Begin
Count := 0;
Limit:=15;
While count <= limit do
Begin
Count := count+1;
Write (count:3)
End; end.
Repeat - UntilRepeat - UntilRepeat - UntilRepeat - Until -Repeat … Until : The statement between Repeat
and Until are execute in sequence until , at the end of the sequence , the Boolean expression is TRUESyntax :
Repeat < statement> ; < statement> ; …… < statement> ;Until < Boolean – expression >;Example:N:= 7;Repeat Writeln (n);N:= n-5 ;Writeln (‘ Hi ‘ , n ) Until N< 0
Controlled LoopsPage 109 -
Program Find Sum
Exercises chapter 7 :
1 , 4 , 8
Example: For LoopExample: For LoopExample: For LoopExample: For Loop---------- * ) For Loop ( *------ program prog008 ;uses crt ;varI:integer ;sum,sum_even,sum_odd:integer ;beginsum:=0 ;sum_even:=0 ;sum_odd:=0 ;for I:=1 to 5 dobeginwriteln('I is ',I ) ;sum:=sum+I ;if i mod 2 = 0 then sum_even:=sum_even+Ielse 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.
Example: WhileExample: WhileExample: WhileExample: While---------- * ) While Loop ( *------ program prog008 ;uses crt ;varI:integer ;sum,sum_even,sum_odd:integer ;beginsum:=0 ;sum_even:=0 ;sum_odd:=0 ;I:=1;While I <= 6 doBeginWriteln(‘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.
Example: Repeat Example: Repeat Example: Repeat Example: Repeat ---------- * ) Repeat Loop ( *------ program prog008 ;uses crt ;varI:integer ;sum,sum_even,sum_odd:integer ;beginsum:=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+Ielse 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.