CSC110 Fall 20041 Chapter 5: Decision Visual Basic.NET.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
ITEC113 Algorithms and Programming Techniques
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Visual Basic Chapter 1 Mr. Wangler.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Decision Structures and Boolean Logic
Chapter 4: The Selection Process in Visual Basic.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Using Client-Side Scripts to Enhance Web Applications 1.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 11 Conditional.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Decisions with Select Case and Strings Chapter 4 Part 2.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic.NET BASICS Lesson 9 Nested If Statements and Radio Buttons.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Visual Basic.NET Windows Programming
Chapter 4: Decisions and Conditions
Chapter 4: Making Decisions.
The Selection Structure
CHAPTER FIVE Decision Structures.
Topics The if Statement The if-else Statement Comparing Strings
CHAPTER FIVE Decision Structures.
More Selections BIS1523 – Lecture 9.
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Decisions and Conditions
Microsoft Visual Basic 2005: Reloaded Second Edition
Presentation transcript:

CSC110 Fall Chapter 5: Decision Visual Basic.NET

2CSC110 Fall 2004 Objectives n Understand relational and logical operators and use them in logical expressions n Code the If block structure to solve various programming problems that involve decisions n Appreciate and design various alternatives to the If block n Understand and use the Select Case block structure

3CSC110 Fall 2004 Logical Expressions n If statement has simple syntax: –If Condition Then Statement »Condition: an expression that can be evaluated as true or false »Statement: a VB statement

4CSC110 Fall 2004 Relational Operators n Used to compare operands and decide if the relationship is true –Examples include =, >, and, and <= n With string data, “a” is greater than “A” –To make comparison case insensitive, set Option Compare to “Text” »Set in code with Option Compare Text statement »Set as default in properties page for project n Logical expression can be part of assignment statement –Left side of expression must be a variable or a property of a control that can be set to True or False

5CSC110 Fall 2004 Logical Operators n Compares two or more expressions and returns appropriate value –Not operator negates operand on its right –And operator returns True when all expressions are True –Or operator returns True if one or more expressions are True

6CSC110 Fall 2004 Order of Operations n If a1 a4 and a5 a4 and a5 < a6 Then n And has precedence over Or n All comparison operators have precedence over all logical operators n Use parentheses to alter the order of evaluation

7CSC110 Fall 2004 The If Block n Similar to If statement, but statements to be executed are not on same line as If keyword –Allows you to execute more than one statement if condition is True n Terminated with End If statement

8CSC110 Fall 2004 The If…Then…Else…End If Block n Adds an Else clause –Contains statements to be executed if the condition is False n Terminated with End If keyword n Either clause can be left blank –Often used for statements that will be carried out only if condition is False If chkSort.Checked Then cboSort.Enabled = True Else cboSort.Enabled = False End If

9CSC110 Fall 2004 If…Then…ElseIf…Then…Else… End If Block n Used when a decision depends on several conditions n ElseIf keyword used to define each condition n Often has a “catch-all” Else clause n Terminated with End If If Score >= 90 Then Grade = “A” ElseIf Score >=80 Then Grade = “B” Else Grade = “C” End If ElseIf Clause also contains Then keyword

10CSC110 Fall 2004 If Statement Considerations n Varying conditions –The ElseIf block is useful when conditions vary –Conditions can be based on entirely different factors n Nesting If Blocks –Using another If block in either the Then clause or the Else clause –Nested block must be terminated before returning to the outer block »Indent each nested block to enhance readability n Use comments liberally –Especially for nested If blocks

11CSC110 Fall 2004 "If" statement and option buttons

12CSC110 Fall 2004 Messages in Message boxes n Special window displaying message to user n Form: MsgBox “message” [,buttons][, “t.b. caption”] n Example: MsgBox “Numeric ID only”, vbOkOnly, “Error”

13CSC110 Fall 2004 Displaying a Message String n Use & to concatenate strings (“Concatenate” means join end to end) n The VB intrinsic constant vbCRLF creates a new line in a string MsgBox strMessage, vbOKOnly, stTitle

14CSC110 Fall 2004 Message box return values ConstantValueDescription vbOK1OK button pressed. vbCancel2Cancel button pressed. vbAbort3Abort button pressed. vbRetry4Retry button pressed. vbIgnore5Ignore button pressed. vbYes6Yes button pressed. vbNo7No button pressed.

15CSC110 Fall 2004 Input Validation n Checking a data type: IsNumeric & IsDate n IsNumeric checks & returns true or false If IsNumeric(txtQty.Text) Then lblDue.Caption = curPrice + CInt(txtQty.text) lblDue.Caption = curPrice + CInt(txtQty.text) n Validating value ranges If Val(txtHours.Text) > 10 And _ Val(txtHours.Text) <= 80 Then... Val(txtHours.Text) <= 80 Then...

16CSC110 Fall 2004 Data Validation n IsDate returns true or false depending on whether or not a value is a date If IsDate(txtData.text) Then … n the VarType function return a number that corresponds to the data type stored in a variant. If VarType(varValue) = 0 Then...

17CSC110 Fall 2004 Calling Event Procedures n An event procedure is a subprocedure that reacts to a specific event such as a button click. n You can call any given event procedure from multiple locations, as long as the procedure is in the same form or is public Example: Call cmdCalculate_Click Example: Call cmdCalculate_Click n Suffix is event, prefix is object name

18CSC110 Fall 2004 Hands on Programming Example

19CSC110 Fall 2004 Select Case Block n Useful when decision depends on different results of the same expression –Begins with Select Case statement »Each condition coded with Case criterion –Should have “catch all” Case Else clause –Terminated with End Select statement n Criteria are mutually exclusive –Once a criterion is found to be true, that block is executed –Place most restrictive criterion at top

20CSC110 Fall 2004 Syntax Rules n To test for equality, give the value, –i.e. Case 80 n To test several values for equality, separate list with commas, –i.e. Case 80, 81, 85 n To specify a closed range, insert the “To” keyword between upper and lower bounds –i.e. Case 80 to 90 n To specify an open range, use the “Is” keyword –i.e. Case Is < 0

21CSC110 Fall 2004 Nesting Select Case Blocks n Similar to nesting If blocks –Nested blocks must terminate before returning to outer block –Make extensive use of comments

22CSC110 Fall 2004 If Statement Nested Inside Select Case Statement If block is terminated before returning to Select Case block

23CSC110 Fall 2004 Application Example: Tuition Calculator n Analyze and determine system requirements –Calculate student tuition »Based on residence status and hours taken n Design visual interface –Need controls for hours taken and residence »Use radio buttons for status, since limited number of options that won’t change »Use text box for hours taken

24CSC110 Fall 2004 Visual Interface Tuition displayed in label, formatted to look like text box Checked property of In State radio button set to true to create Default

25CSC110 Fall 2004 Code Solution Select Case with If then Blocks

26CSC110 Fall 2004 Code solution Nested Select Case

27CSC110 Fall 2004 An Alternative Solution n Use combo boxes to display residence status and range of hours taken –Offers more flexibility, but code is not as clear

28CSC110 Fall 2004 Visual Interface Items added to combo box at runtime; SelectedIndex property used to set default value

29CSC110 Fall 2004 Code the Solution SelectedIndex property used to determine which option is selected

30CSC110 Fall 2004 Block Level Declarations n Variables may be declared inside a block –Variables exist only inside the block –You may declare the same variable name inside each block »i.e. a variable named ID may be declared in the If block, the ElseIf block, and Else block »While you can do this, it is confusing to read and debug –You may not declare a variable name if you have declared a module-level variable with same name

31CSC110 Fall 2004 Summary n Two structures commonly used to handle decisions: If and Select Case n If structure involves testing whether the condition following If keyword is True or False n Relational operators include =, <>, and, and <. Each compares two operands to determine if relation is True n Commonly used logical operators include And, Or, and Not

32CSC110 Fall 2004 Summary If Block n If operational precedence is confusing, use parentheses to encloses expression(s) n Four ways to construct If structure –Simple If statement –Simple If block –If…Else block –If…ElseIf…Else block n If blocks can be nested

33CSC110 Fall 2004 Summary n Use comments to explain the purpose of the condition n Computer evaluates logical or relational expressions differently than we might interpret them n String comparisons can be “Binary” (case sensitive) or “Text” (case insensitive) –A < a “Cat” < “cat”

34CSC110 Fall 2004 Summary Select Case n Select Case structure can replace If…ElseIf block when decision depends on the result of a single expression n Select Case structure can be nested and can also be nested with If structure n Tuition calculation example illustrates how Select Case structure can be nested n You can declare block level variables in both the If blocks and the Case blocks