T12 - Switch, Enumerations, Exception Handling, Recursion...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Switch, Enumerations, Exception Handling, Recursion Engineering 1D04, Teaching Session 12 Switch Statement © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 1 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 2 Switch Statement Consider the if-else construct. A typical situation is to capture a keystroke and use it to determine some aspect of the application’s behaviour. For example: ‘B’ or ‘b’ means set bold font ‘C’ or ‘c’ means copy selected text ‘I’ or ‘i’ means set italics ‘M’ or ‘m’ means move the selected text ‘Q’ or ‘q’ means quit the application how do we implement this? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 3 Switch Statement Motivation if (ch == 'B' || ch == 'b') { //code to bold text } else if (ch == 'C' || ch == 'c') { //code to copy text } else if (ch == 'I' || ch == 'i') { //code to italicize text } else if (ch == 'M' || ch == 'm') { //code to move text } else if (ch == 'Q' || ch == 'q') { //code to quit }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 4 Switch Statement Motivation if (ch == 'B' || ch == 'b') { //code to bold text } else if (ch == 'C' || ch == 'c') { //code to copy text } else if (ch == 'I' || ch == 'i') { //code to italicize text } else if (ch == 'M' || ch == 'm') { //code to move text } else if (ch == 'Q' || ch == 'q') { //code to quit } And this can get cumbersome! Also, what if you have many cases to consider - does the order of the else-if clauses matter? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 5 The Switch Construct switch (ch) { case 'B': case 'b': //code to bold text break; case 'C': case 'c': //code to copy text break; case 'I': case 'i': //code to italicize text break; . . . break; case 'Q': case 'q': //code to quit break; } how do you think this works? © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 6 The Switch Construct switch (ch) { case 'B': case 'b': //code to bold text break; case 'C': case 'c': //code to copy text break; case 'I': case 'i': //code to italicize text break; . . . break; case 'Q': case 'q': //code to quit break; } variable that controls switch (variable must be “simple” - integer, character etc) break - transfers control outside of the switch (works in loops also) © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 7 The Switch Construct switch (ch) { case 'B': case 'b': //code to bold text break; . . . break; case 'Q': case 'q': //code to quit break; default: //code to run if no case is entered break; } we can also use a default case to cope with the situation if no case has been specified with the current value of the switch variable for example, what if ch = ‘s’?
Background image of page 2
3 © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng 8 The Switch Construct Surprisingly, as well as integer, characters and other sub-range variables, in C# strings can also be used as the switch variable. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/07/2010 for the course SDF sdf taught by Professor Sdf during the Spring '10 term at Manchester IN.

Page1 / 11

T12 - Switch, Enumerations, Exception Handling, Recursion...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online