Mark Dixon Page 1 05 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

Selection (decision) control structure Learning objective
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 02 – Software Development Life-Cycle.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Constants and Variables.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 04 – Data Types. Mark Dixon Page 2 Admin: On-line Quiz.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
02 – Client-side code: JavaScript
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
M Dixon 1 04 – Data Types & Debugging. Questions: Expressions a)What is the result of: 7 + Int(8.245) b)Write an expression to: put a random number into.
Mark Dixon 1 05 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6 words) currently.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 06 – Conditional Execution. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 03 – Information Processing. Mark Dixon 2 Questions: Events Consider the following code: a) How many unique events does it contain? b) Name.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon 1 9 – Case Study. Mark Dixon 2 Session Aims and Objectives Aims –To give an overview of the development of a web-page from initial idea to.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon 1 06 – Conditional Execution. Mark Dixon 2 Admin: Test (next week) In class test –teaching week 7 50 minutes short answer (5 - 6 words max)
M Dixon 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon Page 1 04 – Conditional Execution. Mark Dixon Page 2 Questions: Expressions a)What is the result of: 10 * Int( ) b)How many functions.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
PHY 107 – Programming For Science. Announcements no magic formulas exist  Need to learn concepts: no magic formulas exist  Single solution not useful;
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Mark Dixon Page 1 03 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
04 – Information Processing: Expressions, Operators & Functions
06 – Conditional Execution
Introduction To Robot Sensors
Introduction To Robot Decision Making
Introduction To Robot Decision Making
Chapter 3: Selection Structures: Making Decisions
08 – Iterative Execution.
Chapter 3: Selection Structures: Making Decisions
Conditionals.
Presentation transcript:

Mark Dixon Page 1 05 – Conditional Execution

Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 minutes short answer (5 - 6 words max) 25% of coursework mark

Mark Dixon Page 3 Questions: Data Types a)What is the result of: Mid("George Boole", 5, 4) b)What is put in lblRes? txtPetName.value = "George" lblRes.innerText = Right(txtPetName.value, 3) c)What is put in lblRes? txtPetName.value = "George" lblRes.innerText = Right("txtPetName", 3) ge B rge ame

Mark Dixon Page 4 Session Aims & Objectives Aims –to introduce the main concepts involved in getting the computer to act differently under different circumstances Objectives, by end of this week’s sessions, you should be able to: –evaluate and generate conditional expressions –use conditional statements to make your code more adaptable

Mark Dixon Page 5 Adaptive Behaviour So far –every statement always executed in sequence Often necessary for software to –change behaviour under different circumstances

Mark Dixon Page 6 Example: Multiplication Test SPECIFICATION User Requirements –A primary school teacher wants to test the multiplication skills of her children. Software Requirements –Functional: –display a multiplication question –allow the user to type a response –check the response and provide feedback –Non-functional should be interesting, colourful, and easy to use

Mark Dixon Page 7 Example: Multiplication Test v1 Multiply What is 5 times 3? Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" lblComment.innertext = "Correct, well done!" Else document.bgcolor = "cyan" lblComment.innertext = "Sorry, try again" End If End Sub

Mark Dixon Page 8 Example: Multiplication Test v1

Mark Dixon Page 9 Example: Multiplication Test v1

Mark Dixon Page 10 George Boole 1815 (Lincoln, UK) – 1864 Invented Boolean algebra –key concept in computing –boolean datatype: 0false 1true Condition – expression, evaluates to: –true(stored as –1) –false(stored as 0)

Mark Dixon Page 11 Conditions: Relational Operators conditions contain relational operators: =is equal to >is greater than =is greater than or equal to is not equal to

Mark Dixon Page 12 Conditions: Examples (literal) Using literals: 34 = = > 4 18 <= 18 true false true

Mark Dixon Page 13 Conditions: Examples (symbolic) Using symbols (controls' properties): Assume that: picMain.style.pixelLeft is 2300 picMain.style.pixelLeft = 2300 picMain.style.pixelLeft = 2309 picMain.style.pixelLeft <> 189 picMain.style.pixelLeft > 1900 true false true

Mark Dixon Page 14 Conditions: Errors Are the following valid: – 23 > 30 – – 23 < – picBat.style.pixelLeft > 1000 – < picBat.style.pixelTop   missing (relational) operator missing data  missing data

Mark Dixon Page 15 Questions: Conditions What is the result of (picMain.style.pixelLeft is 5589): picMain.style.pixelLeft > 4400 What is the result (txtAge.value is 19, txtSalary.value is 10787): txtAge.Value < 21 AND txtSalary.Value < Write an expression to check if: the pixelLeft of picMain is larger than 167 Write an expression to check if: picMain pixelTop is more than picBall pixelTop true false picMain.style.pixelLeft > 167 picMain.style.pixelTop > picBall.style.pixelTop

Mark Dixon Page 16 If Then statements Use the following syntax (pattern): If condition Then statementblock End If For example: If txtAge.value < 18 Then document.bgColor = "Red" End If

Mark Dixon Page 17 If Then Else statements Use the following syntax (pattern): If condition Then statementblock-1 Else statementblock-2 End If For example: If txtAge.value < 18 Then document.bgColor = "Red" Else document.bgColor = "Blue" End If

Mark Dixon Page 18 Example: Student Loan Student Loan Repayment Calculator Student Loan Repayment Calculator Sub btnCalc_OnClick() lblPayment.innertext = (txtIncome.value ) * 0.09 End Sub

Mark Dixon Page 19 Example: Student Loan (v2) Student Loan Repayment Calculator Student Loan Repayment Calculator Sub btnCalc_OnClick() If txtIncome.value > Then lblPayment.innertext = "£" & ((txtIncome.value ) * 0.09) Else lblPayment.innertext = "You pay nothing (£0.00)!" End If End Sub

Mark Dixon Page 20 Example: Ball Char Functional Decomposition Incremental Development Get ball char to bounce horizontally: –get ball char to appear on left of page –get ball char to move right on page (user click) –get ball char to move right on page automatically –get ball char to stop at end –get ball char to change direction

Mark Dixon Page 21 Example: Ball Char (v2) Ball Char Sub window_OnLoad () window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight () picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End Sub

Mark Dixon Page 22 Example: Ball Char (v2.1) Ball Char Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.style.pixelLeft < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub

Mark Dixon Page 23 Example: Ball Char (v2.2) Ball Char Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.style.pixelLeft + picBall.width ) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub

Mark Dixon Page 24 Example: Ball Char (v2.3) Ball Char Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If ( picBall.style.pixelLeft + picBall.width + 5) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 End If End Sub

Mark Dixon Page 25 Example: Ball Char (v2.4) Ball Char Sub window_OnLoad() window.setInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.style.pixelLeft + picBall.width + 5) < document.body.clientWidth Then picBall.style.pixelLeft = picBall.style.pixelLeft + 5 Else window.setInterval "MoveBallLeft", 50 End If End Sub Sub MoveBallLeft () picBall.style.pixelLeft = picBall.style.pixelLeft - 5 End Sub

Mark Dixon Page 26 Example: Ball Char (v2.5) Bounce from side to side, with sound:

Mark Dixon Page 27 Example: Pizza Delivery A Pizza shop provides a delivery service. If the delivery is within five miles of the shop, then no delivery fee is charged. If the cost of the goods is less than £10 then a £3 delivery fee is charged, otherwise a £1.50 delivery fee is charged.

Mark Dixon Page 28 Decision Trees Natural language –ambiguous & difficult to follow Decision trees –express same information clearly distance <= 5 miles value >= £10 Free £1.50 £3.00 Y N Y N

Mark Dixon Page 29 Example: Pizza Delivery Delivery Distance: Cost: Sub btnCalc_OnClick() If txtDist.value <= 5 Then lblCharge.innerText = "Delivery Charge: £0.00" Else If txtCost.value >= 10 Then lblCharge.innerText = "Delivery Charge: £1.50" Else lblCharge.innerText = "Delivery Charge: £3.00" End If End Sub Nested If statements –one if inside another if

Mark Dixon Page 30 If statements: Errors If txtNum.value > 5 Then If txtNum.value = 4 Then document.bgColor = "green" End If If picMan.width > 5 document.bgColor = "red" End If missing Then keyword missing End If

Mark Dixon Page 31 Logical Operators AndTrue when both items are True picMain.vSpace > 5 AND picMain.vSpace 55 false picMain.vSpace > 6 AND picMain.vSpace = 6 AND picMain.vSpace <= 23 true OrTrue when either item is True picMain.vSpace = 23 OR picMain.vSpace = 11 true picMain.vSpace 55 false NotTrue when item is False Not (picMain.vSpace = 23) false Use to join conditions (picMain.vSpace is 23):

Mark Dixon Page 32 Tutorial Exercises: Multiplication LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the Multiplication v1 and v1.1 examples (from the lecture) working. Task 2: Modify your program so that the text box is disabled after the answer is checked Task 3: Modify your program so that it makes a suitable sound when the user gets the answer right/wrong. Sound files are in the resources section of the web-site

Mark Dixon Page 33 Tutorial Exercises: Student Loan LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the Student Loan v1 and v2 examples (from the lecture) working. Task 2: Modify your program so that it calculates and displays monthly income and repayment amounts (as well an annual).

Mark Dixon Page 34 Tutorial Exercises: BallChar LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the BallChar example (from the lecture) working. You will need to work out the code for v2.5 – use the previous code for inspiration. Task 2: Modify your program so that the Ball Character blinks when the mouse moves over it Task 3: Modify your program to play a sound when the ball character is clicked

Mark Dixon Page 35 Tutorial Exercises: Pizza Delivery LEARNING OBJECTIVE: use nested if statements to perform conditional execution Task 1: Get the Pizza Delivery example (from the lecture) working.