240 likes | 373 Views
Elementary Programming. Pascal Syntax. What you have learnt so far. writeln () and write(); readln () and read(); variables (integers, strings, character) manipulation of variables := ‘+’, ‘-’, ‘*’, ‘/’, ‘div’, ‘mod’ length(), chr (), ord (), etc. Just to warm up.
E N D
Elementary Programming Pascal Syntax
What you have learnt so far • writeln() and write(); • readln() and read(); • variables (integers, strings, character) • manipulation of variables • := • ‘+’, ‘-’, ‘*’, ‘/’, ‘div’, ‘mod’ • length(), chr(), ord(), etc.
Just to warm up • 1. Try to write a program thatoutputs: Enter the first number: <input var1> Enter the second number: <input var2> Their sum is: Their difference is: ** Their product is: Their quotient is: Their remainder is: ** If you know absolute value, you can try abs().
Just to warm up • 2. Try to write a program that goes: What’s your name? <input> What’s your year of birth? <input> What’s the year now? <input> Wow, <name>, you are <age> now !!! Yeah I know it’s kinda lame…
A few more variable types • Boolean • Have only two values: ‘true’ or ‘false’ • Declaration var boo : boolean; • Assignment is possible but not input by users • boo := TRUE; or boo := FALSE; is valid • readln(boo); is not valid
A few more variable types • Boolean • Basic manipulation • ‘Not’ • boo := not boo; • ‘And’ • boo1 := (boo1 and boo2); • ‘Or’ • boo1 := (boo1 or boo2);
A few more variable types • Boolean • NOT • AND
A few more variable types • Boolean • OR • Other boolean operations can be derived from the basic ‘and’ and ‘or’ and ‘not’
A few more variable types • Boolean • Question: given XOR is a boolean operation such that Can you give a proper definition of XOR with ‘and’, ‘or’ and ‘not’? p XOR q := ((p OR q) AND NOT (p AND q))
A few more variable types • Boolean • A few more points to note • Just like arithmetic, the operation within the parentheses will be done first. • e.g. ((p AND q) OR r) means doing the operation (p AND q) first then do the operation OR r • In Pascal, it is not essential that you put the parentheses around every boolean expression, but you should do so anyway to have a good ‘programmer habit’ • e.g. p := (p AND q); and p := p AND q; should give same result
A few more variable types • Boolean Operation (Just a side note) • Guess whether the following is valid: var a, b, result : integer; begin readln(a); readln(b); result := a AND b; writeln(result); end.
A few more variable types • Array • Array is an indexed collection of a type of variables • Imagine you need to declare 10 variables var a, b, c, d, e, f, g, h, i : integer; • Now imagine you need to declare 100 variables
A few more variable types • Array • Solve the problem by providing indexed elements • Declaration var a : array[1..100] of integers; • Similar to declaring var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 (…) a100 : integer;
A few more variable types • Array • Analysis • ‘a’ : the name of the array • ‘1..100’: ‘1’ means the first element, ‘100’ means the last element • ‘’a’..’z’’ is valid • ‘of integers’: referring what types of the variables inside the array is this array about
A few more variable types • Array • Usage • How to extract information? • Similar to the normal variables, e.g. var a : array[1..100] of integer; begin a[1] := 5; a[2] := 6; end.
A few more variable types • Array • Usage • A very convenient way to visualize arrays are ‘drawers’ • e.g. var a : array[1..10] of integers; • a • (Why is every element 0?) • Let’s say we need to change a[1] into 3. So we type • a[1] := 3; • a • Is it meaningful to declare a := 0?
A few more variable types • Array • Multi-dimensional array • It is possible to declare multi-dimensional array like this: var a : array[1..100, 1..100] of integer; (2D array) and the usage will be a[1,2] := 5; • If you don’t understand, try to think it as a co-ordinate system • It is also possible to declare var a : array[1..100, 1..100, 1..100, 1..100] of integer; (4D array)
So far so good? • Any questions??? • If no, try this out (Courtesy Jasper Lee, your future dai lo in OI team, modified): 3. Write a program that has an array of 5 booleans. The program should then read 3 integers, ranging from 1 to 5. Afterwards show whether an integer, from 1 – 5, has been inputted before. Hint: Consider i as an integer ranging from 1 – 5, what does a[i], the ith element in the array of 10 booleans mean?
If then else • The programs we are writing now are slightly ‘more useful’ then the ‘Hello, world!’ but still we can’t do much about it. • Selection is a very important topic in programming and will help you to write more powerful programs.
If then else • Selection Controls • Basically it verifies a condition. When this condition is true it does something, otherwise it does another thing. • Syntax: if <condition> then begin … end <- NOTE: no semi-colon here else begin … end;
If then else • Selection Controls • Analysis • ‘if <condition> then begin … end’ : if the <condition> is true then do … • e.g. if (a >= 18) then begin writeln(‘You are an adult!’); end • ‘else begin … end’ : if the <condition> in the first line is false then do … • e.g. (continued from the first example) else begin writeln(‘Go back to your momma.’);
If then else • Selection Controls • The (not-so-)complicated ‘begins’ and ‘ends’ • Why do we need ‘begin’ and ‘end’? • Basically ‘begin’ and ‘ends’ works somewhat like a parentheses, specifying which chunk of code should the program run at which condition. • Consider the following two example:
If then else • Try the following: • 1(a) if (a >= 18) then begin writeln(‘You are an adult!’); writeln(‘Go ahead.’); end else begin writeln(‘You are not an adult yet!’); writeln(‘Wait for a few more years and then come back.’); end; • 1(b) if (a >= 18) then writeln(‘You are an adult!’); writeln(‘Go ahead.’); else writeln(‘You are not an adult yet!’); writeln(‘Wait for a few more years and then come back.’);
If then else • 2(a) if (a >= 18) then begin writeln(‘You are an adult!’); end else begin writeln(‘You are not an adult yet!’); end; • 2(b) if (a >= 18) then writeln(‘You are an adult!’); else writeln(‘You are not an adult yet!’); • * Any modification for 1(b) and 2(b) to make it ‘compilable’ (but not necessarily correct)? • What conclusions can you reach about ‘begin’ and ‘end’ after trying out the above examples?