pascal syntax. what you have learnt so far writeln() and write(); readln() and read(); variables...

24
Elementary Programming Pascal Syntax

Upload: luke-gorsuch

Post on 14-Jan-2016

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

Elementary ProgrammingPascal Syntax

Page 2: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

What you have learnt so farwriteln() and write();readln() and read();variables (integers, strings, character)manipulation of variables

:=‘+’, ‘-’, ‘*’, ‘/’, ‘div’, ‘mod’length(), chr(), ord(), etc.

Page 3: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

Just to warm up1. Try to write a program that outputs:

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().

Page 4: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

Just to warm up2. 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…

Page 5: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

Have only two values: ‘true’ or ‘false’Declaration

varboo : boolean;

Assignment is possible but not input by users boo := TRUE; or boo := FALSE; is valid readln(boo); is not valid

Page 6: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

Basic manipulation ‘Not’

boo := not boo; ‘And’

boo1 := (boo1 and boo2); ‘Or’

boo1 := (boo1 or boo2);

Page 7: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

NOT

AND

p ~p

TRUE FALSE

FALSE

TRUE

p q p^q

T T T

T F F

F T F

F F F

Page 8: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

OR

Other boolean operations can be derived from the basic ‘and’ and ‘or’ and ‘not’

p q pVq

T T T

T F T

F T T

F F F

Page 9: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

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))

p q p XOR q

T T F

T F T

F T T

F F F

Page 10: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean

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

Page 11: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesBoolean 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.

Page 12: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

Array is an indexed collection of a type of variables

Imagine you need to declare 10 variablesvar

a, b, c, d, e, f, g, h, i : integer;Now imagine you need to declare 100 variables

Page 13: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

Solve the problem by providing indexed elementsDeclaration

vara : array[1..100] of integers;

Similar to declaringvar

a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 (…) a100 : integer;

Page 14: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

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

Page 15: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

Usage How to extract information? Similar to the normal variables, e.g.

vara : array[1..100] of integer;

begina[1] := 5;a[2] := 6;

end.

Page 16: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

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?

0 0 0 0 0 0 0 0 0 0

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

a[10]

3 0 0 0 0 0 0 0 0 0

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

a[10]

Page 17: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

A few more variable typesArray

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)

Page 18: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

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?

Page 19: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then elseThe 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.

Page 20: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then elseSelection Controls

Basically it verifies a condition. When this condition is true it does something, otherwise it does another thing.

Syntax:if <condition> thenbegin

…end <- NOTE: no semi-colon hereelsebegin

…end;

Page 21: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then elseSelection 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.’);

Page 22: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then elseSelection 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:

Page 23: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then elseTry the following:1(a)

if (a >= 18) thenbegin

writeln(‘You are an adult!’);writeln(‘Go ahead.’);

endelsebegin

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.’);

elsewriteln(‘You are not an adult yet!’);writeln(‘Wait for a few more years and then come back.’);

Page 24: Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables

If then else2(a)

if (a >= 18) thenbegin

writeln(‘You are an adult!’);endelsebegin

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?