15
1 Introducing ASML Enumerations, Conditionals and Loops, Quantifiers Lecture 13 Software Engineering COMP201

Introducing ASML

Embed Size (px)

DESCRIPTION

Introducing ASML. Enumerations, Conditionals and Loops, Quantifiers Lecture 13 Software Engineering COMP201. y. y. y. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Introducing ASML

1

Introducing ASML

Enumerations,

Conditionals and Loops, Quantifiers

Lecture 13

Software Engineering COMP201

Page 2: Introducing ASML

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

x

y

y

y

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 )

Page 3: Introducing ASML

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

Page 4: Introducing ASML

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 enumerationenum Punctuation Comma = 5 Period = 3 Semi = 1Main() step if Comma < Period then WriteLine(Period) else WriteLine(Comma)

// Partially initialised // enumerationenum Punctuation Comma Period = 100 Semi Main() step if Semi < Period then WriteLine(Period) else WriteLine(Semi)

Page 5: Introducing ASML

5

II. Conditionals and Loops

• Each of these operators compares two values and returns one of two possible Boolean values: true or false

Meaningeq = =ne <>

lt < <gt > >lte <=

gte >=

in notin subset

superset subseteq superseteq

Page 6: Introducing ASML

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.

Page 7: Introducing ASML

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

S = {1..6}Main() step forall x in S if (x mod 2 = 1) then WriteLine(x+“ is odd. ”) else WriteLine ( x + “ is even. ”)

The result is: 6 is even.1 is odd.

2 is even.3 is odd.5 is odd.4 is even

Remember that sets have no intrinsic

order

Page 8: Introducing ASML

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

Page 9: Introducing ASML

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

or At least one condition must be true for a true result

not Inverts the value of a true or false expression

implies True unless the first condition is true and the second condition is false

iff True when both conditions have the same value

Page 10: Introducing ASML

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

Page 11: Introducing ASML

11

Operators

p q not p p or q p and q p implies q

p

iff

q

T T F T T T T

T F F T F F F

F T T T F T F

F F T F F T T

Page 12: Introducing ASML

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

Page 13: Introducing ASML

13

The forall … holdsQuantifiers

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

Page 14: Introducing ASML

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.

Page 15: Introducing ASML

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.