150 likes | 247 Views
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.
E N D
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 Set s x Min2 (S as Set of Integer) as Integer require S ne {} returnany x | x in S where not (exists y in S where y>x )
I. Enumerations • Enumerations provide a way of creating symbolic constants enumident [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
Initialised enumeration • By default,enumerationsare 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)
Meaning eq = = ne <> lt < < gt > > lte <= gte >= in notin subset superset subseteq superseteq II. Conditionals and Loops • Each of these operators compares two values and returns one of two possible Boolean values: trueorfalse
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 ] The result is: 1 is odd. 3 is odd. 5 is odd. S = {1..6} Main() step forall x in S if (x mod 2 = 1) then WriteLine ( x + “ is odd. ”)
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. ”)
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
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
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. ”)
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 … 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.
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.
` 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.