1 / 91

Introduction to Modula - 2

Introduction to Modula - 2. Johan Parent. Introduction. 3 hours of total immersion into the programming language Modula-2 We start from the very basics Johan Parent Johan@info.vub.ac.be 4K216. Modula 2 : why?. Why not Java, C, C++, C# ?! Learning Looks like English Very structured

Download Presentation

Introduction to Modula - 2

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. Introduction to Modula-2 Johan Parent Practicum I

  2. Introduction • 3 hours of total immersion into the programming language Modula-2 • We start from the very basics • Johan Parent • Johan@info.vub.ac.be • 4K216 Practicum I

  3. Modula 2 : why? • Why not Java, C, C++, C# ?! • Learning • Looks like English • Very structured • Safe (easy?) • Has all the modern features • All programming languages are equal!! Practicum I

  4. Programming • Way to control a computer • Write down the program (editor) • Translate using special program (compiler) • Execution by the computer • Compiler is clever • Translates in machine language • Reports mistakes Practicum I

  5. Modula 2 • Has keywords: words from the language • Always in CAPITAL letters! • DO, FOR, IF, WHILE, END, THEN, UNTIL, LOOP, ELSE, RECORD, ARRAY, OF, TYPE, CONST, IMPORT, FROM, SET, VAR, WITH, CASE, MODULE, REAL, INTEGER… Practicum I

  6. Program structure • Program is called a MODULE MODULE name; FROM …. IMPORT …; CONST pi = 3.1459; TYPE euro = REAL; BEGIN …. (* comment *) END name. Practicum I

  7. Syntax • Keywords completely in CAPITAL letters • Spaces do not matter • White lines do not matter • Signal the end of a command with semi colon ; • End of program with a period . Practicum I

  8. Variables • Convenient way to: • Store values • Manipulate values • Closely related to the variable concept used in mathematics! Practicum I

  9. Variables • Need to be declared: • Name • Type VAR X : CARDINAL; • Change the value (assignment) X := 451; : := := Practicum I

  10. Variables • Single types: • Characters (‘a’) : CHAR • Integers (-40) : INTEGER • Reals (2.7) : REAL • Pos number (3) : CARDINAL • Logical (TRUE/FALSE) : BOOLEAN • Why the name « single types »? Practicum I

  11. Variables Keyword Declarations MODULE demo; VAR a : CHAR; b : INTEGER; c : REAL; d : CARDINAL; e : BOOLEAN: BEGIN a := ‘z’; b := -345; c := 0.35E-3; d := 345; e := FALSE; END demo. Assignments := Practicum I

  12. Variables vs. Literals • Variable can have several values (not at one time) • Literals are the values themselves: • -3 • 123 • ‘z’ • 4.342 • TRUE Practicum I

  13. Working with numbers • INTEGER, CARDINAL and REAL • Operators: • Sum using + • Substract using – • Multiply using * • Divide using • DIV for CARDINAL and INTEGER • / for REAL • Modulo using MOD Practicum I

  14. Working with numbers MODULE demo; VAR a, b : INTEGER; d : REAL; f : CARDINAL; BEGIN a := -3; b := a * 2; d := 5.0 / 3.0; f := 5 DIV 3; END demo. Practicum I

  15. Conversions • Numbers can not be mixed! • CARDINAL  REAL  INTEGER • Conversion is needed • TRUNC(real) => CARDINAL • FLOAT(cardinal/integer) => REAL Practicum I

  16. BOOLEAN algebra • Built-in type: BOOLEAN • 2 values: TRUE, FALSE • OPERATORS: • Logical : AND, OR, NOT • Comparison : <, >, =, <=, >=, <>, # Practicum I

  17. BOOLEAN algebra MODULE demo; VAR a, b : INTEGER; c, d : BOOLEAN; BEGIN a := -3; b := 33; c := a > b; d := NOT c; END demo. Practicum I

  18. Precedence rules • Define which operator is evaluated first. • Multiplication, division, sum, … • 13 DIV 4 = 3 = TRUE • Is this correct? • What is the value then? • Use brackets to ensure the order of evalution => • ((13 DIV 4) = 3) = TRUE) Practicum I

  19. Characters • Built-in type: CHAR • Values: all the entries of the ASCII table • Character literals have to be surrounded by single quotes : • ‘z’ • How can we represent text… later Practicum I

  20. Constant • Keyword CONST • Usage identical to variable • Assignment is impossible MODULE demo; CONST pi = 3.14159; code = 9342; = Practicum I

  21. Constant • What is the type of the constant? • Can I use constant together with variables and literals? MODULE demo; CONST Pi = 3.14159; VAR x, r : REAL; BEGIN r := 5.0; x := 2.0 * Pi * r; END demo. Practicum I

  22. Enumeration • First flexible type: defined by you • Explicit definition of the possible values of a variable during the declaration VAR x : ( value1, value2, …, valueN); • Variable x can only have the values listed between brackets Practicum I

  23. Enumeration MODULE demo; VAR continent : ( europe, asia, africa, antartica, america); BEGIN continent := antartica; END demo. • Danger: values of enumeration can not be used as name for other constants or variable Practicum I

  24. Subrange • Equivalent to interval in mathematics x  [0, 10] • Again defined by you during the declaration VAR x : [0 .. 10]; c : [’a’ .. ‘d’]; • Not for real valued intervals!! .. Practicum I

  25. Subrange MODULE demo; VAR a, b, c : [2..15]; BEGIN a := 2; b := 8; c := a * b; END demo. Practicum I

  26. Structured types • Most types are built-in: • CHAR, REAL, BOOLEAN, CARDINAL, INTEGER • Self defined types: • Enumeration • Subrange • Until now simple types i.e. one variable = one value at a time Practicum I

  27. Structured types • Store more than one value per variable • RECORD: possible to combine different types in one variable • ARRAY: represent several values of the same type in one variable Practicum I

  28. Record • Delcaration VAR name: RECORD field1 : type1; . . fieldN : typeN; END; Practicum I

  29. Record • Declaration VAR date : RECORD day : [1..31]; month : [1..12]; year : [1900..3000]; END; Practicum I

  30. Record • Since a record contains several values we use the field name to access the value: • record.field_name date.day := 8; date.month := 10; date.year := 2002; . Practicum I

  31. Record VAR point1, point2 : RECORD x, y : REAL; END; BEGIN point1.x := 5.4; point1.y := 2.3; point2 := point1; Possible? Practicum I

  32. Array • Declaration VAR name : ARRAY index_type OF type; • Array contains one or more values depending on the index_type VAR x : ARRAY [1..10] OF REAL; Practicum I

  33. Array • As with RECORDs with need more work to take one value out of an array => indexing • array[position] x[8] := 4.3; x[2] := x[8] + 1.2; Practicum I

  34. Array VAR price : ARRAY (junior, normal, maxi) OF REAL; BEGIN price[normal] := 2.25; price[maxi] := 3.00; Practicum I

  35. Array VAR taste : ARRAY (bread, rice) OF (bad, normal, good); class : ARRAY(john, alan) OF RECORD age : [0..200]; weight : REAL; END; BEGIN taste[bread] := good; taste[rice] := good; class[john].age := 25; Practicum I

  36. Array • Respect the index type VAR x : ARRAY [3..10] OF BOOLEAN; BEGIN x[1] := TRUE; • Arrays can not be copied in 1 step VAR x, y : ARRAY [3..10] OF BOOLEAN; BEGIN x := y; Practicum I

  37. Strings • Character can be represented using the built-in CHAR type • One character at a time • There is no seperate type in Modula 2 for strings of characters! VAR text : ARRAY [1..30] OF CHAR; Practicum I

  38. Writing on screen • Writing on the computer screen can be done using the following procedures • String : WrStr(x); • INTEGER: WrInt(x, 0); • REAL: WrReal(x, 7, 0); • CARDINAL: WrCard(x, 0); • BOOLEAN: WrBool(x); • CHAR: WrChar(x); Practicum I

  39. N-dimensional array • Modula 2 arrays can have more than one index => more than one dimension VAR x : ARRAY [1..5] OF ARRAY [1..3] OF REAL; BEGIN x[5][2] := 5.0; Practicum I

  40. N-dimensional array • Declaration short hand for n-dim arrays: • x : ARRAY type1 OF ARRAY type2 OF ARRAY type3 OF type  • x : ARRAY type1,type2,type3 OF type • Indexing short hand for n-dim arrays: • x[3][3][4]  • x[3,3,4] Practicum I

  41. TYPE • Modula 2 comes with built-in types • You can also define new types MODULE demo; TYPE Mp3 = RECORD name : ARRAY [1..40] OF CHAR; length : [1..3600]; END; = Practicum I

  42. TYPE MODULE demo; TYPE Mp3 = RECORD name : ARRAY [1..40] OF CHAR; length : [1..3600]; END; VAR song : Mp3; BEGIN song.name := "4 seasons – winter"; END demo. Practicum I

  43. Control instruction • Normal program execution • Top down • Line by line • Statements that influence the execution of the program • 3 groups: • Branches • Loops • Procedures Practicum I

  44. IF • Choose an alternative based on result of the boolean expression IF boolean_express1 THEN statements… ELIF boolean_express2 THEN statements… ELSE statements… END; Optional Practicum I

  45. IF MODULE demo; FROM IO IMPORT WrStr; VARx : CARDINAL; BEGINx := 3; IF (x < 5) THEN WrStr(" x is smaller than 5"); END; END demo. Practicum I

  46. IF MODULE demo; FROM IO IMPORT WrStr; VARx : CARDINAL; BEGINx := 3; IF (x < 5) THEN WrStr(" x is smaller than 5"); ELSE WrStr(" x is >= 5"); END; END demo. Practicum I

  47. IF MODULE demo; FROM IO IMPORT WrStr; VARx : CARDINAL; BEGINx := 3; IF (x < 5) THEN WrStr(" x is smaller than 5"); ELIF (x > 5) THEN WrStr(" x is bigger than 5"); ELSE WrStr("x equals 5"); END; END demo. Practicum I

  48. CASE • Choose one alternative using a label, no test!! CASE expression OF label1, label2 : statements… | label3 : staments… | ELSE statements… END; Optional Practicum I

  49. CASE MODULE demo; VARmenu : (junior, normal, maxi); price : REAL; BEGINmenu := junior; CASE menu OF junior : price := 1.25; | normal : price := 2.25; | maxi : price := 3.00; | ELSE price := -99.999; END; END demo. Practicum I

  50. FOR • Loop a fixed number of times using a counter variable FOR counter := start TO stop BY step DO statements… END; • BY step is optional Practicum I

More Related