Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger Fall 2018 Lecture 9: Range checking with if statements
2
Announcements/reminders
Grader: Pakin Pongcheewin Hours: W/F 2-6 (please contact by first) Program 1 resubmissions due Wed, 9/26 You must Dr. Geiger and Pakin when you resubmit Program 2 due today Program 3 due Monday, 10/1 Looking ahead: Exam 1 on Friday, 10/5 Will be allowed one double-sided 8.5” x 11” note sheet No calculators or other electronic devices allowed 5/25/2019 ECE Application Programming: Lecture 9
3
ECE Application Programming: Lecture 9
Lecture outline Review If statements Today’s lecture Program 3 intro Range checking with if statements 5/25/2019 ECE Application Programming: Lecture 9
4
Program 3: bitwise operators
Basic 1-bit Boolean calculator 1-bit values = 0 or 1 Boolean operations: bitwise logical operations & (AND), | (OR), ^ (XOR) Not the same as &&, || used to combine conditions Bitwise operators are binary operators Given unsigned x = 1; unsigned y = 0; printf("x | y = %u, x & y = %u, x ^ y = %u\n", x | y, x & y, x ^ y); would print x | y = 1, x & y = 0, x ^ y = 1 5/25/2019 ECE Application Programming: Lecture 9
5
Bitwise Logical Operations
AND A B A&B 1 NOT (not needed for P3) A ~ A 1 OR XOR (exclusive or) A B A|B 1 A B A^B 1 5/25/2019 ECE Application Programming: Lecture 9
6
Program 3: conditional statements
Conditional statements will be required to Check for errors Evaluate which operator user entered Don’t use conditional statements to evaluate bitwise operations! You don’t need to directly implement truth tables Operators will do that work for you 5/25/2019 ECE Application Programming: Lecture 9
7
ECE Application Programming: Lecture 9
Review: if 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)) 5/25/2019 ECE Application Programming: Lecture 9
8
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!"); } 5/25/2019 ECE Application Programming: Lecture 9
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!"); 5/25/2019 ECE Application Programming: Lecture 9
10
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!"); 5/25/2019 ECE Application Programming: Lecture 9
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); if ( (1 <= n) && (n <= 10) ) printf(“Good job!"); else printf(“That’s not in range!"); 5/25/2019 ECE Application Programming: Lecture 9
12
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!"); 5/25/2019 ECE Application Programming: Lecture 9
13
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 5/25/2019 ECE Application Programming: Lecture 9
14
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); 5/25/2019 ECE Application Programming: Lecture 9
15
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; } 5/25/2019 ECE Application Programming: Lecture 9
16
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; } 5/25/2019 ECE Application Programming: Lecture 9
17
ECE Application Programming: Lecture 9
Final notes Next time Switch statements While loops Reminders: Grader: Pakin Pongcheewin Hours: W/F 2-6 (please contact by first) Program 1 resubmissions due Wed, 9/26 You must Dr. Geiger and Pakin when you resubmit Program 2 due today Program 3 due Monday, 10/1 Looking ahead: Exam 1 on Friday, 10/5 Will be allowed one double-sided 8.5” x 11” note sheet No calculators or other electronic devices allowed 5/25/2019 ECE Application Programming: Lecture 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.