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

Slides:



Advertisements
Similar presentations
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Advertisements

1 Introducing ASML ASML version 2, “Steps and Updates ” Lecture 9 Software Engineering COMP201.
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
True or false A variable of type char can hold the value 301. ( F )
1 Introducing ASML Methods, Values, Constraints, Constants, Variables Lecture 10 Software Engineering COMP201.
1 Introducing ASML Methods, Values, Constraints, Constants, Variables, Sets, Sequences Lecture 11, 12 Software Engineering COMP201.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
1 Introducing ASML Sequences, Parallel evaluation, Maps, Non-Determinism Lecture 12 Software Engineering COMP201.
EECS 20 Chapter 21 Defining Signals and Systems Last time we Introduced mathematical notation to help define sets Learned the names of commonly used sets.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Software Engineering, COMP201 Slide 1 Lectures 9,10 Formal Specifications.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
Lab 8 Shell Script Reference:
Chapter 4 The If…Then Statement
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Controlling Program Flow with Decision Structures.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Chapter 3 Control Statements
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4 The If…Then Statement
Programming for Mobile Technologies
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Expressions and Control Flow in JavaScript
More Selections BIS1523 – Lecture 9.
IF if (condition) { Process… }
Chapter 3: Introduction to Problem Solving and Control Statements
Control Structures: for & while Loops
Control Structures: Selection Statement
MongoDB Read/Write.
Pages:51-59 Section: Control1 : decisions
MongoDB Read/Write.
Computer Science Core Concepts
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
MongoDB Read.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Decisions, decisions, decisions
Introduction to Programming
Control Structures: Selection Statement
Pages:51-59 Section: Control1 : decisions
MongoDB Read Operations
REPETITION Why Repetition?
Presentation transcript:

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

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)

5 II. Conditionals and Loops Each of these operators compares two values and returns one of two possible Boolean values: true or false Meaning eq== ne <>  lt << gt >> lte<=  gte >=  in  notin  subset  superset  subseteq  superseteq 

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

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

10 The and Operator Use the and operator when you have two conditions that must be true for a true result: S = { } 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 pq not pp or qp and qp implies q p iff q TTFTTTT TFFTFFF FTTTFTF FFTFFTT

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

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

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.