Download presentation
Presentation is loading. Please wait.
Published byArline Richardson Modified over 9 years ago
2
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
3
Copyright 1999 by Larry Fuhrer. Pascal Begin with an Algorithm A set of instructions that will lead to a solution Instructions take the form of natural language Sometimes called pseudo-code May be a diagram - flowchart
4
Copyright 1999 by Larry Fuhrer. Pascal To reach a solution... Problem Solving –Define the problem –Design the algorithm –Test the solution Implement the solution –Code in Pascal –Compile –Test the solution
5
Copyright 1999 by Larry Fuhrer. Pascal Variables – objects that hold data Examples: –X Y Sum N1 N2 Rate Time Bonus Compilers assign memory locations to variables – that memory contains the data Sample... –var N1, N2, Sum: integer;
6
Copyright 1999 by Larry Fuhrer. Pascal Variables have names called identifiers –Comprised of...a string consisting of letters, numbers or an underscore (_) –Must begin with letter or underscore Reserved words – a type of identifier –e g: begin end var program –Reserved words may only be used as defined by Turbo Pascal
7
Copyright 1999 by Larry Fuhrer. Pascal Body of the program begin – starts the body which contains statements end. – ends the body of the program Statements – provide the instructions that comprise the body of the program
8
Copyright 1999 by Larry Fuhrer. Pascal The assignment statement Incorporates the assignment operator := May be read as “gets the value” Example... –Sum := N1 + N2 variable on the left expression on the right
9
Copyright 1999 by Larry Fuhrer. Pascal Program layout Starts with a program heading –program Getting Started; List the variables –var N1, N2, Sum: integer; Body – bounded by –begin end. Spacing – identifiers or numbers must be separated by a space or punctuation mark Line breaks – very flexible
10
Copyright 1999 by Larry Fuhrer. Pascal Data types – a category of data Numbers – Integer or Real Integer is a whole number Real is a whole number plus mantissa (fraction) A computation may result in a scientific notation – 9.34E13 Operations include *, -, *, /, div, mod (div divides the first number by the second. mod tells the remainder. Type must be integer.) –Continue...
11
Copyright 1999 by Larry Fuhrer. Pascal Note... Combine type Real and Integer and you get REAL Dividing (/) yields REAL Operations +, *, - with Integer yield Integer A minus (-) preceding a number does not alter its type
12
Copyright 1999 by Larry Fuhrer. Pascal Text or letter types char -- holds any ONE keyboard character string -- a series of characters – should be limited FirstName, LastName: string [25]
13
Copyright 1999 by Larry Fuhrer. Pascal Input and Output To output to the screen we use – writeln – to write and move to a new line write -- writes on the same line –e g: writeln: (‘This begins our answer.’) Numbers in scientific notation can be limited e g: Cost: 6:2 – six space for the answer with two digits to the right of the decimal –Continue...
14
Copyright 1999 by Larry Fuhrer. Pascal Input data by... readln – reads and goes to next line read – continues on the same line A readln without instruction stops the program because data entry is expected. Generally, writeln and readln will be preferred in a program.
15
Copyright 1999 by Larry Fuhrer. Pascal Comments Explanatory notes make a program understandable. e g: {This program calculates the interest on a loan.} Punctuation must be complete { }
16
Copyright 1999 by Larry Fuhrer. Pascal Constants Programs use constant values like interest rates or days in a year. In Pascal constants need to be declared – const –e g: const Year = 365; InterestRate =.075;
17
Copyright 1999 by Larry Fuhrer. Pascal Summary... Start with an algorithm Use meaningful variable names Declare data type and double check Declare constants with meaningful names Observe ( ) and order of precedence in mathematical expressions Initialize variables Add helpful comments Use prompt lines when calling for data entry
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.