EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
ECE Application Programming
ECE Application Programming
The Ohio State University
Microprocessor Systems Design I
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
CHAPTER 4 Selection CSEG1003 Introduction to Computing
ECE Application Programming
EECE.2160 ECE Application Programming
Conditional Statements
Writing a program with conditionals
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
EECE.2160 ECE Application Programming
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructors: Dr. Michael Geiger Fall 2018 Lecture 9: Range checking with if statements

Announcements/reminders Grader: Pakin Pongcheewin E-mail: Pakin_Pongcheewin@student.uml.edu Hours: W/F 2-6 (please contact by e-mail first) Program 1 resubmissions due Wed, 9/26 You must e-mail 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

ECE Application Programming: Lecture 9 Final notes Next time Switch statements While loops Reminders: Grader: Pakin Pongcheewin E-mail: Pakin_Pongcheewin@student.uml.edu Hours: W/F 2-6 (please contact by e-mail first) Program 1 resubmissions due Wed, 9/26 You must e-mail 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