Chapter 4 Decisions and Conditions Programming In Visual Basic.NET.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

1.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Chapter 4 Decisions and Conditions Programming In Visual Basic.NET.
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
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.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
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.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Tutorial 11 Using and Writing Visual Basic for Applications Code
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Chapter 7 Decision Making. Class 7: Decision Making Use the Boolean data type in decision-making statements Use If statements and Select Case statements.
Chapter 4 Decisions and Conditions Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
Chapter 2 More Controls Programming in C#. NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 6 Decisions and Conditions.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 4: The Selection Process in Visual Basic.
Chapter 4: The Selection Structure
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Decisions and Conditions.
4-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Decisions and Conditions.
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.
Chapter 5: More on the Selection Structure
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
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.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Decisions with Select Case and Strings Chapter 4 Part 2.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
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.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 4: Decisions and Conditions
A variable is a name for a value stored in memory.
Chapter 4: Decisions and Conditions
Brief description on how to navigate within this presentation (ppt)
The Selection Structure
CHAPTER FIVE Decision Structures.
Chapter 4: The Selection Structure
CHAPTER FIVE Decision Structures.
Variables and Arithmetic Operations
Making Decisions in a Program
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
Microsoft Visual Basic 2005: Reloaded Second Edition
Brief description on how to navigate within this presentation (ppt)
Chapter 4 Decisions and Conditions
Brief description on how to navigate within this presentation (ppt)
Presentation transcript:

Chapter 4 Decisions and Conditions Programming In Visual Basic.NET

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved If Statements Used to make decisionsUsed to make decisions If true, only the Then clause is executed, if false, only Else clause, if present, is executedIf true, only the Then clause is executed, if false, only Else clause, if present, is executed Block If…Then…Else must always conclude with End IfBlock If…Then…Else must always conclude with End If Then must be on same line as If or ElseIfThen must be on same line as If or ElseIf End If and Else must appear alone on a lineEnd If and Else must appear alone on a line Note: ElseIf is 1 word, End If is 2 wordsNote: ElseIf is 1 word, End If is 2 words

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved If…Then…Else – General Form If (condition) Then statement(s) [ElseIf (condition) Then statement(s)] [Else statement(s)] End If Condition True False StatementStatement

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved If…Then…Else - Example unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D Then freshmanRadioButton.Checked = True Else freshmanRadioButton.Checked = False End If

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Conditions Test in an If statement is based on a conditionTest in an If statement is based on a condition Six relational operators are used for comparisonSix relational operators are used for comparison Negative numbers are less than positive numbersNegative numbers are less than positive numbers An equal sign is used to test for equalityAn equal sign is used to test for equality Strings can be compared, enclose strings in quotes (see Page 142 for ANSI Chart, case matters)Strings can be compared, enclose strings in quotes (see Page 142 for ANSI Chart, case matters) –JOAN is less than JOHN –HOPE is less than HOPELESS Numbers are always less than lettersNumbers are always less than letters –300ZX is less than Porsche

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved The Six Relational Operators Greater Than>Greater Than> Less Than<Less Than< Equal To=Equal To= Not Equal To<>Not Equal To<> Greater Than or Equal To>=Greater Than or Equal To>= Less Than or Equal to<=Less Than or Equal to<=

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved ToUpper and ToLower Methods Use ToUpper and ToLower methods of the String class to return the uppercase or lowercase equivalent of a string, respectivelyUse ToUpper and ToLower methods of the String class to return the uppercase or lowercase equivalent of a string, respectively If nameTextBox.Text.ToUpper( ) = "Basic" Then ' Do something. End If

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Compound Conditions Join conditions using logical operatorsJoin conditions using logical operators –OrIf one or both conditions True, entire condition is True –AndBoth conditions must be True for entire condition to be True –NotReverses the condition, a True condition will evaluate False and vice versa ORTF T F TT TF Condition 1 Condition 2 ANDTF T F TF FF Condition 1 Condition 2

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Compound Condition Examples If maleRadioButton.Checked And _ Integer.Parse(ageTextBox.Text) < 21 Then minorMaleCountInteger += 1 End If If juniorRadioButton.Checked Or seniorRadioButton.Checked Then upperClassmanInteger += 1 End If

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Combining And and Or Example If saleDecimal > Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved If tempInteger > 32 Then If tempInteger > 80 Then commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" End If Else commentLabel.Text = "Freezing" End If Nested If Statements

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Using If Statements with Radio Buttons & Check Boxes Instead of coding the CheckedChanged events, use If statements to see which are selectedInstead of coding the CheckedChanged events, use If statements to see which are selected Place the If statement in the Click event for a Button, such as an OK or Apply buttonPlace the If statement in the Click event for a Button, such as an OK or Apply button Private Sub testButton_Click(ByVal sender As System.Object, _ By Val e As System.EventArgs) Handles testButton.Click ' Test the value of the check box. If testCheckBox.Checked Then messageLabel.Text = "Check box is checked" End If End Sub

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Enhancing Message Boxes For longer, more complex messages, store the message text in a String variable and use that variable as an argument of the Show methodFor longer, more complex messages, store the message text in a String variable and use that variable as an argument of the Show method VB will wrap longer messages to a second lineVB will wrap longer messages to a second line Include ControlChars to control the line length and position of the line breakInclude ControlChars to control the line length and position of the line break

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved ControlChars Constants (p 152) ConstantDescription CRCarriage Return CRLFCarriage Return + Line Feed NewLineCarriage Return + Line Feed TabTab Character NullCharCharacter with a Value of Zero QuoteQuotation Mark Character

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Message Box - Multiple Lines of Output ControlChars.NewLine Used to force to next line

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Message String Example Dim formattedTotalString As String Dim formattedAvgString As String Dim messageString As String formattedTotalString = totalSalesDecimal.ToString( " N " ) formattedAvgString = averageSalesDecimal.ToString( " N " ) messageString = "Total Sales: " & formattedTotalString _ & ControlChars.NewLine & "Average Sale: " & _ formattedAvgString MessageBox.Show(messageString, " Sales Summary ", _ MessageBoxButtons.OK)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Displaying Multiple Buttons Use MessageBoxButtons constants to display more than one button in the Message BoxUse MessageBoxButtons constants to display more than one button in the Message Box Message Box's Show method returns a DialogResult object that can be checked to see which button the user clickedMessage Box's Show method returns a DialogResult object that can be checked to see which button the user clicked Declare a variable to hold an instance of the DialogResult type to capture the outcome of the Show methodDeclare a variable to hold an instance of the DialogResult type to capture the outcome of the Show method

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Message Box - Multiple Buttons MessageBoxButtons.YesNo

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Declaring a Variable for the Method Return Dim whichButtonDialogResult As DialogResult whichButtonDialogResult = MessageBox.Show _ ("Clear the current order figures?", "Clear Order", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) If whichButtonDialogResult = DialogResult.Yes Then ' Code to clear the order. End If

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Specifying a Default Button and Options Use a different signature for the Message Box Show method to specify a default buttonUse a different signature for the Message Box Show method to specify a default button Add the MessageBoxDefaultButton argument after the MessageBoxIcons argumentAdd the MessageBoxDefaultButton argument after the MessageBoxIcons argument Set message alignment with MessageBoxOptions argumentSet message alignment with MessageBoxOptions argument

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Input Validation Check to see if valid values were entered by user before beginning calculationsCheck to see if valid values were entered by user before beginning calculations Check for a range of values (reasonableness)Check for a range of values (reasonableness) –If Integer.Parse(hoursTextBox.Text) > 10 Then MessageBox.Show("Too many hours") Check for a required field (not blank)Check for a required field (not blank) –If nameTextBox.Text <> "" Then...

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Performing Multiple Validations Use nested If statement to validate multiple values on a formUse nested If statement to validate multiple values on a form –Examine example on Page 156 Use Case structure to validate multiple valuesUse Case structure to validate multiple values –Simpler and clearer than nested If –No limit to number of statements that follow a Case statement –When using a relational operator must use the word Is –Use the word To to indicate a range of constants

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Sharing an Event Procedure Add events to the Handles clause at the top of an event procedureAdd events to the Handles clause at the top of an event procedure –Allows the procedure to respond to events of other controls Key to using a shared event procedure is the sender argumentKey to using a shared event procedure is the sender argument –Cast (convert) sender to a specific object type using the CType function

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Calling Event Procedures Reusable codeReusable code General FormGeneral Form –[Call] ProcedureName ( ) –Keyword Call is optional and rarely used ExamplesExamples –Call clearButton_Click (sender, e) OR –clearButton_Click (sender, e)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Debugging (p 169) Debug MenuDebug Menu Debug ToolbarDebug Toolbar Toggle BreakPoints on/off by clicking Editor's gray left margin indicatorToggle BreakPoints on/off by clicking Editor's gray left margin indicator Step through Code, Step Into, Step OverStep through Code, Step Into, Step Over View the values of properties, variables, mathematical expressions, and conditionsView the values of properties, variables, mathematical expressions, and conditions

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Debugging (cont.) Output WindowOutput Window Locals WindowLocals Window Autos WindowAutos Window

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Debug Menu and Toolbar

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Writing to the Output Window Debug.WriteLine(TextString)Debug.WriteLine(TextString) Debug.WriteLine(Object)Debug.WriteLine(Object) Debug.WriteLine("calculateButton procedure entered") Debug.WriteLine(quantityTextBox)

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Breakpoints Toggle Breakpoints On/Off by clicking in Editor's gray left margin indicator

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Checking the Current Value of Expressions Place mouse pointer over variable or property to view current value

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Locals Window Shows values of local variables that are within scope of current statement

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved Autos Window Automatically adjusts to show variables and properties that appear in previous and next few lines

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved