© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

Chapter 13 Control Structures in C. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Control Structures if Statement if-else Statement switch.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Selection Flow Charts If statements. Flow of Control The flow of control is a concept we’ve already encountered. The concept of control relates to the.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Decision making in VBA. Current Event Occurs: When a form is opened When the focus leaves one record and moves to another Before the first or next record.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Chapter 8 - Visual Basic Schneider1 Chapter 8 Sequential Files.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
Control Flow C and Data Structures Baojian Hua
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 6 Functions & If/Else.
UNIT II Decision Making And Branching Decision Making And Looping
© 1999, by Que Education and Training, Chapter 7, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Exceptions COMPSCI 105 S Principles of Computer Science.
The University of Texas – Pan American
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Visual Basic: An Object Oriented Approach 5: Structured Programming.
Chapter 4 The If…Then Statement
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Using Visual Basic for Applications (VBA) – Project 8.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 5 - VB 2005 by Schneider1 Chapter 5 – Decisions 5.1 Relational and Logical Operators 5.2 If Blocks.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Selection Flow Charts If statements. Flow of Control The flow of control is a concept with which we’re already familiar. The concept of control relates.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Controlling Program Flow with Decision Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
JavaScript and Ajax (Control Structures) Week 4 Web site:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Programming in visual basic .net Visual Basic Building Blocks
Scripts & Functions Scripts and functions are contained in .m-files
EGR 141 Computer Problem Solving in Engineering and Computer Science
Microsoft Visual Basic 2005 BASICS
Chapter 3: Selection Structures: Making Decisions
Looping and Repetition
Presentation transcript:

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Decisions Nested on True Branch If condition1 Then Statements for steps ABC If condition2 Then statement(s) for condition1 and condition2 true Else statement(s) for condition1 true and condition2 false End If Statements for steps XYZ Else statement(s) for condition1 false End If Condition1 ? FalseTrue Steps to process if condition 1 is false True Condition2 ? False Steps to process if condition1 is true and condition2 is false Steps to process if both condition1 and condition2 are true Steps ABC Steps XYZ

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Decisions Nested on False Branch If condition1 Then statement(s) for condition1 true Else Statements for steps ABC If condition2 Then statement(s) for condition1 false and condition2 true Else statement(s) for condition1 and condition2 false End If Statements for steps XYZ End If Condition1 ? TrueFalse Steps to process if condition1 is true Condition2 ? TrueFalse Steps to process if condition1 is false and condition2 is true Steps to process if both condition1 and condition2 are false Steps ABC Steps XYZ

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Nested Decisions on False (ElseIf) If condition1 Then statement(s) for condition1 true ElseIf condition2 Then statement(s) for condition1 false and condition2 true Else statement(s) for both condition1 and condition2 false End If Steps to process if condition1 is false and condition2 is true Condition1 ? TrueFalse Steps to process if condition1 is true Condition2 ? TrueFalse Steps to process if both condition1 and condition2 are false False branch contains a complete decision structure and no intervening steps before and after the decision

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Select Case: Special Case of Nested Decisions on False (ElseIf) Select Case expression Case cond1 statement(s) for cond1 true Case cond2 statement(s) for cond1 false and cond2 true : Case condn statement(s) for cond1-condn-1 false and condn true Case Else statement(s) for al conditions false End Select When every decision in nested if compares values against the same expression cond1 True False Steps to process if cond1 True Case expression cond2 True False Steps to process if cond2 True condn True Steps to process if condn True Steps to process if condn False

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Limitations of Data Validation n It can become quite difficult to handle every type of problem during data validation that could arise. n VB provides the option of including error handlers to augment or as an alternative to data validation processing. i Supplying too large of a value to a conversion function will trigger an overflow error. i During division operations, a zero divisor will trigger a division by zero run time error. i Passing the wrong type of data to a function or procedure will trigger a type mismatch error.

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach VB’s Built-in Error Handling n What happens when errors occur in VB? i Number, source and description of VB’s Err object are updated i VB displays error message with above information i Program breaks or terminates

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Writing Error Handlers n Instruct VB to bypass its error handling i On Error Goto PgmLabelName n Bypass error handler if no errors triggered i Add Exit Sub after last line of procedure body and right before End Sub n Add Error Handler code between Exit Sub & End Sub i Add Error Handler label u PgmLabelName: i Add Error Handler code

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Error Handler Example 1 n Similar to VB’s built-in error handling n Important difference is that the program does not terminate execution Private Sub cmdCalc_Click() On Error GoTo HandleErrors ‘ override VB’s intrinsic error handling If DataVal Then ‘ do normal processing End If Exit Sub ‘ all done if it was the normal case HandleErrors: ‘ starting point of error handler Call MsgBox(Err.Number & “: “ & Err.Description, _ vbOkOnly+vbInformation) End Sub

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Error Handler Example 2 Private Sub cmdCalc_Click() On Error GoTo HandleErrors If DataVal Then ‘ do normal processing End If Exit Sub HandleErrors: If Err.Number = 6 Then Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation) ElseIf Err.Number = 11 Then Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation) ElseIf Err.Number = 13 Then Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation) Else Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation) End If End Sub

© 1999, by Que Education and Training, Chapter 6, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Error Handler Example 3 Private Sub cmdCalc_Click() On Error GoTo HandleErrors If DataVal Then ‘ do normal processing End If Exit Sub HandleErrors: Select Case Err.Number Case 6 Then Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation) Case 11 Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation) Case 13 Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation) Case Else Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation) End Select End Sub