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

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

Embed Size (px)

Citation preview

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

Decisions and Debugging

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

Page 2: 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.

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

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

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

4

More Relational Operators

Operator Algebraic Definition

> > Greater than

< < Less than

>= ≥ Greater than or equal to

<= ≤ Less than or equal to

== = Equal to

!= ≠ Not equal to

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

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

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

6

Nested if

• When you place an if within the dependent instructions of the first if, you have a nested if.

NestedIfs

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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.

}

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

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.

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

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

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

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.

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

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

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

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.

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

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:

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

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 5th parameter of the MessageBox will be MessageBoxDefaultButton and Intellisense will display Button1, Button 2, etc, corresponding to the Buttons you chose.

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

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

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

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

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

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.

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

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

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

26

Let’s Do a Problem Together

• Ch4ex4_6pg180