Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Visual BASIC 2005.
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 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
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 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 03 – User Interface Design, and Sound and Graphics.
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 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
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, SoCCE SOFT 131Page 1 04 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
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.
Chapter 4: The Selection Structure
Multimedia ! Graphics ! Animation ! Sound Tip Of the Day 4 Tip of the Day: When using graphic methods at Form Load, the AutoRedraw property of the container.
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 3 – Web applications: Server-side code (JSP)
Lecture Set 5 Control Structures Part D - Repetition with Loops.
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.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
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.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
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 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, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
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 24 – Datatypes and Object Association.
22/11/ Selection If selection construct.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Visual BASIC 6.0.
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.
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)
Pay Example (PFirst98) Please use speaker notes for additional information!
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)
31/01/ Selection If selection construct.
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, SoCCE SOFT 131Page 1 17 – Procedures.
Controlling Program Flow with Decision Structures.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
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, SoCCE SOFT 131Page 1 03 – Conditional Execution.
06 – Conditional Execution
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Presentation transcript:

Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution

Mark Dixon, SoCCE SOFT 136Page 2 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 136Page 3 Adaptive Behaviour So far –every statement always executed in sequence Often necessary for software to –change behaviour under different circumstances

Mark Dixon, SoCCE SOFT 136Page 4 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, SoCCE SOFT 136Page 5 Example: Multiplication Test v1 Option Explicit Private Sub btnCheck_Click() If txtAns.Text = 15 Then lblMessage.Caption = "Correct, well done!" Me.BackColor = vbYellow Else lblMessage.Caption = "Sorry, 5 times 3 is 15" Me.BackColor = vbRed End If End Sub lblMessage btnCheck txtAns

Mark Dixon, SoCCE SOFT 136Page 6 If Then statements Use the following syntax: If condition Then [statementblock] End If For example: If txtAge.Text < 21 Then lblResult.BackColor = vbRed End If

Mark Dixon, SoCCE SOFT 136Page 7 If Then Else statements Use the following syntax: If condition1 Then [statementblock-1] [Else [statementblock-2]] End If For example: If txtAge.Text Then lblResult.BackColor = vbRed Else lblResult.BackColor = vbBlue End If

Mark Dixon, SoCCE SOFT 136Page 8 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 136Page 9 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: picMain.Left = 2300 (picMain.Left = 2300) (true) (picMain.Left = 2309 (false) (picMain.Left <> 189 (true) (picMain.Left > 1900 (true)

Mark Dixon, SoCCE SOFT 136Page 10 Example: Multiplication Test Problem –if user enters non-numeric data, then an error occurs

Mark Dixon, SoCCE SOFT 136Page 11 Example: Multiplication Test v2 Option Explicit Private Sub btnCheck_Click() If txtAns.Text = 15 Then lblMessage.Caption = "Correct, well done!" Me.BackColor = vbYellow Else lblMessage.Caption = "Sorry, 5 times 3 is 15" Me.BackColor = vbRed End If End Sub Private Sub txtAns_KeyPress(KeyAscii As Integer) If KeyAscii 57 Then KeyAscii = 0 End If End Sub code of key that was pressed executed every time key is pressed cancels key press

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

Mark Dixon, SoCCE SOFT 136Page 13 Questions: Conditions What is the result of (picMain.Left is 5589): (picMain.Left > 4400) What is the result of (txtAge.Text is 19, txtSalary.Text is 10787): (txtAge.Text < 21) AND (txtSalary.Text < 10787) Write an expression to: check if picMain.height is larger than Write an expression to: check if picMain.Top is larger than picBall.Top true false (picMain.Height > ) (picMain.Top > picBall.Top)

Mark Dixon, SoCCE SOFT 136Page 14 Example: Student Loan v1 Option Explicit Private Sub btnCalc_Click() lblResult.Caption = (txtIncome.Text ) * 0.09 End Sub SLC

Mark Dixon, SoCCE SOFT 136Page 15 Example: Student Loan v2 Option Explicit Private Sub btnCalc_Click() If txtIncome.Text > Then lblResult.Caption = "£" & (txtIncome.Text ) * 0.09 Else lblResult.Caption = "You pay nothing (£0.00)!" End If End Sub

Mark Dixon, SoCCE SOFT 136Page 16 Example: BallChar v3 Option Explicit Private Sub tmrLeft_Timer() ' You need to work this out! End Sub Private Sub tmrRight_Timer() picBallChar.Left = picBallChar.Left If picBallChar.Left >= Me.ScaleWidth Then tmrRight.Enabled = False tmrLeft.Enabled = True End If End Sub BallChar v3 tmrLeft picBallChar tmrRight

Mark Dixon, SoCCE SOFT 136Page 17 Check Box - give user on/off, yes/no choice –Value: 1 (vbChecked) if selected 0 (vbUnchecked) if not selected Option Box - Used as a group to give user multiple options (only 1 item selected at a time) –Value: -1 (True) if selected 0 (False) if not selected Can place option boxes in Frame control: –groups option boxes Selection/Decision controls

Mark Dixon, SoCCE SOFT 136Page 18 Example: Face v2 Private Sub btnDraw_Click() picFace.Cls picFace.Circle (2400, 2400), 2000 If chkNose.Value = vbChecked Then picFace.Line (2400, 2200)-Step(0, 600) End If If optOpen.Value = True Then picFace.Circle (1600, 1600), 500 picFace.Circle (3200, 1600), 500 Else picFace.Line (1100, 1600)-Step(1000, 0) picFace.Line (2700, 1600)-Step(1000, 0) End If If optHappy.Value = True Then picFace.Circle (2400, 2400), 1200,, 3.4, 6 Else picFace.Circle (2400, 4400), 1200,, 0.6, 2.5 End If End Sub Face

Mark Dixon, SoCCE SOFT 136Page 19 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 136Page 20 Example: Pizza Delivery Option Explicit Private Sub btnCalc_Click() If txtDist.Text <= 5 Then lblDel.Caption = 0 Else If txtCost.Text >= 10 Then lblDel.Caption = 1.5 Else lblDel.Caption = 3 End If End Sub Delivery lblDelbtnCalc txtDisttxtCost

Mark Dixon, SoCCE SOFT 136Page 21 Tutorial Exercises: Multiply LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the Multiply v1 and v2 examples (from the lecture) working. Task 2: Modify your program so that the keyboard cursor is put in the text box automatically HINT: use the SetFocus method Task 3: Modify your program so that the text box is disabled after the answer is checked Task 4: Modify your program so that a happy/sad face is drawn depending on whether the answer was correct. Task 5: 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, SoCCE SOFT 136Page 22 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, SoCCE SOFT 136Page 23 Tutorial Exercises: BallChar LEARNING OBJECTIVE: use if statement to perform conditional execution Task 1: Get the BallChar example (from the lecture) working. Task 2: Modify your program so that the Ball Character blinks when it is clicked Task 3: Modify your program to play a sound when the ball character is clicked

Mark Dixon, SoCCE SOFT 136Page 24 Tutorial Exercises: Face LEARNING OBJECTIVE: use if statement with selection controls Task 1: Get the Face example (from the lecture) working. Task 2: Modify your program so that the face is coloured in (e.g. eyes white, face pink) – be creative HINT: use the FillColour and FillStyle properties (described in previous lecture) Task 3: Modify your program to use sound (happy sound for a happy face, etc)

Mark Dixon, SoCCE SOFT 136Page 25 Tutorial Exercises: Pizza Delivery LEARNING OBJECTIVE: use nested if statements (one inside another) Task 1: Get the Pizza Delivery examples (from the lecture) working. Task 2: Modify your program to calculate the total cost of the order, as well as the delivery cost.

Mark Dixon, SoCCE SOFT 136Page 26 Tutorial Exercises: Music Player LEARNING OBJECTIVE: use if statements Task 1: Modify your Music Player (from previous week) to automatically move to the first track (file) in the current folder, when the last track finishes playing. Hint: You will need to use the File List Box’s ListCount property.