1 / 74

Learn Pascal

Learn Pascal. Content. Backgournd Basics Input/Output Program Flow Data Types Subroutines. About Pascal. Origins: ALGOL Inventor: Dr. Niklaus Wirth Date of Birth: 1971 Feature: Simple, Precise, Procedure-oriented Classic Compilers: Turbo Pascal, Borland Pascal, Free Pascal

Download Presentation

Learn Pascal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Learn Pascal

  2. Content • Backgournd • Basics • Input/Output • Program Flow • Data Types • Subroutines

  3. About Pascal • Origins: ALGOL • Inventor: Dr. Niklaus Wirth • Date of Birth: 1971 • Feature: Simple, Precise, Procedure-oriented • Classic Compilers: Turbo Pascal, Borland Pascal, Free Pascal • Extension: Object Pascal, Delphi

  4. First Pascal Program • program Hello; • begin • WriteLn('Hello, world!'); • end.

  5. Program Structure • program ProgramName; • const • { Constant Declarations } • type • { Type Declarations } • var • { Variable Declarations }

  6. Program Structure • { Subroutine definitions } • begin • { Main program} • end.

  7. Comments • { Comment } • (* Comment *) • // Line Comment, New in Delphi & Free Pascal

  8. Identifiers • Name of programs, procedures, functions, types, constants, variables, etc. • Must begin with a letter or an underscore(_) • Can be followed by letters, digits, and underscores • NOT case-sensitive (i.e. ID=id=Id=iD)

  9. Reserved Words

  10. Types • Simple Type • String Type • Structured Type • Pointer Type • Procedural Type

  11. Simple Types • Ordinary Types • Integer Types • Char Types • Boolean Types • Real Types

  12. Integer Types

  13. Integer Types (Extension)

  14. Real Types

  15. Constants • const • Identifier = value; • // Cannot be modified • Identifier : Type = value; • // Can be modified

  16. Constants • Example: • const • Name = 'Han Wentao'; // string • Message = 'That''s OK.'; // string • FirstLetter = 'a'; // Char • NewLine = #10; // Char • Year = 2003; // Integer • Pi = 3.141592653589793; // Real • IsInDebugMode = True; // Boolean • a : Real = 12; // Typed Real

  17. Variables • var • Identifier : Type; • // Can be modified • Example: • var • i, j, Count : Integer; • FileName : string;

  18. Assignment • Symbol: := • Format: • Variable := expression; • Example: • i := $FF; • Count := 0; • FileName := 'berry10.out';

  19. Operators • Arithmetic Operators (+, -, *, /, mod, div) • Logical Operators (not, and, or, xor, shl, shr) • Boolean Operators (not, and, or, xor) • Relation Operators (=, <>, <, >, <=, >=)

  20. Assignment & Operators Examples • i := (123 + 456) * 789; • q1 := (1 + 10) div 3; // q1=3,Integer • q2 := (1 + 10) / 3; // q2=3.666667 • Value := 123; • Radix := 16; • Remainder := Value mod Radix; • // Remainder=11

  21. Standard Routine

  22. Standard Math Routine

  23. Punctuation & Indentation • You MUST have a semicolon(;) following: • the program heading • each constant definition • each variable declaration • almost all statements • Indentation is not necessary but useful. It makes your program readable.

  24. Program Task 1 • Input 3 real numbers, a, b, c, from keyboard, print out the 2 roots of the equation ax2+bx+c=0 on the screen. It is guaranteed that the roots must exist. • Sample Input • 1 -3 2 • Sample Output • 2 1

  25. My Solution for Task 1 • program Task1; • var • a, b, c, Delta: Real; • begin • Read(a, b, c); • Delta := Sqr(b) - 4 * a * c; • WriteLn((-b + Sqrt(Delta)) / (2 * a)); • WriteLn((-b - Sqrt(Delta)) / (2 * a)); • end.

  26. Input • Read(<Variable1>, <Variable2>, …); • // does not skip to the next line unless necessary • ReadLn(<Variable1>, <Variable2>, …); • // just a Read procedure that skips to the next line at the end of reading

  27. Input Example

  28. Output • Write(<Expression1>, <Expression2>, …); • WriteLn(<Expression1>, <Expression2>, …); • // skips to the next line when done

  29. Formatting Output • <Expression> : <Field Width> • for reals: • <Real Expression> : <Field Width> : <Decimal Field Width>

  30. Output Example • Write('Hi' : 10, 5 : 4, 5673 : 2); • ********Hi***55673 • Write(573549.56792 : 20 : 2); • ***********573549.57 • Write(123.345 : 0 : 2); • 123.35 • // * represents a space

  31. Text Files • Declaration: • var • FileVar : Text;

  32. Access a File • Assign(<FileVar>, 'File Path & Name'); • for input: Reset(<FileVar>); • for output: Rewrite(<FileVar>); • and DO NOT forget: • Close(<FileVar>); • at last!

  33. Read and Write File • Read/ReadLn(<FileVar>, <Variable1>, <Variable2>, …); • Write/WriteLn(<FileVar>, <Expression1>, <Expression2>, …);

  34. A Shortcut • for input: • Assign(Input, 'File Path & Name'); • Reset(Input); • for output: • Assign(Output, 'File Path & Name'); • Rewrite(Output); • … • Close(Output); // DO NOT FORGET

  35. Test End of Line & End of File • Eoln(FileVar : Text) : Boolean; • Eof(FileVar : Text) : Boolean;

  36. Program Task 2 • Read an angle from file trigono.in in degrees. Write the function sin, cos, tan, cot, sec and csc of the angle to the file trigono.out, rounded to 4 decimals. It is guaranteed that the angle is valid.

  37. Program Task 2 • Sample Input (trigono.in): • 30 • Sample Output (trigono.out): • 0.5000 • 0.8660 • 0.5774 • 1.7321 • 1.1547 • 2.0000

  38. My Solution for Task 2 • program TrigonometricFunctions; • const • InputFileName = 'trigono.in'; • OutputFileName = 'trigono.out'; • var • Deg, Rad, SinA, CosA: Real; • begin • Assign(Input, InputFileName); • Reset(Input); • Assign(Output, OutputFileName); • Rewrite(Output); • Read(Deg);

  39. My Solution for Task2 • Rad := Deg / 180 * Pi; • SinA := Sin(Rad); • CosA := Cos(Rad); • WriteLn(SinA : 0 : 4); • WriteLn(CosA : 0 : 4); • WriteLn(SinA / CosA : 0 : 4); • WriteLn(CosA / SinA : 0 : 4); • WriteLn(1 / CosA : 0 : 4); • WriteLn(1 / SinA : 0 : 4); • Close(Input); • Close(Output); • end.

  40. Sequential Control • Sequential control is the default. The computer executes each statement and goes on to the next statement until it sees an end.

  41. Boolean Expressions • Precedence • - not • * / div mod and shr shl • + - or xor • = <> < > <= >= • Complex Boolean • 3 > 5 or 650 < 1 // WRONG • (3 > 5) or (650 < 1) // CORRECT

  42. Real Value Comparison • DO NOT compare two real values with operator =. Small round-off errors may cause two equivalent expressions to differ. • Instead, use this comparison: • const Epsilon = 1E-10; • Abs(x1 - x2) <= Epsilon

  43. Statement • Assignment • Subroutine Call • goto • Compound Statement • Conditional Statement • Repetitive Statement

  44. Compound Statement • begin • Statement1; • Statement2; • … • end;

  45. IF Statement • if <BooleanExpression> then • Statement; • OR • if <BooleanExpression> then • Statement1 • else • Statement2;

  46. IF Statement Pitfall • if <BooleanExpression1> then • if <BooleanExpression2> then • Statement1 • else • Statement2;

  47. IF Statement Pitfall Correction • if <BooleanExpression1> then • begin • if <BooleanExpression2> then • Statement1 • end • else • Statement2;

  48. IF Statement Extension • if <BooleanExpression1> then • Statement1 • else if <BooleanExpression2> then • Statement2 • else if … • … • else • Statement n;

  49. CASE Statement • case <OrdinalExpression> of • <OrdinalValueList1>: Statement1; • <OrdinalValueList2>: Statement2; • … • else Statement • end;

  50. FOR Statement • for <OrdinalVariable>:=<LowerBound> to <UpperBound> do • Statement; • OR • for <OrdinalVariable>:=<UpperBound> downto <LowerBound> do • Statement;

More Related