Chapter 4: Decisions and Conditions

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Programming with Microsoft Visual Basic th Edition
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Chapter 4 Decisions and Conditions Programming In Visual Basic.NET.
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.
Chapter 4: Control Structures: Selection
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Lecture Set 5 Control Structures Part A - Decisions Structures.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Decisions and Conditions.
4-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 4 Decisions and Conditions Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 4 Decisions and Conditions.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
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.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Decisions with Select Case and Strings Chapter 4 Part 2.
Controlling Program Flow with 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.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Random Functions Selection Structure Comparison Operators Logical Operator
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Decisions and Conditions
5.04 Apply Decision Making Structures
Brief description on how to navigate within this presentation (ppt)
The Selection Structure
Chapter 4: The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Chapter 4 Control Statements: Part I
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:
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Microsoft Visual Basic 2005: Reloaded Second Edition
Brief description on how to navigate within this presentation (ppt)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 4 Decisions and Conditions
Brief description on how to navigate within this presentation (ppt)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Chapter 4: Decisions and Conditions REV 01 Objectives Use If statements to control the flow of logic. Understand and use nested If statements. Read and create action diagrams that illustrate the logic in a selection process. Evaluate conditions using the comparison operators. Combine conditions using And, Or, AndAlso, and OrElse. Test the Checked property of radio buttons and check boxes. Perform validation on numeric fields. Use a Case structure for multiple decisions. Use one event procedure to respond to the events for multiple controls and determine which control caused the event. Call an event procedure from another procedure. Create message boxes with multiple buttons and choose alternate actions based on the user response. Debug projects using breakpoints, stepping program execution, and displaying intermediate results. DDC 3363 Programming III

Chapter 4: If Statements REV 01 Used to make decisions If 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 If. Then must be on same line as If or ElseIf. End If and Else must appear alone on a line. Note: ElseIf is 1 word, End If is 2 words. A powerful capability of the computer is its ability to make decisions and to take alternate courses of action based on the outcome. DDC 3363 Programming III 2

Chapter 4: If…Then…Else - General Form REV 01 If (condition) Then statement(s) [ElseIf (condition) Then statement(s)] [Else End If A decision made by the computer is formed as a question: Is a given condition true or false? If it is true, do one thing, if it is false, do something else. Example: If the sun is shining Then (condition) go to the beach (action to take if condition is true) Else go to class (action to take if condition is false) End If Logic of an If /Then/ Else statement Logic of an If statement without the Else DDC 3363 Programming III 3

Chapter 4: If…Then…Else — Example REV 01 unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D Then freshmanRadioButton.Checked = True Else freshmanRadioButton.Checked = False End IF When the number of unitsDecimal is less than 32, select the radio button for Freshman; otherwise, make sure the radio button is deselected. DDC 3363 Programming III 4

Chapter 4: Charting If Statements REV 01 A Uniform Modeling Language (UML) activity diagram is a useful tool for showing the logic of an IF statement. Can be used to help programmers organize their thoughts and design projects more quickly UML includes several types of diagrams. Activity diagram-visual planning tool for decisions/actions for either the entire application or single procedure Diamond shape symbol (decision symbol) represents a condition. Branches from the decision symbol indicate which path to take for different results of the decision. DDC 3363 Programming III 5

Chapter 4: Conditions Test in an If statement is based on a condition. REV 01 Test in an If statement is based on a condition. Six relational operators are used for comparison. Negative numbers are less than positive numbers. An equal sign is used to test for equality. Strings can be compared. Enclose strings in quotes. JOAN is less than JOHN HOPE is less than HOPELESS Numbers are always less than letters. 300ZX is less than Porsche Note that strings can be compared only to other strings, and numeric values can be compared only to other numeric values, whether a variable, constant, property, or arithmetic expression. VB actually stores string characters in Unicode, a coding system that uses 2 bytes to store every character. DDC 3363 Programming III 6

Chapter 4: The Helpful Editor REV 01 When entering IF statements, the editor automatically adds the Then and End If. The editor attempts to correct errors by supplying a colon if multiple statements are entered on a line. The colon is a statement terminator. Good programming practices dictate that there should be only one statement per line - so remove the extra colon if found, and correct the syntax. The VS code editor is very helpful when If statements are entered. If you type EndIf without the space, the editor adds the space for you. DDC 3363 Programming III 7

Chapter 4: The 6 Relational Operators REV 01 The test in an IF statement if based on a condition. To form conditions, comparison operators are used. *Refer to Table 4.1 <> not equal to >= greater than or equal to Integer.Parse(QuantityTextBox.Text) >= 500 <= less than or equal to Name1TextBox.Text <= Name2TextBox.Text > < = <> >= <= DDC 3363 Programming III 8

Chapter 4: Comparing Strings REV 01 Comparison begins with the left-most character and proceeds one character at a time, left to right. If a character in one string is not equal to the corresponding character in the 2nd string, the comparison terminates. The string with the lower-ranking character is judged less than the other. Ranking is based on ANSI code, an established order (collating sequence) for all letters, numbers, and special characters. String variables can be compared to other string variables, string properties, or string literals enclosed in quotes. ANSI stands for American National Standards Institute. DDC 3363 Programming III 9

Chapter 4: Comparing Upper and Lowercase Characters REV 01 Use ToUpper and ToLower methods of the String class to return the uppercase or lowercase equivalent of a string, respectively. When comparing strings, the case of the characters is important—an uppercase Y is not equal to a lowercase y. Users may type a name or word in upper or lower case or a combination of both; there are checks for all possibilities. General Form: TextString.ToUpper() TextString.ToLower() If nameTextBox.Text.ToUpper( ) = “BASIC" Then ' Do something. End If When converting name TextBox.Text to uppercase, it must be compared to an uppercase literal (“BASIC”) if it is to evaluate as True. DDC 3363 Programming III 10

Chapter 4: Compound Condition REV 01 If maleRadioButton.Checked And _ Integer.Parse(ageTextBox.Text) < 21 Then minorMaleCountInteger += 1 End If If juniorRadioButton.Checked Or seniorRadioButton.Checked Then upperClassmanInteger += 1 Compound conditions can be used to test more than one condition. Create compound conditions (key term) by joining conditions with logical operators (key term). The logical operators are Or, And, Not, AndAlso, OrElse, and Xor. Each side of the logical operator must be a complete condition. Example: CountInteger > 10 Or < 0 is incorrect. Instead, it must be CountInteger > 10 Or CountInteger < 0 DDC 3363 Programming III 11

Chapter 4: Combining Logical Operators REV 01 Compound conditions can combine multiple logical conditions. When both And and Or are evaluated, And is evaluated before the Or. Use parenthesis to change the order of evaluation—condition inside the parenthesis is evaluated first. If saleDecimal > 1000.0D Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If DDC 3363 Programming III

Chapter 4: Short-Circuit Operations REV 01 Visual Basic has 2 operators that provide short-circuit evaluation for compound conditions: the AndAlso and OrElse. VB evaluates both expressions for True or False, then evaluates the And. The OrElse is designed to short circuit when the first condition evaluates True. AndAlso and OrElse are used for advanced programming when the 2nd expression should not be executed for some reason. In a regular Or operation, if one or the other condition is True, the entire compound condition is True. DDC 3363 Programming III 13

Chapter 4: Nested If Statements REV 01 If tempInteger > 32 Then If tempInteger > 80 Then commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" End If commentLabel.Text = "Freezing" If statements that contain additional If statements are said to be nested If statements. You can nest Ifs in both the Then and Else. In fact, you may continue to nest Ifs within Ifs as long as each If has an End If. DDC 3363 Programming III 14

Chapter 4: Using If Statements with Radio Buttons & Check Boxes REV 01 Instead of coding the CheckedChanged events, use If statements to see which are selected. Place your code in the Click event of Buttons, such as an OK or Apply button; VS checks to see which options are selected. Use If Statements to determine which options are selected. No action will occur when you click on a radio button or check box. Instead, when you click on the OK button, VS checks to see which options are selected. DDC 3363 Programming III 15