Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger & Dr. Lin Li Spring 2019 Lecture 8: If statements Program 3 overview

2 Announcements/reminders
Textbook exercises due 3 days after each lecture Program 2 due today Program 3 due Monday, 2/25 zyBooks IDE now allows interactive program runs—will demonstrate later in class Exam 1: 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 ( 4/27/2019 ECE Application Programming: Lecture 8

3 ECE Application Programming: Lecture 8
Today’s lecture Review If statements Else blocks Else if blocks Program 3 overview 4/27/2019 ECE Application Programming: Lecture 8

4 ECE Application Programming: Lecture 8
Review: Decisions Conditionally execute some path May want to: Only perform operation if condition is true: A = 0? TRUE FALSE A = 0? TRUE X = x + 1 FALSE 4/27/2019 ECE Application Programming: Lecture 8

5 ECE Application Programming: Lecture 8
Review: if statements Conditional execution using if statements: Form: if (<expression>) <statement> 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)) 4/27/2019 ECE Application Programming: Lecture 8

6 ECE Application Programming: Lecture 8
Decisions (cont.) May want to: Perform one operation if condition is true, another if false: Uses if/else construct A = 0? TRUE x = x + 1 FALSE x = x - 1 4/27/2019 ECE Application Programming: Lecture 8

7 ECE Application Programming: Lecture 8
If/else if (A == 0) x = x + 1; else x = x - 1; if (a+6*3-43) printf("wow is this not cool"); else printf("this is not cool"); 4/27/2019 ECE Application Programming: Lecture 8

8 ECE Application Programming: Lecture 8
Decisions (cont.) May want to: Check multiple conditions, in order A = 0? TRUE x = x + 1 FALSE B=1? TRUE x = x - 1 FALSE x = 0 4/27/2019 ECE Application Programming: Lecture 8

9 ECE Application Programming: Lecture 8
If/else if if (A == 0) x = x + 1; else if (B == 1) x = x – 1; else x = 0; No limit on number of else if blocks else block isn’t required, even if you use else if 4/27/2019 ECE Application Programming: Lecture 8

10 ECE Application Programming: Lecture 8
if (common pitfalls) a single equals means ASSIGN. a double equal must be used to check for equality. x=12345; if (x=3) { printf("x is 3\n"); } else { printf("x is not 3\n"); } This code will ALWAYS print: x is 3 4/27/2019 ECE Application Programming: Lecture 8

11 ECE Application Programming: Lecture 8
if (example) void main(void) { float a,b,c,disc; : scanf("%f %f %f",&a,&b,&c); if (a==0) { // statements } else { disc = b*b-4*a*c; if ( disc < 0 ) { // statements } else { // statements } } } 4/27/2019 ECE Application Programming: Lecture 8

12 Example: if statements
What does the following code print? int main() { int x = 3; int y = 7; if (x > 2) x = x - 2; else x = x + 2; if ((y % 2) == 1) { y = -x; if ((x != 0) && (y != -1)) y = 0; } printf("x = %d, y = %d\n", x, y); return 0; 4/27/2019 ECE Application Programming: Lecture 8

13 ECE Application Programming: Lecture 8
Example solution int main() { int x = 3; int y = 7; if (x > 2)  Condition is true, since 3 > 2 x = x - 2;  x set to 1 else x = x + 2; if ((y % 2) == 1)  Tests if y is an odd number--true condition { y = -x;  y set to -1 if ((x != 0) && (y != -1))  First part of condition is true, second part is false--overall false y = 0; } printf("x = %d, y = %d\n", x, y);  Prints: x = 1, y = -1 return 0; 4/27/2019 ECE Application Programming: Lecture 8

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

15 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 4/27/2019 ECE Application Programming: Lecture 8

16 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 4/27/2019 ECE Application Programming: Lecture 8

17 ECE Application Programming: Lecture 8
Final notes Next time Range checking with if statements Reminders: Textbook exercises due 3 days after each lecture Program 2 due Monday, 2/11 4/27/2019 ECE Application Programming: Lecture 8


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google