Introducing ASML - University of Liverpool

Download Report

Transcript Introducing ASML - University of Liverpool

1
Introducing ASML
Enumerations,
Conditionals and Loops,
Quantifiers
Lecture 13
Software Engineering
COMP201
2
Logical Operators,
Quantifiers and Sets
Min1 (s as Set of Integer) as Integer
require s ne {}
return any x | x in s
where forall y in s holds x lte y
Set
s
y
x
Min2 (S as Set of Integer) as Integer
require S ne {}
return any x | x in S
where not (exists y in S where y>x )
3
I. Enumerations
• Enumerations provide a way of
creating symbolic constants
enum ident [enumElement-List]
enum Color
Red
Green
Blue
• This statement does two things
– It declares an enumeration type called
Color
– It establishes Red, Green and Blue as
symbolic constants called enumerators
• Let’s create a variable of that type:
var range as Color
4
Initialised enumeration
• By default, enumerations are assigned
integer values starting with
– 0 for the first enumerator,
– 1 for the second, and so on.
// Initialised enumeration
enum Punctuation
Comma = 5
Period = 3
Semi = 1
Main()
step
if Comma < Period
then WriteLine(Period)
else WriteLine(Comma)
// Partially initialised
// enumeration
enum Punctuation
Comma
Period = 100
Semi
Main()
step
if Semi < Period
then WriteLine(Period)
else WriteLine(Semi)
II. Conditionals
and Loops
• Each of these operators compares two
values and returns one of two possible
Boolean values: true or false
eq
ne
lt
gt
lte
gte
in
notin
subset
superset
subseteq
superseteq
Meaning
=
=
<>

<
<
>
>
<=

>=







5
6
The If Statement
• There are a variety of ways to use the
results of comparison to modify the
behaviour of a program
• The most basic of these is:
if expr [then] stmt-List
{ elseif expr [then] stmt-List }
[ else expr [then] stmt-List ]
S = {1..6}
Main()
step
forall x in S
if (x mod 2 = 1) then
WriteLine ( x + “ is odd. ”)
The result is:
1 is odd.
3 is odd.
5 is odd.
7
If-then-else
• Be extending the if statement to an
if-else statement, we can perform
one action if the condition is true
and another if it false
Remember that sets
have no intrinsic
order
The result is:
6 is even.
1 is odd.
2 is even.
3 is odd.
5 is odd.
4 is even
S = {1..6}
Main()
step
forall x in S
if (x mod 2 = 1) then WriteLine(x+“ is odd. ”)
else
WriteLine ( x + “ is even. ”)
8
If-then-elseif
• You can nest if-else statement to
handle multiple possible conditions:
S = { “ham”, “turkey”, “bit”, “cheese”}
Main()
step
x = any y | y in S
if x = “ham” then
WriteLine (“Ham sandwich”)
elseif x = “turkey” then
WriteLine (“Turkey sanwich”)
elseif
WriteLine (“Cheese sanwich”)
This program randomly selects one of the
elements in set S, thus determining the
program’s output
9
Logical Operators
• Using if statement with complex
conditions can be rather clumsy
• To simplify the situation, use logical
operations that allow you to combine
a series of comparisons
Symbol
Meaning
and
Both conditions must be true for a true
result
At least one condition must be true for
a true result
or
Inverts the value of a true or false
expression
implies True unless the first condition is true
and the second condition is false
not
iff
True when both conditions have the
same value
10
The and Operator
• Use the and operator when you have
two conditions that must be true for
a true result:
S = { 1 .. 12 }
Main()
x = any y | y in S
step
if (x<=6) and (x mod 2 = 1) then
WriteLine (x + “is an odd number
between 1 and 6. ”)
step
if (x>6) or (x mod 2 = 1) then
WriteLine (x + “is greater than six or an
odd number. ”)
11
Operators
p
q
not p p or q p and q
p
p
implies
iff
q
T
T
F
T
T
T
q
T
T
F
F
T
F
F
F
F
T
T
T
F
T
F
F
F
T
F
F
T
T
12
Quantifiers
• Quantifiers are used in quantifying
expressions
• These expressions return true or
false depending on whether a
condition has been met
– universally over some collection of
bindings ( forall … holds ) or
– existentially by at least one example
( exists )
• They are called quantifiers
because they specify how much of
the collection (a set, sequence, or
map) is included or executed by the
condition
The forall … holds
Quantifiers
• The forall expression holds quantifier
requires that all members of the
collection meet the specified condition
S = { 1, 2, 4, 6, 7, 8 }
IsOdd( i as Integer)
return (1 = i mod 2)
Main()
step
test1 = forall i in S holds IsOdd(i)
if test1 then
WriteLine ( “All odd numbers. ”)
else
WriteLine ( “Not all odd numbers. ”)
This prints the result:
Not all odd numbers.
13
14
The exist Quantifier
• The exist quantifier requires that at
least one member of the collection
meet the specified condition
S = { 1, 2, 4, 6, 7, 8, 9}
IsOdd( i as Integer)
return (1 = i mod 2)
Main()
step
test1 = exists i in S where IsOdd(i)
if test1 then
WriteLine ( “Some odd numbers. ”)
else
WriteLine ( “No odd numbers. ”)
This prints the result:
Some odd numbers.
15
`
S = { 2, 4, 6, 7, 9}
IsOdd( i as Integer)
return (1 = i mod 2)
Main()
step
test1 = exists unique i in S where IsOdd(i)
if test1 then
WriteLine ( “Some odd numbers. ”)
else
WriteLine ( “None or more than 1 odd
numbers. ”)
This prints the result:
None or more than 1 odd numbers.