Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2012 Lecture 14: Range checking, switch statements

2 ECE Application Programming: Lecture 14
Lecture outline Announcements/reminders Program 4 due today; Program 5 to be posted ASAP Regrades on previous programs coming (no, really) Exams to be returned Wednesday Midterm feedback forms on Wednesday Today Review: if Range checking with if switch statements 7/4/2018 ECE Application Programming: Lecture 14

3 Review: conditional statements
Conditional execution using 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)) 7/4/2018 ECE Application Programming: Lecture 14

4 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!"); } 7/4/2018 ECE Application Programming: Lecture 14

5 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!"); 7/4/2018 ECE Application Programming: Lecture 14

6 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!"); 7/4/2018 ECE Application Programming: Lecture 14

7 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!"); 7/4/2018 ECE Application Programming: Lecture 14

8 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!"); 7/4/2018 ECE Application Programming: Lecture 14

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 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 7/4/2018 ECE Application Programming: Lecture 14

10 ECE Application Programming: Lecture 14
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); 7/4/2018 ECE Application Programming: Lecture 14

11 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; } 7/4/2018 ECE Application Programming: Lecture 14

12 Example 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; } 7/4/2018 ECE Application Programming: Lecture 14

13 ECE Application Programming: Lecture 14
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 7/4/2018 ECE Application Programming: Lecture 14

14 switch/case statement - General form
switch ( <expression> ) { case <value1> : <statements> [ break; ] case <value2> : <statements> [ break; ] . . [ default: <statements> [ break; ] ] } 7/4/2018 ECE Application Programming: Lecture 14

15 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) 7/4/2018 ECE Application Programming: Lecture 14

16 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 7/4/2018 ECE Application Programming: Lecture 14

17 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; } 7/4/2018 ECE Application Programming: Lecture 14

18 switch/case statement - example
#include <stdio.h> int main() { char grd; printf("Enter Letter Grade: "); scanf("%c",&grd); printf(“You are "); // continued next slide 7/4/2018 ECE Application Programming: Lecture 14

19 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; } 7/4/2018 ECE Application Programming: Lecture 14

20 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: 7/4/2018 ECE Application Programming: Lecture 14

21 ECE Application Programming: Lecture 14
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 7/4/2018 ECE Application Programming: Lecture 14

22 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; } 7/4/2018 ECE Application Programming: Lecture 14

23 ECE Application Programming: Lecture 14
Next time Exam 1 Review Midterm feedback 7/4/2018 ECE Application Programming: Lecture 14


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google