Chapter 4 P 1 Decisions and Conditions Control statements - seqeuncing - selection - repetition (picture later) - abstraction (procedure calls)

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
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.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
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.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
© 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 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Sixth Edition
Chapter 4 The If…Then Statement
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 6 Decisions and Conditions.
Chapter 4: The Selection Process in Visual Basic.
Chapter 4: The Selection Structure
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Programming Logic and Design, Second Edition, Comprehensive
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
1 Flow Control Ifs, loops. 2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Introduction to Problem Solving and Control Statements.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
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.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Controlling Program Flow with Decision Structures.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Chapter 4: Decisions and Conditions
A variable is a name for a value stored in memory.
Chapter 4: Decisions and Conditions
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 4: Making Decisions.
Computer Programming I
The Selection Structure
Chapter 4: Making Decisions.
CHAPTER FIVE Decision Structures.
Making Decisions in a Program
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 3: Introduction to Problem Solving and Control Statements
Objectives After studying this chapter, you should be able to:
VB Decisions, Conditions & If
Decisions and Conditions
Microsoft Visual Basic 2005: Reloaded Second Edition
VB Decisions & Conditions
Boolean Expressions to Make Comparisons
The Selection Structure
Presentation transcript:

Chapter 4 P 1 Decisions and Conditions Control statements - seqeuncing - selection - repetition (picture later) - abstraction (procedure calls)

Chapter 4 P 2 If-statements - a decision is made by the program - a given condition is either True or False Example If the street is blocked then take a detour Endif ExampleIf English T122 is not full then sign up for English T122 Else check History T122 End If

Chapter 4 P 3 The flowchart for these examples street is blocked True take a detour False True branch False branch (This is called a null branch)

Chapter 4 P 4 Eng T122 is not full True sign up for Eng T122 False check on Hist T122 True branch False branch

Chapter 4 P 5 if..then.. - general form If condition Then statement End If The statement can be any Visual Basic statement, including another If statement. Syntax for flowcharts an individual instruction decision box

Chapter 4 P 6 Example If a student’s GPA is 3.5 or more, write that they are on the Dean’s List. True Write “Dean’s List” GPA >= 3.5 False If sGpa >= 3.5 Then lbl.HonorMessage.Caption = “Dean’s List” End If

Chapter 4 P 7 if..then..else If condition Then statement 1 Else statement 2 End If Example A student takes a course on a Pass/Fail basis; a report prints “P” for pass and “F” for fail. Write “Pass” Grade = “P” True False Write “Fail” If sGrade = “P” Then lblMessage.Caption =“Pass” Else lblMessage.Caption = “Fail” End If

Chapter 4 P 8 Note: indentations are required for readability Definition A boolean expression is a statement which has the value True or False. Example sGPA >= 3.5 sGrade = “P” Another way to write the statement If sGpa >= 3.5 Then lbl.HonorMessage.Caption = “Dean’s List” End If is Dim bFlag As Boolean bFlag = (sGpa >= 3.5) If bFlag Then lbl.HonorMessage.Caption = “Dean’s List” End If

Chapter 4 P 9 or If bFlag = True Then lbl.HonorMessage.Caption = “Dean’s List” End If The statement bFlag = (sGpa >= 3.5) places the value, either True or False, of the boolean expression sGpa >= 3.5 into the boolean variable bFlag. The advantage is that a boolean expression can be long and complicated. Using a flag will simplify the code.

Chapter 4 P 10 Terminology A boolean expression or condition is also known as a predicate. Definition The valus of all predicates and variables in a program is called the state of the program. Note The state of the progrm is a description of what the program has done at any given moment. Flowcharting if-statements General If..ThenGeneral If..Then..Else True False True False

Chapter 4 P 11 Note: Advantage of flowcharts - detail Disadvantage of flowcharts- detail Flowcharts are good for descrbing small pieces of code, not entire programs. Conditions Relational operators = < > < =(less than or equal) > =(greater than or equal) (not equal) Example Let iSal_1 = 10000, iSal_2 = 20000, iSal_3 = (a) iSal_1 = iSal_2 is False (b)iSal_2 < iSal_3 is True

Chapter 4 P 12 Using if statements with option buttons and check boxes - use if-statements for option buttons and check buttons - put the code into the appropriate command button Example P. 108 Example P. 120 Private subCalculate_Click()) Nested if-statements Example P. 108, 109 If iTemp > 32 Then If iTemp > 80 Then lblComment.Caption = “Hot” Else lblComment.Caption = “Moderate” End If Else lblComment.Caption = “Freezing” End If

Chapter 4 P 13 Use Elseif for several If-statements Example P. 109 If iTemp <= 32 Then lblComment.Caption = “Freezing” Elseif iTemp > 80 Then lblComment.Caption = “Hot” Else lblComment.Caption = “Moderate” End If Note Avoid too much nesting. This is cahracterized by “wide” flowcharts.

Chapter 4 P 14 Example Find the largest of three numbers, A, B, C. First flowchart A < B True False B < C A < C True False True False Write CWrite B Write C Write A If iA < iB Then If iB < iC Then write iC Else write iB End If Else If iA < iC Then write iC Else write iA End If

Chapter 4 P 15 Second flowchart The idea is to declare an extra variable iMax, which can simplify the flowchart considerably Max = A Max < B True False Max = B Max<C True Max = C iMax = A If iMax< B Then iMax = B End If If iMax < C Then iMax = C End If write iMax In general, avoid wide flowcharts.

Chapter 4 P 16 Comparing strings ASCII code P all symbols, including numbers, are represented as symbols - strings are compared left to right (alphabetical order) Example “Bill” < “Hillary” “2” < “A” “A” < “a” Comparing the text property of text boxes P values entered in text boxes are compared as strings - their data type is variant Example Suppose cSalFirst is and cSalSecond is Then cSalFirst > cSalSecond is True val(cSalFirst) > val(cSalSecond) is False

Chapter 4 P 17 Comparing uppercase and lowercase characters Example Let sName_1 be “Smith” and sName_2 be “jones”. Then (sName_1 > sName_2) is False because the ASCII valus of “S” is less than the ASCII value of “j”. Use ucase or lcase to change the words all to compare the uppercase or lowercase value of the words. Example ucase(sName_1) > ucase(sName_2) is True. Compound conditions The logical operators, in hierarchical order, are not, and, and or. Example (4 >= 5) and (8 = (3 + 5)) (4 >= 5) or (8 = (3 + 5)) not (- 4 > 0)

Chapter 4 P 18 Hierarchical order of all operators ( ) NOT */AND +-OR relational operators Example Suppose iNum holds the value 1. 0 < iNum and iNum < 2 gives a syntax error! The operator of highest precedence is AND. The compiler evaluates iNum AND iNum. But iNum is of type integer, so this expression is not well-defined. It should be written (0 < iNum) and (iNum < 2) which is true. See handout

Chapter 4 P 19 Control arrays (Fig ) - list of controls with the same Name - elements of the list are distinguished by some number - syntax - give the first control a name - give the second control the same name - a message box will ask if you want a control array - select Yes - the Caption may be different - in code, refer to the elements of the control array by name and number Example P. 113

Chapter 4 P 20 Example Message formatter The option buttons have Caption Red, Green, Blue, and Black. Suppose their names are all optColor. The code would be If optColor(0).Value = Checked Then lblMessage.Forecolor = vbRed Elseif optColor(1).Value = Checked Then lblMessage.Forecolor = vbGreen ElseifoptColor(2).Value = Checked Then lblMessage.Forecolor = vbBlue ElselblMessage.Forecolor = vbBlack End If Finding the highest or lowest value previous

Chapter 4 P 21 Input validation (for numeric data) Checking for correct data type Example P. 115 If isNumeric (txtQuantity.Text) Then lblDue.Caption = cPrice * val (txtQuantity) End If Checking for the range of values - the programmer checks that the date entered is reasonable Example Check that the number of hours per week do not exceed 168. If iHours > 168 Then some action ElsecPay = iHours * cWage End If

Chapter 4 P 22 Message boxes Definition A message box is a box within which the program displays a message to the user. (Fig. 4.10) The general form is MsgBox “string” [,buttons/icon][,“caption on title bar”] (Everything between the square brackets is optional.) The possible buttons/icons are vbOkOnlyvbCriticalvbQuestion vbExclamationvbInformation Example If iHours > 168 Then MsgBox “Please enter a numeric value”, vbOkOnly, “Error” ElsecPay = iHours * cWage End If

Chapter 4 P 23 Note: The exceptional case (the case that generates the error message) should come first, if possible. Example If not IsNumeric (txtQuantity.Text) Then MsgBox “Please enter a numeric value”, vbOkOnly, “Error” Else lblDue.Caption = cPrice * Val(txtQuantity.Text) End If This is better than writing what the book writes, which is If IsNumeric (txtQuantity.Text) Then lblDue.Caption = cPrice * Val(txtQuantity.Text) Else MsgBox “Please enter a numeric value”, vbOkOnly, “Error” End If

Chapter 4 P 24 The message string To include long, formatted messages, do something like formatted string = Format(formatted string, etc.) string = formatted string & “xxxxxxxxxx” MsgBox formatted string, vbOkOnly, name of box Example Dim stFormattedString as String Dim stMessageString as String stFormattedString = Format$(iHighValue, “Standard”) stMessageString = “Your value is higher than” & stFormattedString MsgBox = stMessageString, vbCritical, “Value out of bounds”

Chapter 4 P 25 To have a message extending over several lines, use - line feed - insert a new line - the code is Chr (10) - carriage return - place the cursor at the beginning of this new line - the code is Chr (13) Note: Chr is a method which takes an integer and returns the corresponding ASCII character (if any). Example Dim stNewline as String stNewLine = Chr(10) & Chr (13) Place stNewLine into a string when you want a new line in the message box.

Chapter 4 P 26 Note: Chr is a method which takes an integer and returns the corresponding ASCII character, if any Example lblMessage.Caption = Chr (90) displays Z Note that Chr (500) is undefined, and causes an error. Debugging tools The Debug toolbar - select View - select Toolbar - select Debug Definition A break is a pause in the execution of the program. Forcing a break Method 1 - write Stop in the code

Chapter 4 P 27 Method 2 - set a break using the debugger - set the cursor where you want the break to occur - select Toggle Breakpoint - remove the breakSAME STEPS - remove all breaks - select Debug - select Remove all breaks The Immediate Window - select View and then select Immediate WIndow - allows the user to execute a single intruction - done during Break time - enter code (either type or copy it) - hit Return

Chapter 4 P 28 Watch - examine a variable or expression during program execution -two methods for watches Method 1 The Watch Pane (Fig. 4.15) - this is set during design time - allows the user to examine values and predicates - select View and Watch Window - type in a variable or expression and select options -look at the window in Break time Method 2 The Instant Watch - under Code select View - highlight the variable or expression you wish to examine - under Debug select Instant Watch - Break the code (as before)

Chapter 4 P 29 Stepping through code - execute one or more lines of code individually - Step into - Break the code - under Debug select Step Into