IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
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.
Chapter 4: Control Structures: Selection
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
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.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
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.
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.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
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
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.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Controlling Program Flow with Decision Structures.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
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.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
5.04 Apply Decision Making Structures
Chapter 4: Making Decisions.
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
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:
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 6 Control Statements: Part 2
Problem Solving and Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Boolean Expressions to Make Comparisons
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

IS 1181 IS 118 Introduction to Development Tools VB Chapter 03

Copyright (c) 2003 by Prentice Hall Chapter 5: Decision Visual Basic.NET

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

IS 1184 Logical Expressions If statement has simple syntax:  If Condition Then Statement Condition: an expression that can be evaluated as true or false Statement: a VB statement

IS 1185 Relational Operators Used to compare operands and decide if the relationship is true  Examples include =, >, and <= 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 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

IS 1186 Logical Operators 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  AndAlso operator returns True when all expressions are true; stops evaluating when a False expression is found  Or operator returns True if one or more expressions are True  OrElse operator returns True if one or more expressions are true; stops evaluating when a True expression is found  XOr operator returns True if exactly one expression is True

IS 1187 Logical Operator Considerations AndAlso operator is more efficient than And  AndAlso stops evaluating when a false expression is found, but And evaluates all  Put conditions more likely to be false first OrElse operator is more efficient than Or  OrElse stops evaluating when a true expression is found, but Or evaluates all  Put conditions more likely to be true first

IS 1188 The If Block 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 Terminated with End If statement

IS 1189 The If…Then…Else…End If Block Adds an Else clause  Contains statements to be executed if the condition is False Terminated with End If keyword 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

IS The If…Then…ElseIf…Then…Else… End If Block Used when a decision depends on several conditions ElseIf keyword used to define each condition Often has a “catch-all” Else clause 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

IS If Statement Considerations Varying conditions  The ElseIf block is useful when conditions vary  Conditions can be based on entirely different factors 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 Use comments liberally  Especially for nested If blocks

IS Alternatives to the If Block Immediate If (IIf) function  Similar to the If function in Excel  Syntax: IIf(condition, expression1, expression2) Returns expression1 if true, expression2 if false  Function is inefficient All three arguments passed to function are evaluated Computation  Set variables or properties by computation i.e. cboSortField.Enabled = chkSort.Checked rather than setting the Enabled property in an If block

IS Select Case Block 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 If statement Criteria are mutually exclusive  Once a criterion is found to be true, that block is executed  Place most restrictive criterion at top

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

IS Nesting Select Case Blocks Similar to nesting If blocks  Nested blocks must terminate before returning to outer block  Make extensive use of comments

IS Nested Select Statement Each nested block is indented Nested block terminates before returning to outer block

IS If Statement Nested Inside Select Case Statement If block is terminated before returning to Select Case block

IS Application Example: Tuition Calculator Analyze and determine system requirements  Calculate student tuition Based on residence status and hours taken 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

IS 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

IS Code The Solution Use If…ElseIf structure inside Select Case Use Nested Select Case Statement

IS An Alternative Solution Use combo boxes to display residence status and range of hours taken  Offers more flexibility, but code is not as clear

IS Visual Interface Items added to combo box at runtime; SelectedIndex property used to set default value

Copyright (c) 2003 by Prentice Hall23 Code the Solution SelectedIndex property used to determine which option is selected

IS Block Level Declarations 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 procedure-level variable with same name

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

IS Summary String comparisons can be “Binary” (case sensitive) or “Text” (case insensitive) If operational precedence is confusing, use parentheses to encloses expression(s) Four ways to construct If structure  Simple If statement  Simple If block  If…Else block  If…ElseIf…Else block If blocks can be nested

IS Summary Use comments to explain the purpose of the condition Computer evaluates logical or relational expressions differently than we might interpret them The AndAlso and OrElse operators are more efficient then the And and Or operators In some cases, computations can replace the use of If blocks  Can be more efficient, but can be harder to read

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