1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.

Slides:



Advertisements
Similar presentations
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Advertisements

Programming Logic and Design Sixth Edition
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Selection in C.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 4: The Selection Structure
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
26/10/ Selection Nested If structures & Complex Multiple Conditions.
30/10/ Iteration Loops Do While (condition is true) … Loop.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
22/11/ Selection If selection construct.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
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!!
Chapter 51 Logical Operators Used with Boolean expressions Not – makes a False expression True and vice versa And – will yield a True if and only if both.
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.
31/01/ Selection If selection construct.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
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.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
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.
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.
Selection Using IF THEN ELSE CASE Introducing Loops.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Introduction to Decision Structures and Boolean Variables
Data Types Variables are used in programs to store items of data e.g a name, a high score, an exam mark. The data stored in a variable is entered from.
Control Structures: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Statement Examples
Introduction to C++ Programming
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)
Text / Serial / Sequential Files
Problem Solving and Control Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Random Access Files / Direct Access Files
Boolean Expressions to Make Comparisons
Programming Logic and Design Fifth Edition, Comprehensive
The Selection Structure
Review of Previous Lesson
Review of Previous Lesson
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

1 2.2 Selection Logical Operators

2 Learning Objectives Explain how the logical operator AND Boolean statements works.

3 Using brackets in If structures From this point we will use brackets in If structures. You do not have to use them but they make complex If structures more readable. State how to set an initial value of a variable when it is declared.

4 Testing multiple Boolean Statement conditions The boolean condition has so far consisted of one test. A multiple boolean condition has two or more tests and each one is either true or false. For this you need to use VB’s logical operators.

5 Logical Operators (Boolean Statements) The two main ones are: And And When you And two or more conditions each one must be true for the overall condition to be true. If just one of them is false the overall condition is false. Or Or When you Or two or more conditions, then if at least one of them is true the overall condition is true. They must all be false for the overall condition to be false. Ands have precedence over Ors Ands have precedence over Ors

6 Program 2.2a Night Club Specification: A program to test conditions for customers waiting to enter into a club holding a ladies only night: A program to test conditions for customers waiting to enter into a club holding a ladies only night: Age >= 18 Gender = “F” Each of these is either true or false. Each of these is either true or false. Anybody else should be refused entry with a simple general message: Anybody else should be refused entry with a simple general message: “Do not allow into nightclub!”.

7 Program 2.2a Night Club Dim Age As Integer Dim Gender As String Console.WriteLine(“Please enter your age.”) Age = Console.ReadLine Console.WriteLine(“Please enter your gender (M/F).”) Gender = Console.ReadLine If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? Console.WriteLine (“Allow into nightclub.”) Console.WriteLine (“Allow into nightclub.”) Else ‘ Everybody else Console.WriteLine(“Do not allow into nightclub!”) Console.WriteLine(“Do not allow into nightclub!”) End If

8 Program 2.2b Bowling Club Members of a ten-pin bowling club get an award if, during one season, they score at least 240 points on 5 or more occasions, or they score 200 points on 10 or more occasions.

9 Or Boolean Statement Dim TwoForty As Integer ‘ Dim TwoHundred As Integer ‘Note that VB will not allow numbers as identifiers. Console.WriteLine(“How many times have you scored 240 points?”) TwoForty = Console.ReadLine Console.WriteLine(“How many times have you scored 200 points?”) TwoHundred = Console.ReadLine If (TwoForty >= 5) Or (TwoHundred >= 10) Then ‘Scored at least 240 points on 5 or more occasions, or 200 points on 10 or more occasions? Console.WriteLine (“Give award.”) Console.WriteLine (“Give award.”) End If

Logical Expressions If an exam asks for “a logical expression” then this means a “single logical expression”. Single Logical Expression: Single Logical Expression: If … = … And … = … Then If … = … Or … = … Then If combination of And’s, Or’s, () Then Basically one If statement with “And” &/ “Or ”. Basically one If statement with “And” &/ “Or ”. NOT nested If statements. 10

1116/10/2015 Extension “Hotel” Program 2.2c Write a program for a person wishing to attend an overnight conference: They cannot afford to pay more than €40.00 for their hotel but it must be no more than 3km from the conference hall. They cannot afford to pay more than €40.00 for their hotel but it must be no more than 3km from the conference hall. Use a Logical Expression. The program should ask for the cost per night and distance from the conference hall and then display a message stating whether the booking should be made or not. The program should ask for the cost per night and distance from the conference hall and then display a message stating whether the booking should be made or not. Use a Logical Expression. Extension: Extension: Give appropriate messages if the distance and the cost are not good (and what they should be), if the distance is good but the price is not (and what it should be) and vice versa.

12 Extension “Salary” Program 2.2d Extend the “Salary” Program written in 1 Variables/Identifiers. 1 Variables/Identifiers1 Variables/Identifiers Limit the user so that they cannot work more than 40 hours. Limit the user so that they cannot work more than 40 hours. Includes normal hours and overtime hours. Use a logical expression. Use a logical expression. Note: Note: Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Total Hours Restricted Version 2.2d).

13 Extension “Deciding Exam Grades” Program 2.2e. Change the “Deciding Exam Grades” Program written in 2.1 Selection. 2.1 Selection2.1 Selection To give a general error message. To give a general error message. “You have entered an invalid mark!” Deciding Exam Grades 2.2e: Deciding Exam Grades 2.2e: Use a logical expression that is false when the mark is within the range and true when the mark is outside the range of 0 – 100 and use Or to produce the general error message above. Note: Note: Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (FalseIfValid Version 2.2e).

14 Extension “Vehicle Type” Program 2.2f 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 a logical expression. Use a logical expression. 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 presentation 2.1. concatenation2.1concatenation2.1

15 Extension “Work Hours” Program 2.2g For each employee the hours worked module collects data for five days. Each person can work up to 9 hours a day for up to 5 days a week. Use a logical expression. Use a logical expression. No-one may work more than 40 hours. If all the above requirements are met then the hours are added up and displayed.

16 Extension “Winner or Winners” Program 2.2h 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.

17 Plenary Explain how the logical operator AND works. When you And two or more conditions each one must be true for the overall condition to be true. When you And two or more conditions each one must be true for the overall condition to be true. If just one of them is false the overall condition is false. If just one of them is false the overall condition is false.