Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes.

Slides:



Advertisements
Similar presentations
Pemrograman VisualMinggu …2… Page 1 MINGGU Ke Dua Pemrograman Visual Pokok Bahasan: Console Application Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Advertisements

1.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
C++ for Engineers and Scientists Third Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Fourth Edition, Introductory
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Five More on the Selection Structure.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Chapter 4: The Selection Structure
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Java Programming, Second Edition Chapter Five Input and Selection.
CPS120: Introduction to Computer Science Decision Making in Programs.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Multi Way Selection You can choose statement(s) to run from many sets of choices. There are two cases for this: (a)Multi way selection by nested IF structure.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
JavaScript, Fourth Edition
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
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.
Java Programming Fifth Edition Chapter 5 Making Decisions.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
CPS120: Introduction to Computer Science Decision Making in Programs.
31/01/ Selection If selection construct.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
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.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Java Programming Fifth Edition
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
The Selection Structure
Chapter 4: The Selection Structure
CHAPTER FIVE Decision Structures.
MIS 3200 – Unit 4 Complex Conditional Statements else if switch.
More Selections BIS1523 – Lecture 9.
Making Decisions in a Program
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
Chapter 3: Introduction to Problem Solving and Control Statements
Control Structures: Selection Statement
Chapter 7 Conditional Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
Boolean Expressions to Make Comparisons
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
Control Structures: Selection Statement
Chapter 5: The Selection Structure
Presentation transcript:

Decisions and Debugging Part06dbg --- if/else, switch, validating data, and enhanced MessageBoxes

2 if Any decision must involve some sort of condition. In the simplest of situations, we ask if two entities are equivalent. If the entities are equivalent we do something; if not, we do nothing. We use the equivalence operator (==) to make the comparison.

3 if Enclose the condition in parentheses. Enclose the dependent instructions—those that will execute if the comparison is true—in braces. Braces are optional if there is only a single dependent instruction.  SimpleIf  TwoInstructionIf

4 More Relational Operators OperatorAlgebraicDefinition >>Greater than <<Less than >=≥Greater than or equal to <=≤Less than or equal to ===Equal to !=≠Not equal to

5 if Comparisons can be made with all of the relational operators. If the condition/comparison is true the dependent instructions will be executed.  RelationalIfs

6 Nested if When you place an if within the dependent instructions of the first if, you have a nested if.  NestedIfs

7 if with Logical AND We can test two comparisons within the same condition parentheses by separating them with the && operator. If both of the comparisons are true, the dependent instructions will be executed.  LogicalAnd

8 if with Logical Inclusive OR Use the symbol || to separate two comparisons within the condition parentheses. If one, the other, or both of the comparisons are true, the dependent instructions will be executed.  LogicalOr

9 if with Logical Exclusive OR Use the symbol ^ to separate two comparisons within the condition parentheses. If one, and only one, of the two comparisons is true, the dependent instructions will be executed.  ExclusiveOr

10 else In previous examples we did nothing if the condition(s) were not true. else provides a set of dependent instructions to be executed if the comparisons of an if are not true. Include the dependent instructions within braces as with if.  SimpleIfElse  BetterExclusiveOr

11 if Nested in else Ask another question if the first answer is false. Enclose a second entire if structure within the braces of the else.  IfNestedInElse

12 Changing Case When comparing strings, the case of the characters is considered. To remove this complication, you can convert a string to all upper case or all lower case characters. The String class has ToUpper() and ToLower() methods for this.  CaseConversion

13 switch Any conditional logic can be portrayed with if and if…else structures. If extensive nesting is necessary, the code may become difficult to follow. switch provides a multiple selection structure that results in simplified code for the same task.

14 switch switch replaces if/else chain code like this: if (grade ==‘A’) else if (grade ==‘B’) else if (grade == ‘C’) … etc. With code like this: switch (grade) { case ‘A’: case ‘B’: case ‘C’: … etc. }

15 if or switch? Mutually exclusive alternatives can be coded 2 ways. The switch code may be easier to read than equivalent if/else chain.

16 switch Limitations Exact values to be matched are listed in the cases; ranges of values expressed with relational operators such as >, =, <= are not allowed. switch can only be used with variable values that are integer, char, or string; no floating point values may be compared.  Switch

17 Validating Data Some user entries (in TextBoxes) are used in calculations and/or are displayed in other controls, such as Labels. If a user entry is required, then you must test to make sure that the user did enter some information. Since user entries are stored in the Text property of the TextBox, test for an empty string.

18 User Did Not Supply Required Data If the user did not supply required data, alert user to the problem by displaying a informational MessageBox.  Required Entry

19 Calling one Event Handler Function in another Event Handler Often one event handler function should execute the code of a different event handler. Ex. The btnClear_Click handler function clears all Textbox and Label contents. The btnClearAll_Click handler should 1) reset summary variable values and 2) clear all Textbox and Label contents. Rather than copy/paste the identical code into second event handler, you could call btnClear_Click handler from btnClearAll handler.

20 More MessageBoxes We have used MessageBoxes with a string message, a string caption, a single OK button, and various informational icons. MessageBoxes may have more than 1 button. You may choose from the following button combinations:

21 MessageBox Buttons Choose a button combination by typing the third parameter of the MessageBox as MessageBoxButtons. and Intellisense will list the available buttons. To make one of the buttons the default button (can be activated by pressing Enter), the 5 th parameter of the MessageBox will be MessageBoxDefaultButton and Intellisense will display Button1, Button 2, etc, corresponding to the Buttons you chose.

22 Capturing User Button Choice If you have chosen multiple buttons for your MessageBox, you will want to test to see which Button the user chose and then execute the appropriate code. To do this, you must 1) declare a variable of the DialogResult type and 2) assign the value returned by the MessageBox to this variable DialogResult response; //declare variable to store returned value response = MessageBox.Show(“message”, “caption”, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); //default is Retry

23 Test User Response Then you test the value of the DialogResult variable if (response == DialogResult.Abort) //execute appropriate code else if (response == DialogResult.Retry) //execute appropriate code else //DialogResult.Cancel //execute appropriate code

24 Tracking Calculations with Debug The Debug object of the System.Diagnostics namespace has some useful methods for examining code while it is running. Running the WriteLine() method allows you to write information to the Output Window while the program is running.

25 Tracking Calculations with Debug This represents a very quick way to peek into a running program and view the actual contents of variables without having to show them with controls.  DebugWriteLine

26 Let’s Do a Problem Together Ch4ex4_6pg180