EECE.2160 ECE Application Programming Instructors: Dr. Michael Geiger & Dr. Lin Li Spring 2019 Lecture 9: Range checking with if statements; switch statements
Announcements/reminders Text exercises due 3 days after each lecture Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Looking ahead: Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page (http://mjgeiger.github.io/eece2160/oldexams.htm) 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Today’s lecture Review Else, else if Range checking with if statements Switch statements 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Review: else else blocks execute if prior condition(s) false if (a == 0) x = x + 1; else // Executes if x = x – 1; // a != 0 Code above executes exactly one block if condition true skip else block if condition false skip if block 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Review: else if Check conditions in order using else if if (a == 0) x = x + 1; else if (b == 1) // Only executes x = x - 1; // if a != 0 else if (c == 2) // Only executes x = x * 2; // if a != 0 // and b != 1 else // Only executes x = x / 2; // if all 3 // conditions false 4/18/2019 ECE Application Programming: Lecture 9
if (range checking - take 1) int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) { printf(“That’s not in range!"); } else if (n < 1) { printf(“That’s not in range!"); } else { printf("Good job!"); } 4/18/2019 ECE Application Programming: Lecture 9
if (range checking - take 2) If there is only one statement needed for the true and/or false condition, the {} are not needed int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) printf(“That’s not in range!"); else if (n < 1) printf(“That’s not in range!"); else printf("Good job!"); 4/18/2019 ECE Application Programming: Lecture 9
if (range checking - take 3) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10 || n < 1) printf(“That’s not in range!"); else printf("Good job!"); 4/18/2019 ECE Application Programming: Lecture 9
if (range checking - take 4) Use the && or || as needed to check for multiple conditions int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (1 <= n && n <= 10) printf(“Good job!"); else printf(“That’s not in range!"); 4/18/2019 ECE Application Programming: Lecture 9
if (range checking) (The WRONG WAY) int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (1 <= n <= 10 ) // THIS MAY NOT COMPILE printf("Good job!"); else printf(“That’s not in range!"); Even if code above compiles, it won’t work! Tests 1 <= n, compares result (true/false) to 10 4/18/2019 ECE Application Programming: Lecture 9
Example: if statements Write a short code sequence to do each of the following: Given int x, check its value If x is greater than 5 and less than or equal to 10, print x Prompt for and read temperature as input (type double) If temp is 90 or higher, print “It’s too hot!” If temp is 32 or lower, print “It’s freezing!” In all other cases, print “It’s okay” Read 3 int values and print error if input problem If fewer than 3 values read, print error message with number of values Example: Error: only 2 inputs read correctly 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Example solution Given int x, check its value If x is greater than 5 and less than or equal to 10, print x if (x > 5 && x <= 10) printf(“%d\n”, x); 4/18/2019 ECE Application Programming: Lecture 9
Example solution (cont.) Prompt for and read temperature as input (type double) If temp is 90 or higher, print “It’s too hot!” If temp is 32 or lower, print “It’s freezing!” In all other cases, print “It’s okay” int main() { double temp; printf(“Enter temperature: “); scanf(“%lf”, &temp); if (temp >= 90) printf(“It’s too hot!\n”); else if (temp <= 32) printf(“It’s too cold!\n”); else printf(“It’s okay\n”); return 0; } 4/18/2019 ECE Application Programming: Lecture 9
Example solution (cont.) Read 3 int values and print error if input problem If fewer than 3 values read, print error message with number of values Example: Error: only 2 inputs read correctly int main() { int x, y, z; // Input values int num; // # values read num = scanf(“%d %d %d”, &x, &y, &z); if (num < 3) printf(“Error: only %d inputs read correctly”, num); return 0; } 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 switch statements Nesting several if/else if statements can get tedious If each condition is simply checking equality of same variable or expression, can use switch 4/18/2019 ECE Application Programming: Lecture 9
switch/case statement - General form switch ( <expression> ) { case <value1> : <statements> [ break; ] case <value2> : <statements> [ break; ] . . [ default: <statements> [ break; ] ] } 4/18/2019 ECE Application Programming: Lecture 9
switch/case statement Check if <expression> matches any value in case statements If <expression> == <value1>, execute <statements> in that case If <expression> == <value2>, execute <statements> in that case If <expression> does not equal any of the values, go to default case (if present) 4/18/2019 ECE Application Programming: Lecture 9
Switch statements and break Each case is just a starting point—switch does not automatically skip other cases! Example: switch (x) { case 0: x = 3; case 1: x = x * 4; default: x = x – 1; } If x == 0: Start at case 0 x = 3; Then, go to case 1 x = x * 4 = 3 * 4 = 12 Then, go to default: x = x – 1 = 12 – 1 = 11 4/18/2019 ECE Application Programming: Lecture 9
Switch statements and break Use break to exit at end of case You may not always want to use break—will see examples later Rewriting previous example: switch (x) { case 0: x = 3; break; case 1: x = x * 4; default: x = x – 1; } 4/18/2019 ECE Application Programming: Lecture 9
switch/case statement - example #include <stdio.h> int main() { char grd; printf("Enter Letter Grade: "); scanf("%c",&grd); printf(“You are "); // continued next slide 4/18/2019 ECE Application Programming: Lecture 9
switch/case statement - example switch (grd) { case 'A' : printf("excellent"); break; case 'B' : printf("good"); break; case 'C' : printf("average"); break; case 'D' : printf("poor"); break; case 'F' : printf("failing"); break; default : printf(“incapable of reading directions"); break; } return 0; } 4/18/2019 ECE Application Programming: Lecture 9
Example: switch statement What does the program on the previous slides print if the user enters: A B+ c X Recognize, of course, that it always prints: Enter Letter Grade: 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Example solution What does the program on the previous slides print if the user enters: A You are excellent B+ Only first character is read—’B’ You are good c This program is case-sensitive—’C’ and ‘c’ are two different characters! Will go to default case You are incapable of reading directions X No case for ‘X’—goes to default case 4/18/2019 ECE Application Programming: Lecture 9
switch/case statement - Alt example switch (grd) { case 'A' : case 'a': case 'B' : case 'b': printf("doing very well"); break; case 'C' : case 'c': case 'D' : case 'd': printf("not doing too well"); break; case 'F' : case ‘f': printf("failing"); break; default : printf("incapable of reading directions"); break; } return 0; } 4/18/2019 ECE Application Programming: Lecture 9
ECE Application Programming: Lecture 9 Final notes Next time While/do-while loops Reminders: Text exercises due 3 days after each lecture Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Looking ahead: Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page (http://mjgeiger.github.io/eece2160/oldexams.htm) 4/18/2019 ECE Application Programming: Lecture 9