Selection: Boolean Expressions (2) Precedence chart:



Similar documents
Visual Basic Programming. An Introduction

Chapter 5. Selection 5-1

Conditions & Boolean Expressions

Wage Calculator Application

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Microsoft Excel 2010 Part 3: Advanced Excel

Windows Phone 7 Mobile Applications Using Decision Structures

PL / SQL Basics. Chapter 3

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Data Analysis with Microsoft Excel 2003

Advanced Programming with LEGO NXT MindStorms

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

VB.NET Programming Fundamentals

PL/SQL TUTORIAL. Simply Easy Learning by tutorialspoint.com. tutorialspoint.com

Alarms & Events Plug-In Help Kepware, Inc.

Banner Workflow. Creating FOAPAL Requests

Lecture 2 Notes: Flow of Control

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Play a Sound file in Visual Basic 6

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Changing the Display Frequency During Scanning Within an ImageControls 3 Application

Final Exam Review: VBA

Python Programming: An Introduction to Computer Science

PL/SQL MOCK TEST PL/SQL MOCK TEST I

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

MICROSOFT EXCEL FORMULAS

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Program to solve first and second degree equations

Directions to Print from WorkFlows:

1. a procedure that you perform frequently. 2. Create a command. 3. Create a new. 4. Create custom for Excel.

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Introduction to Java

The Center for Teaching, Learning, & Technology

Exercise 4 Learning Python language fundamentals

Lecture 8: Synchronous Digital Systems

Object Oriented Software Design

Python Evaluation Rules

Company Setup 401k Tab

EET 310 Programming Tools

WebSphere Business Monitor

Test Generator. Creating Tests

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

BOOLEAN LOGIC IN PROGRAMMING

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

MATLAB Programming. Problem 1: Sequential

PIC 10A. Lecture 7: Graphics II and intro to the if statement

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

ECE 3401 Lecture 7. Concurrent Statements & Sequential Statements (Process)

Computational Mathematics with Python

Writing Control Structures

Creating a Simple Macro

My EA Builder 1.1 User Guide

J a v a Quiz (Unit 3, Test 0 Practice)

Conditional Statements Summer 2010 Margaret Reid-Miller

A Concise Guide for Beginners LIEW VOON KIONG

A LEVEL H446 COMPUTER SCIENCE. Code Challenges (1 20) August 2015

A Brief Introduction to MySQL

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

SAPScript. A Standard Text is a like our normal documents. In Standard Text, you can create standard documents like letters, articles etc

Hands-On Lab. Client Workflow. Lab version: Last updated: 2/23/2011

FrontPage 2003: Forms

Microsoft Excel Tips & Tricks

I PUC - Computer Science. Practical s Syllabus. Contents

USER GUIDE. Unit 2: Synergy. Chapter 2: Using Schoolwires Synergy

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

Writer Guide. Chapter 15 Using Forms in Writer

Data Tool Platform SQL Development Tools

COMPUTER SCIENCE TRIPOS

This module explains fundamental aspects of Microsoft Dynamics NAV Development Environment.

Computational Mathematics with Python

Windows PowerShell Essentials

Creating a table of contents quickly in Word

Outlook Webb App quick guide

Figure Error! No text of specified style in document..1: Project Organization

Sources: On the Web: Slides will be available on:

Chapter 15 Using Forms in Writer

COMP 110 Prasun Dewan 1

Eventia Log Parsing Editor 1.0 Administration Guide

SonicWALL GMS Custom Reports

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Excel. Microsoft Office s spreadsheet application can be used to track. and analyze numerical data for display on screen or in printed

Government Girls Polytechnic, Bilaspur

Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

Website Builder Overview

Application Unit, MDRC AB/S 1.1, GH Q R0111

In this example, Mrs. Smith is looking to create graphs that represent the ethnic diversity of the 24 students in her 4 th grade class.

Creating Basic Excel Formulas

Visual Logic Instructions and Assignments

USING EXCEL 2010 TO SOLVE LINEAR PROGRAMMING PROBLEMS MTH 125 Chapter 4

Business Insight Report Authoring Getting Started Guide

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

USC Marshall School of Business Marshall Information Services

Microsoft Excel 2007 Level 2

Excel 2007 Basic knowledge

VISUAL BASIC Controls. Version 1 Trg. Div., NIC HQRs

Chapter 9 Creating Reports in Excel

Transcription:

Selection: Boolean Expressions Boolan operators: Return a Boolean value Types: 1. Relational operators Operators: (a) < (less than) (b) > (greater than) (c) = (equal to) (d) <= (less than or equal to) (e) >= (greater than or equal to) (f) <> (not equal to) Arguments must be type-compatible Note that assignment operator and equivalence test use same operator When using inequalities: (a) Numeric types as you would expect (b) Character comparison based on position in collating sequence (ASCII) (c) String comparison uses dictionary-style (d) Date comparisons based on calendar sequence (e) F alse < T rue 2. Logical operators Operators: (a) And (b) Or (c) Not (d) Xor (exclusive or) Arguments must be Boolean Values represented by truth tables Graphical way of expressing value of Boolean expression A B A And B A Or B A Xor B Not A True True True True False False True False False True True False False True False True True True False False False False False True 1

Selection: Boolean Expressions (2) Precedence chart: operator highest () unary +, - *, / +, - relational operators Not And, AndAlso Or, OrElse lowest Xor Boolean functions: 1. String functions: (a).endswith(str): hello.endswith( lo ) (b).startswith(str): hello.startswith( hell ) (c).equals(str): hello.equals( hello ) (d).compareto(str): hello.compareto( hello ) Returns 0 if caller equals str Returns -1 if caller < str Returns +1 if caller > str (e) IsNumeric(str): IsNumeric( $5,000,123 ) 2. Char functions (a).isdigit(c) (b).isletter(c) (c).isletterordigit(c) (d).islower(c) (e).isupper(c) (f).ispunctuation(c) (g).issymbol(c) (e.g., operators: +, =, ˆ) (h).iswhitespace(c) Note:.Equals and.compareto available for all data types 2

Short circuit evaluation Selection: Boolean Expressions (3) Consider the Boolean expression a And b If a is F alse, there is no need to evaluate b VB provides two additional Boolean operators that perform short circuit evaluation They only evaluate as much as is needed to determine the truth value of the expression AndAlso and OrElse have the same semantics as And and Or, respectively, but use short circuit evaluation 3

Basic selection statement Syntax (limited): If Boolean-expression Then statements Else statements Semantics: Selection: If-Then-Else If Boolean-expression is True, execute statements after Then If Boolean-expression is False, execute statements after Else Note that the Else component is optional 4

Selection: If-Then-Else Examples Simple If-Then: pay = hours * rate If hours > 40 Then pay = pay + (hours - 40) * rate * 0.5 Simple If-Then-Else example: If hours <= 40 Then pay = hours * rate Else pay = 40 * rate + ((hours - 40) * 1.5 * rate) Nested If-Then-Else example: If temp < 100 Then txtwarn.text = "WARNING: Temperature below normal" Else If temp > 200 Then txtwarn.text = "WARNING: Elevated temperature" Else txtwarn.text = "Normal operating temperature" 5

The complete syntax for If is If Boolean-expression 1 Then statements 1 ElseIf Boolean-expression 2 Then statements 2... Else statements n Semantics: Selection: If-Then-ElseIf If Boolean-expression 1 is True, execute statements 1 If Boolean-expression 1 is False, check Boolean-expression 2... This format much more convenient than using separate nested ifs 6

If-Then-ElseIf example: Selection: If-Then-ElseIf Example If temp < 100 Then txtwarn.text = "WARNING: Temperature below normal" ElseIf temp > 200 Then txtwarn.text = "WARNING: Elevated temperature" Else txtwarn.text = "Normal operating temperature" 7

Selection: If-Then-Else Style Improper use of nested If-Then-Else: How many exemptions does someone over 65, married to someone over 65 and filing jointly receive? Dim married As Boolean Dim age, spouseage, dependents, exemptions As Integer Dim filingstatus As Char... If age > 65 Then exemptions = exemptions + 1 ElseIf spouseage > 65 Then exemptions = exemptions + 1 ElseIf (dependents > 0) Then exemptions = exemptions + dependents ElseIf married And (filingstatus = "J") Then exemptions = exemptions + 1 Proper version of nested If-Then-Else:... If age > 65 Then exemptions = exemptions + 1 If spouseage > 65 Then exemptions = exemptions + 1 If (dependents > 0) Then exemptions = exemptions + dependents If married And (filingstatus = "J") Then exemptions = exemptions + 1 8

Selection: If-Then-Else Style (2) Improper use of sequential ifs: What letter grade does someone with a 95 get? If avg >= 90 Then grade = "A" If avg >= 80 Then grade = "B" If avg >= 70 Then grade = "C" If avg >= 60 Then grade = "D" If avg < 60 Then grade = "F" Better use of sequential ifs If avg >= 90 Then grade = "A" If (avg >= 80) And (avg < 90) Then grade = "B" If (avg >= 70) And (avg < 80) Then grade = "C" If (avg >= 60) And (avg < 70) Then grade = "D" If avg < 60 Then grade = "F" 9

Selection: If-Then-Else Style (3) Best use of nested ifs If avg >= 90 Then grade = "A" ElseIf avg >= 80 Then grade = "B" ElseIf avg >= 70 Then grade = "C" ElseIf avg >= 60 Then grade = "D" Else grade = "F" 10

Selection: Select Case Select statement is an alternative to nested If-ElseIf Syntax: Select Case expression Case valuelist 1 statements 1 Case valuelist 2 statements 2... Case Else statements n End Select valuelist is a comma-separated list of values (if more than one value) A value may have any of the following forms Let a value-returning-expression be either a literal, variable, or expression 1. value-returning-expression 2. Is relop value-returning-expression 3. value-returning-expression 1 To value-returning-expression 2 where value-returning-expression 1 < value-returning-expression 2 Semantics: expression is evaluated The Case clauses are evaluated one by-by-one until the value of expression meets the condition of valuelist When a Case is True, the statements following it are executed If no Case is True, either the statements following the Else are executed, or execution continues to the statement following the End Select 11

Selection: Select Examples Simple example: Select Case year Mod 4 Case 0 MessageBox.Show("This is an election year") Case 1 MessageBox.Show("Last year was an election year") Case 2 MessageBox.Show("Two years ago was an election year") Case 3 MessageBox.Show("Next year is an election year") End Select Another example: Select Case avg Case Is >= 90 grade = "A" Case Is >= 80 grade = "B" Case Is >= 70 grade = "C" Case Is >= 60 grade = "D" Case Else grade = "F" End Select And another: Select Case temp Case 100 To 200 MessageBox.Show("Normal operating temperature") Case Is < 100 MessageBox.Show("WARNING: Temperature below normal") Case Else MessageBox.Show("WARNING: Elevated temperature") End Select 12

Selection: Input Based on User Selection The following examines several controls that allow the user to select from a set of alternatives provided by the program 1. List Box Allows prgrammer to provide a list of choices for the user to select from (like a menu) To add items to a List Box, select String Collection Editor from the control s task button To add them during program execution, assign string values using lst- Box.Items.Add(str) Selection is made by clicking on an item When clicked, the string value is assigned to the List Box Text property The List Box items are numbered by VB (zero-based) 13

Selection: Input Based on User Selection (2) Properties: (a).selecteditem returns a reference to the item that was selected If value is Nothing, no item was selected If.SelectedItem is assigned Nothing, any selection is cleared (deselected) (b).selectedindex returns the index of the selected item If value is -1, no item was selected (c).sorted If value is True, items are displayed in alphabetic order (d).selectionmode determines how many items can be selected at one time If value is None, nothing can be selected If value is One, only one item can be selected at any one time Methods: (a).clearselected() clears all selections Events: (a) SelectedIndexChanged is triggered when an item is selected List boxes tend to be used when there are many options for the user to choose from 2. Group Box A Group Box is a container that holds other objects (controls) as a group These objects are said to be attached to the group box To add controls to a group box, simply drag them into it Events are not usually associated with a group box 14

3. Radio Button Selection: Input Based on User Selection (3) Displayed as a circle and label Usually displayed in a Group Box Radio buttons in a group box are mutually exclusive - only one can be set at any one time Cannot be toggled by reclicking Can associate an access key Properties: (a).text: label that is displayed (b).checked: Boolean that indicated whther the button is set Can provide the user a default by initially setting this to True Events: (a) CheckChanged is triggered when the button is clicked Generally used for short lists (seven or fewer options) 4. Check Box Displayed as a square and label Usually displayed in a Group Box Can select a many as you like at any one time Can be toggled by reclicking Can associate an access key Properties: (a).text: label that is displayed (b).checked: Boolean that indicated whther the check box is set Can provide the user a default by initially setting this to True Events: (a) CheckChanged is triggered when the button is clicked 15

Selection: Input Based on User Selection (4) General note: Each of the above can have events associated it In such a situation, the event will be triggered as soon as the user interacts with that control Frequently want to allow the user to make multiple selections using a combination of controls In this case, would not want to associate events with the individual controls Rather, provide a button for the user to click once the user has finished making selections Processing of the above controls is achieved using selection, as the program must check each control to determine which list box selection, radio button, and check box selections were set by the user 16