16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 11: More if examples Conditional statements—switch
ECE Application Programming: Lecture 11 Lecture outline Announcements/reminders Today’s office hours: 2:00-3:00 only (ECE dept. meeting) Assignment 4 posted; due Friday, 10/7 Exam 1: Wednesday, 10/5 Will be allowed one double-sided sheet of notes Old exams on course website Assignment 3 grading to be completed soon Yes, I know … here we go again with this “completed soon” stuff Once graded, should be able to submit regrades via Blackboard Will need to e-mail me when you resubmit Another possible Mac (or Windows/Linux) IDE: NetBeans Today Range checking with if More if examples Conditional statements: switch 2/16/2018 ECE Application Programming: Lecture 11
ECE Application Programming: Lecture 11 Review: if statements Form: if (<expression>) <statement> [ else brackets show <statement> ] else is optional Expression frequently uses relational operators to test equality/inequality < > <= >= == != e.g., if (x <= 5) Can combine conditions using logical operators AND : && OR: || e.g., if ((x <= 5) && (x > 0)) Can test if condition is false using logical NOT: ! e.g., if (!(x < 5)) May test multiple conditions using if/else if/.../else Second condition is tested only if first is false; third condition tested only if first two are false, etc. Else case used only if all previous tests false 2/16/2018 ECE Application Programming: Lecture 11
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!"); } 2/16/2018 ECE Application Programming: Lecture 11
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!"); 2/16/2018 ECE Application Programming: Lecture 11
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); Note these ( ) are needed if ( (n > 10) || (n < 1) ) printf(“That’s not in range!"); else printf("Good job!"); 2/16/2018 ECE Application Programming: Lecture 11
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); Note these ( ) are needed if ( (1 <= n) && (n <= 10) ) printf(“Good job!"); else printf(“That’s not in range!"); 2/16/2018 ECE Application Programming: Lecture 11
if (range checking) (The WRONG WAY) int n; printf("Enter a number 1 to 10: "); scanf("%d",&n); if (1 <= n <= 10 ) // THIS WILL NOT COMPILE printf("Good job!"); else printf(“That’s not in range!"); 2/16/2018 ECE Application Programming: Lecture 11
Example 1: 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 Values are separated by a comma If fewer than 3 values read, print error message with number of values Example: Error: only 2 inputs read correctly 2/16/2018 ECE Application Programming: Lecture 11
ECE Application Programming: Lecture 11 Example 1 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); 2/16/2018 ECE Application Programming: Lecture 11
Example 1 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; } 2/16/2018 ECE Application Programming: Lecture 11
Example 1 solution (cont.) Read 3 int values and print error if input problem Values are separated by a comma 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; } 2/16/2018 ECE Application Programming: Lecture 11
ECE Application Programming: Lecture 3 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 2/16/2018 ECE Application Programming: Lecture 3
switch/case statement - General form switch ( <expression> ) { case <value1> : <statements> [ break; ] case <value2> : <statements> [ break; ] : [ default: <statements> [ break; ] ] } 2/16/2018 ECE Application Programming: Lecture 3
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) 2/16/2018 ECE Application Programming: Lecture 11
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 2/16/2018 ECE Application Programming: Lecture 11
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; } 2/16/2018 ECE Application Programming: Lecture 11
ECE Application Programming: Lecture 11 Next time PE2: Conditional statements Exam 1 Review Monday Be prepared to ask questions! 2/16/2018 ECE Application Programming: Lecture 11