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

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS 117 Spring 2002 Basic Program Elements Chapter 2.
Program Design and Development
Data types and variables
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Introduction to C Programming
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Lecture 13 Midterm overview When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it: this is.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
INFORMATION TECHNOLOGY CSEC CXC 10/25/ PASCAL is a programming language named after the 17th century mathematician Blaise Pascal. Pascal provides.
C++ Programming: Basic Elements of C++.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to Pascal The Basics of Program writing.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Visual Basic Programming
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Pascal Course Spring Introduction Designed: 1968/9 by Niklaus Wirth Published: 1970 Imperative, structural, procedural Static and strong.
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
PASCAL PROGRAMMING.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Chapter # 2 Part 2 Programs And data
Definition of the Programming Language CPRL
Chapter Topics The Basics of a C++ Program Data Types
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BASIC ELEMENTS OF A COMPUTER PROGRAM
The CONST definition CONST Pi = , City = ‘New York’;
Basic Elements of C++.
The Selection Structure
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
A Very Brief Overview of Pascal
Basic Elements of C++ Chapter 2.
T. Jumana Abu Shmais – AOU - Riyadh
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Introduction to C++.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Presentation transcript:

Pascal language Slides of Omar Al-Nahal

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. 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

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.

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

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

8 - Declared of Variables : Value of type Integer must be between -32,768 and 32, 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 & 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 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

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

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

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 /  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

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 : The average is : 4.25

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

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/ a) 1+2 *  11 b) /  11 c) 2 / 3 *  1/6 or 8/3 Exercises chapter 3 : 2, 3, 4, 6, 7, 9, 1 1, Constants: A constant is something that has a Fixed value. Example: Const Pi=3.14;

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 : Output

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

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

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

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 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

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

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 : Excellent Student Output

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

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.

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

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

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

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 Writeln( sqrt(2) :5 :3) ; Writeln ( sqrt(9) ) ; Writeln ( sqr(9)) ; Output E E

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.

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 Expression Round(3.8)Trunc(3.8)Sqrt(abs(-16)) Sin (pi) / 6 ) 5- Char functions: Ord, Chr, Upcase - ASCII Code numbers: (Look in appendix B)

 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

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

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- Ch

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.

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 Program Find Sum Exercises chapter 7 : 1, 4, 8

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.

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.

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.