Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMBC CMSC 104 – Section 01, Fall 2016

Similar presentations


Presentation on theme: "UMBC CMSC 104 – Section 01, Fall 2016"— Presentation transcript:

1 UMBC CMSC 104 – Section 01, Fall 2016
Switch

2 Notes & Announcements Project 6 due before next class!
Next 5:30pm Extra Credit question posted for Project 6 Will review in a moment Grades for Project 4 and Project 5 coming before next Tuesday Next Tuesday is a hands-on lab

3 Project 6 Q2 Refresher

4 Project 6 Extra Credit

5 Multiple Selection So far, we have only seen binary selection.
if ( age >= 18 ) { printf(“Vote!\n”) ; } if ( age >= 18 ) { printf(“Vote!\n”) ; } else printf(“Maybe next time!\n”) ;

6 Multiple Selection (con’t)
Sometimes it is necessary to branch in more than two directions. We do this via multiple selection. The multiple selection mechanism in C is the switch statement. Lets look at an example where we’re trying to convert a numeric value for day of the week to the corresponding printed value.

7 Multiple Selection with if
(continued) if (day == 4) { printf (“Thursday”) ; } if (day == 5) { printf (“Friday”) ; if (day == 6) { printf (“Saturday”) ; if ((day < 0) || (day > 6)) { printf(“Error - invalid day.\n”) ; if (day == 0 ) { printf (“Sunday”) ; } if (day == 1 ) { printf (“Monday”) ; if (day == 2) { printf (“Tuesday”) ; if (day == 3) { printf (“Wednesday”) ;

8 Multiple Selection with if-else
if (day == 0 ) { printf (“Sunday”) ; } else if (day == 1 ) { printf (“Monday”) ; } else if (day == 2) { printf (“Tuesday”) ; } else if (day == 3) { printf (“Wednesday”) ; } else if (day == 4) { printf (“Thursday”) ; } else if (day == 5) { printf (“Friday”) ; } else if (day == 6) { printf (“Saturday”) ; } else { printf (“Error - invalid day.\n”) ; } This if-else structure is more efficient than the corresponding if structure. Why?

9 The switch Multiple-Selection Structure
switch ( integer expression ) { case constant1 : statement(s) break ; case constant2 : . . . default: }

10 switch Statement Details
The last statement of each case in the switch should almost always be a break. The break causes program control to jump to the closing brace of the switch structure. Without the break, the code flows into the next case. This is usually not what you want. A switch statement will compile without a default case, but always consider using one.

11 Good Programming Practices
Include a default case to catch invalid data. Inform the user of the type of error that has occurred (e.g., “Error - invalid day.”). If appropriate, display the invalid value. If appropriate, terminate program execution (discussed in CMSC 202).

12 switch Example switch ( day ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; case 2: printf (“Tuesday\n”) ; case 3: printf (“Wednesday\n”) ; case 4: printf (“Thursday\n”) ; case 5: printf (“Friday\n”) ; case 6: printf (“Saturday\n”) ; default: printf (“Error -- invalid day.\n”) ; } Is this structure more efficient than the equivalent nested if-else structure?

13 Make Multiple Selection Great Again!
A nested if-else structure is less efficient than a switch statement. A switch statement may be easier to read. If-else statements take up a lot of whitespace. Also, it is easier to add new cases to a switch statement than to a nested if-else structure.

14 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); break; case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); default: printf("You entered another number!\n"); } printf("Goodbye!\n"); return 0;

15 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); break; case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); default: printf("You entered another number!\n"); } printf("Goodbye!\n"); return 0; #include <stdio.h> int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); if (num == 1) { printf("You entered one!\n"); } else if (num == 2) { printf("You entered two!\n"); } else if (num == 3) { printf("You entered three!\n"); } else { printf("You entered another number!\n"); } printf("Goodbye!\n"); return 0;

16 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); break; case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0;

17 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); break; case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0; #include <stdio.h> int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); if (num == 1) { printf("You entered one!\n"); } else if (num == 2) { printf("You entered two!\n"); } else if (num == 3) { printf("You entered three!\n"); } printf("Goodbye!\n"); return 0;

18 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0;

19 Another Example #include <stdio.h> #include <stdio.h>
int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); case 2: printf("You entered two!\n"); case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0; #include <stdio.h> int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); if (case == 1) { printf("You entered one!\n"); } if (case == 1 || case == 2) { printf("You entered two!\n"); if (case == 1 || case == 2 || case == 3) { printf("You entered three!\n"); printf("Goodbye!\n"); return 0;

20 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); case 2: printf("You entered two!\n"); break; case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0;

21 Another Example #include <stdio.h> #include <stdio.h>
int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: printf("You entered one!\n"); case 2: printf("You entered two!\n"); break; case 3: printf("You entered three!\n"); } printf("Goodbye!\n"); return 0; #include <stdio.h> int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); if (case == 1) { printf("You entered one!\n"); } if (case == 1 || case == 2) { printf("You entered two!\n"); if (case == 3) { printf("You entered three!\n"); printf("Goodbye!\n"); return 0;

22 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: case 2: case 3: printf("You entered a valid number!\n"); break; default: printf("You entered an invalid number!\n"); } printf("Goodbye!\n"); return 0;

23 Another Example #include <stdio.h> int main() { int num;
printf("Enter a number between 1 and 3: "); scanf("%d", &num); switch(num) case 1: case 2: case 3: printf("You entered a valid number!\n"); break; default: printf("You entered an invalid number!\n"); } printf("Goodbye!\n"); return 0; #include <stdio.h> int main() { int num; printf("Enter a number between 1 and 3: "); scanf("%d", &num); if (case == 1 || case == 2 || case == 3) { printf("You entered a valid number!\n"); } else { printf("You entered an invalid number!\n"); } printf("Goodbye!\n"); return 0;

24 Another Example #include <stdio.h> #define ELECTORAL_VOTES_NEEDED 270 #define NOT_ENOUGH_VOTES -1 #define TIE -1 #define HILLARY_CLINTON 0 #define DONALD_TRUMP 1 int main() { int clintonVotes, trumpVotes, winner; printf("Enter total electoral college votes for Clinton: "); scanf("%d", &clintonVotes); printf("Enter total electoral college votes for Trump: "); scanf("%d", &trumpVotes); if(clintonVotes < ELECTORAL_VOTES_NEEDED && trumpVotes < ELECTORAL_VOTES_NEEDED) { winner = NOT_ENOUGH_VOTES; } else if (clintonVotes > trumpVotes) { winner = HILLARY_CLINTON; } else if (trumpVotes > clintonVotes) { winner = DONALD_TRUMP; } else { winner = TIE; } (continued) switch (winner) { case HILLARY_CLINTON: printf("Stronger Together\n"); break; case DONALD_TRUMP: printf("Make America Great Again\n"); default: printf("UH OH!\n"); } return 0;

25 The char Data Type The char data type holds a single character.
char ch; Example assignments: char grade, symbol; grade = ‘B’; symbol = ‘$’; The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our need.

26 The char Data Type (con’t)
Use scanf (“%c”, &ch) ; to read a single character into the variable ch. (Note that the variable does not have to be called “ch”.”) printf(“%c”, ch) ; to display the value of a character variable.

27 char Example If the user entered an A, the output would be:
#include <stdio.h> int main ( ) { char ch ; printf ("Enter a character: ") ; scanf ("%c", &ch) ; printf ("The value of %c is %d.\n", ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

28 The getchar ( ) Function
The getchar( ) function is found in the stdio library. The getchar( ) function reads one character from stdin (the standard input buffer) and returns that character’s ASCII value. The value can be stored in either a character variable or an integer variable.

29 getchar ( ) Example If the user entered an A, the output would be:
#include <stdio.h> int main ( ) { char ch ; /* int ch would also work! */ printf (“Enter a character: “) ; ch = getchar( ) ; printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

30 Problems with Reading Characters
When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character. What will the user actually type? The character he/she wants to enter, followed by pressing ENTER. So, the user is actually entering two characters, his/her response and the newline character. Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.

31 Improved getchar( ) Example
#include <stdio.h> int main ( ) { char ch, newline ; printf (“Enter a character: “) ; ch = getchar( ) ; newline = getchar( ) ; /* could also use scanf(“%c”, &newline) ; */ printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

32 Additional Concerns with Garbage in stdin
When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer. That is because scanf( ) was looking for the next integer and ignored the newline (whitespace). If we use scanf (“%d”, &num); to get an integer, the newline is still stuck in the input stream. If the next item we want to get is a character, whether we use scanf( ) or getchar( ), we will get the newline. We have to take this into account and remove it.

33 You can switch on a char! #include <stdio.h> int main() {
char letter; printf("Enter the letter A: "); scanf("%c", &letter); switch (letter) case 'A': printf("You entered uppercase A!\n"); break; case 'a': printf("You entered lowercase a!\n"); default: printf("You suck at following directions!\n"); } return 0;

34 EOF Predefined Constant
getchar( ) is usually used to get characters from a file until the end of the file is reached. The value used to indicate the end of file varies from system to system. It is system dependent. But, regardless of the system you are using, there is a #define in the stdio library for a symbolic integer constant called EOF. EOF holds the value of the end-of-file marker for the system that you are using.

35 getchar( ) Example Using EOF
#include <stdio.h> int main () { int grade, aCount, bCount, cCount, dCount, fCount ; aCount = bCount = cCount = dCount = fCount = 0 ; while ( (grade = getchar( ) ) != EOF ) switch ( grade ) case 'A': aCount++; break ; case 'B': bCount++; break ; case 'C': cCount++; break ; case 'D': dCount++; break ; case 'F': fCount++; break ; default : break ; } printf("A=%d, B=%d, C=%d, D=%d, F=%d\n", aCount, bCount, cCount, dCount, fCount); return 0 ;

36 Switching on a float You can’t! switch can only be used with integers!
Remember, a char is really an int under the hood.

37 Switching in Project 6 Remember, I want you to use a switch statement in Project 6 for using the various operators! “I will build a great switch statement -- and nobody builds switch statements better than me, believe me -- and I'll build it with very little whitespace. I will build a great, great switch statement in my C program, and I will make the CPU evaluate that switch statement. Mark my words.” -- You (while working on Project 6)

38 Questions?


Download ppt "UMBC CMSC 104 – Section 01, Fall 2016"

Similar presentations


Ads by Google