Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection (decision) control structure Learning objective
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 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.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: HTML and Client-side code.
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 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Information Processing: Expressions, Operators & Functions.
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 19 – Web applications: Server-side code (ASP)
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Web applications: HTML and ActiveX controls.
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.
Chapter 12: How Long Can This Go On?
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, 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)
M Dixon 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, SoCCE SOFT 131Page 1 02 – Dynamic HTML (client-side scripting)
Mark Dixon Page 1 03 – Dynamic HTML (client-side scripting)
Algorithms Writing instructions in the order they should execute.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Visual BASIC 6.0.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon Page 1 04 – Information Processing: Expressions, Operators & Functions.
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 Page 1 15 – Web applications: Server-side code (ASP)
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
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.
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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
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.
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
Sequence, Selection, Iteration The IF Statement
06 – Conditional Execution
Introduction To Robot Decision Making
Visual Basic – Decision Statements
Chapter 3: Selection Structures: Making Decisions
08 – Iterative Execution.
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 03 – Conditional Execution

Mark Dixon, SoCCE SOFT 131Page 2 Admin Technicians (Babbage 205) can provide you with free copies of (bring your own blank CDs): –MS Windows XP Professional (1 CD), includes MS Internet Information Services (term 2) –MS Visual Studio 6.0 (4 CDs), includes Visual InterDev 6.0 Visual BASIC 6.0 Visual C++ 6.0

Mark Dixon, SoCCE SOFT 131Page 3 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 conditional expressions, and –implement decision trees in code

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

Mark Dixon, SoCCE SOFT 131Page 5 Example: Puppy Puppy's web page Sub btnGuest_OnClick() document.title = "Puppy (large image)" picFace.src = "FaceLarge.jpg" End Sub Welcome, Puppy's web page.

Mark Dixon, SoCCE SOFT 131Page 6 Example: Puppy (version 2) Freya's web page Sub btnFreya_OnClick() If btnFreya.value = "Large" Then picFace.src = "FaceLarge.jpg" btnFreya.value = "Small" Else picFace.src = "Face.jpg" btnFreya.value = "Large" End If End Sub Freya's web page. Animation

Mark Dixon, SoCCE SOFT 131Page 7 If Then statements Use the following syntax: If condition Then [statementblock] End If For example: If txtAge.value < 21 Then document.bgColor = "Red" End If

Mark Dixon, SoCCE SOFT 131Page 8 If Then Else statements Use the following syntax: If condition1 Then [statementblock-1] [Else [statementblock-2]] End If For example: If txtAge.value Then document.bgColor = "Red" Else document.bgColor = "Blue" End If

Mark Dixon, SoCCE SOFT 131Page 9 Conditions & Relational Operators Conditions – expression, evaluates to: –true(stored as –1) –false(stored as 0) contain relational operators: =is equal to >is greater than =is greater than or equal to is not equal to

Mark Dixon, SoCCE SOFT 131Page 10 Examples: Conditions Using literals: (34 = 34)(evaluates to true) (34 = 12)(evaluates to false) (34 > 4)(evaluates to true) (18 <=18)(evaluates to true) Using controls' properties: Assume that picMain.hSpace is 2300 (picMain.hSpace = 2300) (true) (picMain.hSpace = 2309 (false) (picMain.hSpace <> 189 (true) (picMain.hSpace > 1900 (true)

Mark Dixon, SoCCE SOFT 131Page 11 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 (assume picMain.vSpace is 23):

Mark Dixon, SoCCE SOFT 131Page 12 Exercise: Conditions What is the result of (picMain.hSpace is 5589): (picMain.hSpace > 4400) What is the result (txtAge.value is 19, txtSalary.value is 10787): (txtAge.Value < 21) AND (txtSalary.Value < 10787) Write an expression to check if: picMain.hSpace is larger than 167 Write an expression to check if: picMain.vSpace is larger than picBall.vSpace true false (picMain.hSpace > 167) (picMain.vSpace > picBall.vSpace)

Mark Dixon, SoCCE SOFT 131Page 13 Example: Multiply Multiply Sub btnAns_OnClick() If txtAns.Value = 15 Then document.bgcolor = "yellow" lblComment.innertext = "Correct, well done!" sndFanfare.Play Else document.bgcolor = "cyan" lblComment.innertext = "Sorry, try again" sndFart.Play End If End Sub What is 5 times 3?

Mark Dixon, SoCCE SOFT 131Page 14 Example: Student Loan Student Loan Repayment Calculator Sub btnCalc_OnClick() lblPayment.innertext = (txtIncome.value ) * 0.09 End Sub Student Loan Repayment Calculator SLC

Mark Dixon, SoCCE SOFT 131Page 15 Example: Student Loan (v2) 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 Student Loan Repayment Calculator

Mark Dixon, SoCCE SOFT 131Page 16 Example: Student Loan (v3)

Mark Dixon, SoCCE SOFT 131Page 17 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, SoCCE SOFT 131Page 18 Example: Ball Char (v2) Test Sub window_OnLoad () window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight () picBall.hspace = picBall.hspace + 5 End Sub

Mark Dixon, SoCCE SOFT 131Page 19 Example: Ball Char (v2.1) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hSpace < Document.Body.ClientWidth Then picBall.hSpace = picBall.hSpace + 5 End If End Sub

Mark Dixon, SoCCE SOFT 131Page 20 Example: Ball Char (v2.2) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If picBall.hspace < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub

Mark Dixon, SoCCE SOFT 131Page 21 Example: Ball Char (v2.3) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 End If End Sub

Mark Dixon, SoCCE SOFT 131Page 22 Example: Ball Char (v2.4) Test Sub Window_OnLoad() Window.SetInterval "MoveBallRight", 50 End Sub Sub MoveBallRight() If (picBall.hspace + 5) < (document.body.clientwidth - picBall.Width) Then picBall.hspace = picBall.hspace + 5 Else Window.SetInterval "MoveBallLeft", 50 End If End Sub Sub MoveBallLeft () picBall.hspace = picBall.hspace - 5 End Sub >

Mark Dixon, SoCCE SOFT 131Page 23 Example: Ball Char (v2.5) Bounce from side to side, with sound:

Mark Dixon, SoCCE SOFT 131Page 24 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, SoCCE SOFT 131Page 25 Decision Trees Natural language –ambiguous & difficult to follow Decision trees –express same information clearly Delivery Fee <= 5 miles > 5 miles >= £10 < £10 Free £1.50 £3.00

Mark Dixon, SoCCE SOFT 131Page 26 Example: Pizza Delivery Delivery 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 Distance: Cost:

Mark Dixon, SoCCE SOFT 131Page 27 Example: Garden (v2) To teach children names of garden animals: