920 likes | 965 Views
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
E N D
Introduction to Modula-2 Johan Parent Practicum I
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
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
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
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
Program structure • Program is called a MODULE MODULE name; FROM …. IMPORT …; CONST pi = 3.1459; TYPE euro = REAL; BEGIN …. (* comment *) END name. Practicum I
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
Variables • Convenient way to: • Store values • Manipulate values • Closely related to the variable concept used in mathematics! Practicum I
Variables • Need to be declared: • Name • Type VAR X : CARDINAL; • Change the value (assignment) X := 451; : := := Practicum I
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
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
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
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
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
Conversions • Numbers can not be mixed! • CARDINAL REAL INTEGER • Conversion is needed • TRUNC(real) => CARDINAL • FLOAT(cardinal/integer) => REAL Practicum I
BOOLEAN algebra • Built-in type: BOOLEAN • 2 values: TRUE, FALSE • OPERATORS: • Logical : AND, OR, NOT • Comparison : <, >, =, <=, >=, <>, # Practicum I
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
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
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
Constant • Keyword CONST • Usage identical to variable • Assignment is impossible MODULE demo; CONST pi = 3.14159; code = 9342; = Practicum I
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
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
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
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
Subrange MODULE demo; VAR a, b, c : [2..15]; BEGIN a := 2; b := 8; c := a * b; END demo. Practicum I
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
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
Record • Delcaration VAR name: RECORD field1 : type1; . . fieldN : typeN; END; Practicum I
Record • Declaration VAR date : RECORD day : [1..31]; month : [1..12]; year : [1900..3000]; END; Practicum I
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
Record VAR point1, point2 : RECORD x, y : REAL; END; BEGIN point1.x := 5.4; point1.y := 2.3; point2 := point1; Possible? Practicum I
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
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
Array VAR price : ARRAY (junior, normal, maxi) OF REAL; BEGIN price[normal] := 2.25; price[maxi] := 3.00; Practicum I
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
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
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
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
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
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
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
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
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
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
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
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
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
CASE • Choose one alternative using a label, no test!! CASE expression OF label1, label2 : statements… | label3 : staments… | ELSE statements… END; Optional Practicum I
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
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