30/04/20151 4.3 Selection Nested If structures & Complex Multiple Conditions.

Slides:



Advertisements
Similar presentations
EasyGUI “Probably the Easiest GUI in the world”. Assumptions (Teachers’ Notes) This resources sets out an introduction to using easyGUI and Python
Advertisements

Tutorial 8: Developing an Excel Application
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
08/09/ Arrays Defining, Declaring & Processing.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
A453 Exemplar Password Program using VBA
Mr C Johnston ICT Teacher BTEC IT Unit 06 - Lesson 05 Learning To Program.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
27/05/ Iteration Loops Nested Loops & The Step Parameter.
30/10/ Iteration Loops Do While (condition is true) … Loop.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
22/11/ Selection If selection construct.
Validation "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Lesson 1. Security At the menu bar at the top you will see the word Tools. Click your mouse on Tools scroll down to Macro. Move the Mouse over and down.
31/01/ Selection If selection construct.
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Controlling Program Flow with Decision Structures.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Macros in Excel Using VBA Time Required – 5 hours.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
IE 8580 Module 4: DIY Monte Carlo Simulation
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Conditions and Ifs BIS1523 – Lecture 8.
If selection construct
Do While (condition is true) … Loop
4.1 Strings ASCII & Processing Strings with the Functions
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
If selection construct
Do … Loop Until (condition is true)
1D Arrays Defining, Declaring & Processing
3.1 Iteration Loops For … To … Next 18/01/2019.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Random Access Files / Direct Access Files
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Boolean Expressions to Make Comparisons
Review of Previous Lesson
Review of Previous Lesson
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
10.3 Procedures Function Procedures 07/06/2019.
3.2 Working with Data Scope of variables 29/07/2019.
8 Records 25/07/2019.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

30/04/ Selection Nested If structures & Complex Multiple Conditions

2 Learning Objectives State what is needed for each If statement in Nested If Structures. State which has precedence: And / Or Boolean statements and note that this can cause problems in complex multiple conditions.

3 Nested If Structures An alternative to using multiple And conditions.

4 Multiple And Boolean Statement So instead of using a multiple And condition like:

5 Multiple And Boolean Statement Dim Age As Integer Dim Gender As String Age = txtAge.Text Gender = txtGender.Text If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? MsgBox (“Allow into nightclub.”) MsgBox (“Allow into nightclub.”) Else ‘ Everybody else MsgBox (“Do not allow into nightclub.”) MsgBox (“Do not allow into nightclub.”) End If

6 Nested If Structure We could use:

7 Nested If Structure Dim Age As Integer Dim Gender As String Age = txtAge.Text Gender = txtGender.Text If Age >= 18 Then ‘Aged 18 or over? If Gender = “F” Then ‘and female? If Gender = “F” Then ‘and female? lblMessage.Text = “Allow into nightclub.” Else ‘All other people. Else ‘All other people. lblMessage.Text = “Do not allow into nightclub!” End If End If End If 12

8 Nested If Structures We can also replace ElseIf structures with nested If structures.

9 ElseIf structures So instead of:

10 ElseIf structures Dim Mark As Integer Mark = InputBox(“Enter an exam mark from 0 to 100.”) If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) MsgBox (“Merit”) ElseIf Mark >= 40 Then ‘Mark ? MsgBox (“Pass”) MsgBox (“Pass”) Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) MsgBox (“A mark of “ & Mark & “ is a fail.”) End If

11 Nested If Structures We can use:

12 Nested If Structures If Mark >=60 Then ‘Mark 60 or more? MsgBox (“Merit”) MsgBox (“Merit”)Else If Mark >= 40 Then ‘Mark ? If Mark >= 40 Then ‘Mark ? MsgBox (“Pass”) Else ‘Mark under 40. Else ‘Mark under 40. MsgBox (“A mark of “ & Mark & “ is a fail.”) End If End If End If 1 2

13 Nested If Structures Note: If you use nested If structures you need to remember to use an End If statement for each If statement. If you use nested If structures you need to remember to use an End If statement for each If statement.

14 Program 4.3 Rent a property Specification: Illustrate complex multiple conditions: Illustrate complex multiple conditions: More specifically to illustrate the order of precedence of the And & Or logical operators. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house.

15 Program 4.3 Rent a property Create a new project named ‘Rent a property’. Change the form’s Text property to ‘Rent a property’.

16 The Form Two labels. Two list boxes. One button. Set their text / item properties as shown on the form opposite.

17 Program 4.3 Rent a property Name the list boxes lstTypes and lstBedrooms as appropriate. Name the button butRent.

18 Program 4.3 Rent a property butRent code: If (lstTypes.Text = "Cottage") Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then If (lstTypes.Text = "Cottage") Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then MsgBox("Rent it!") Else Else MsgBox("Don’t rent it!") End If End If

19 Program 4.3 Rent a property Run the program and test all possibilities. You should find a problem! What is it?

20 Program 4.3 Rent a property You should have found that if it is a cottage and the number of bedrooms is lower than 4 the program still says ‘Rent it!’.

21 Program 4.3 Rent a property This is because And is done before Or. So the program interprets the code like this: (lstTypes.Text = "Cottage") (lstTypes.Text = "Cottage") Or Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) So if it is Detached then this works as both the type and the number of bedrooms has to be true but if it is a cottage then the type or the number of bedrooms is required to be true; so it doesn’t work.

22 Program 4.3 Rent a property Replace the Rent button’s template code with: If (lstTypes.Text = "Cottage") And (lstBedrooms.Text >= 4) Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then If (lstTypes.Text = "Cottage") And (lstBedrooms.Text >= 4) Or (lstTypes.Text = "Detached") And (lstBedrooms.Text >= 4) Then MsgBox("Rent it!") Else Else MsgBox("Don’t rent it!") End If End If

23 Program 4.3 Rent a property Run the program and test all possibilities. It should work perfectly now.

24 Extension “Work Hours” Program 1 For each employee the hours worked module collects data for five days. If the user leaves a day empty give a message box asking the user to enter a 0. Use one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. Each person can work up to 9 hours a day for up to 5 days a week. Use one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. No-one may work more than 40 hours. If all the above requirements are met then the hours are added up and displayed. I suggest 5 text boxes (one for each day), a button and a label to display the Total Number of Hours worked. I suggest 5 text boxes (one for each day), a button and a label to display the Total Number of Hours worked.

2530/04/2015 Concatenation contents of the Name variable contents of the Age variable Note the spaces needed to create a readable message. Join variables and strings together using the & operator. e.g. Putting a variable in a message: e.g. Putting a variable in a message: MsgBox (Name & “, you are “ & Age & “ years old.”) MsgBox (Name & “, you are “ & Age & “ years old.”) Will show: Will show: ……, you are … years old.

26 Extension “Vehicle Type” Program 2 Vehicle Type: Enter See the next slide for more details.

27 Extension “Vehicle Type” Program 2 Write a program to allow only car, motorbike and lorry as valid vehicle types. Note that you must declare a variable to store the vehicle type so this will be the “first” time you will need to declare a variable as “String” as it will not be a number. Invalid vehicle types should produce the error message: Invalid vehicle types should produce the error message: “Invalid”. Use one If with either And / Or – you will need to think/find out which is suitable. Use one If with either And / Or – you will need to think/find out which is suitable. Valid vehicle types should produce a message: Valid vehicle types should produce a message: ….. is valid ….. is valid Does the program also accept Car, Motorbike and Lorry? Find out but do not attempt to change this. Please explain what happens and why in your comments. vehicle type Hint: Use concatenation – see previous slides. concatenation

28 Extension Program “Password Attempts” 3 Password: Enter See the next slide for more details. EnterEnter Password: Enter Password:

29 Extension Program “Password Attempts” 3 You choose the password but it must be a combination of letters and numbers. Write a program that will check an entered password. First test if the password the user has entered is correct. First test if the password the user has entered is correct. If PasswordEntered = “…….” If the password is correct then display a suitable message e.g. “Password Correct!”. If the password is correct then display a suitable message e.g. “Password Correct!”. If the password is incorrect then increment the number of attempts by one. If the password is incorrect then increment the number of attempts by one. Hint: Do you need a global variable? If Attempt = 1, output “First try is wrong. Please try again.” If Attempt = 1, output “First try is wrong. Please try again.” On the second try output “Password still wrong. One more chance.” On the second try output “Password still wrong. One more chance.” On the third try output “No valid password entered.” On the third try output “No valid password entered.”

30 Extension “Winner or Winners” Program 4 Write a program to accept three marks for three candidates Candidate A, Candidate B and Candidate C. The program should display the winner (highest mark). Extension: Extension: Adapt the program to deal correctly with three or two of the candidates receiving equal marks. Hints: 1. Test for clear winners first, then for three winners and then for two winners. 2. Either use: A series of ElseIf statements. A series of ElseIf statements.Or Separate If statements with Exit Sub after each winner is declared Separate If statements with Exit Sub after each winner is declared So as to help make other IF’s less complicated or run the chance of a correct winner being replaced by incorrect winners later on in the program.

31 Extension “Mouse Clicks” Program 5 Extend the “Global Variables/Mouse Clicks” program: 3.2 Working with Data 3.2 Working with Data 3.2 Working with Data 3.2 Working with Data Rename the original button as “Button A” and add two similar buttons “Button B” and “Button C”. The program should record how many times each button is clicked but should no longer display these numbers on the screen as they are clicked. Instead, add an extra “Winner” button which, when clicked, displays, the name of the button which was clicked the most times (i.e. the “Winner”). Also either: Add a “Reset” button. Add a “Reset” button.Or Declare the “The winner/s was/were” and then reset automatically. Declare the “The winner/s was/were” and then reset automatically. Do not use “Application.Reset” as exams will not allow you to use this “simplistic” way. Please use the more “manual complex” way. Do not use “Application.Reset” as exams will not allow you to use this “simplistic” way. Please use the more “manual complex” way.

32 Plenary What is needed for each If statement in Nested If Structures? End If End If Which has precedence: And / Or? And And