Download presentation

Presentation is loading. Please wait.

Published byLuke Gorsuch Modified about 1 year ago

1
Pascal Syntax

2
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.

3
Just to warm up 1. Try to write a program that outputs: Enter the first number: Enter the second number: Their sum is: Their difference is:** Their product is: Their quotient is: Their remainder is: ** If you know absolute value, you can try abs().

4
Just to warm up 2. Try to write a program that goes: What’s your name? What’s your year of birth? What’s the year now? Wow,, you are now !!! Yeah I know it’s kinda lame…

5
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

6
A few more variable types Boolean Basic manipulation ‘Not’ boo := not boo; ‘And’ boo1 := (boo1 and boo2); ‘Or’ boo1 := (boo1 or boo2);

7
A few more variable types Boolean NOT AND p~p TRUEFALSE TRUE pqp^q TTT TFF FTF FFF

8
A few more variable types Boolean OR Other boolean operations can be derived from the basic ‘and’ and ‘or’ and ‘not’ pqpVq TTT TFT FTT FFF

9
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)) pqp XOR q TTF TFT FTT FFF

10
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

11
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.

12
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

13
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;

14
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

15
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.

16
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[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8]a[9]a[10] a[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8]a[9]a[10]

17
A few more variable types Array Multi-dimensional array It is possible to declare multi-dimensional array like this: var a : array[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, , , ] of integer;(4D array)

18
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 i th element in the array of 10 booleans mean?

19
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.

20
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 then begin … end<- NOTE: no semi-colon here else begin … end;

21
If then else Selection Controls Analysis ‘if then begin … end’ : if the is true then do … e.g. if (a >= 18) then begin writeln(‘You are an adult!’); end ‘else begin … end’ : if the in the first line is false then do … e.g. (continued from the first example) else begin writeln(‘Go back to your momma.’);

22
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:

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

24
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?

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google