180 likes | 310 Views
Administrative. MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday Quiz on Thursday (material you want to go over? tomorrow’s class) Homework - will stay late after class tomorrow. .
E N D
Administrative • MUST GO TO CORRECT LAB SECTION! • Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday • Quiz on Thursday (material you want to go over? tomorrow’s class) • Homework - will stay late after class tomorrow.
Basic Control Structures • Control order of execution of statements • sequential • selection • iteration
Sequential Statement 1 Statement 2 Statement 3
Selection Statement 1 Statement 2 Statement 2 Statement 3 Statement 3
iteration statements (sequential, selective, or iterative
boolean Data Type • if some condition is true, do this group of statements, otherwise do this other group of statements (selection) • keep doing this group of statements until some condition is true (iteration) • boolean Data Type holds the condition value true or false • ANSI standard bool, true and false
Boolean Data Expressions • constant (true or false ) • variable bool notDone = true; • relational (5 >= 3) • logical (a || b)
Single-selection if statement if (condition) statement1 if (input < 0) cout << “Invalid input” << endl; (Note: indentation)
Double selection if if (condition) statement1 // Note: indentation else statement2; int numerator = 0, denominator = 0, quotient = 0; cout << “enter two integers: “; cin >> numerator >> denominator; if (0 == denominator) cout << “Division by zero is not allowed! “ << endl; else quotient = numerator/ denominator;
if (denominator == 0) cout << “Division by zero is not allowed! “ << endl; else quotient = numerator/ denominator; OR we could have written: if (denominator != 0) quotient = numerator/ denominator; else cout << “Division by zero is not allowed! “ << endl; OR we could have written: if (denominator) //boolean implemented as integer >0 is true quotient = numerator/ denominator; else cout << “Division by zero is not allowed! “ << endl;
Multi-selection if statements int score; cout << “Enter score: “; cin >> score; cout << “Your grade is “; if (score < 68) cout << “NR” << endl; else if (score < 78) cout << “C” << endl; else if (score < 88) cout << “B” << endl else cout << “A” << endl;
Compound Statements if (condition) { statement1; statement2; } else { statement3; statement4; } Documentation standard
Using braces What happens in the following code if we leave out braces around else statements? bool bFlag = false; if (bFlag){ cout << "condition is true " << endl; cout << "TRUE" << endl; } else cout << "condition is false " << endl; cout << "FALSE" << endl; Can use { } for single statement too.
Common programming errors • forgetting one or both braces • putting a ; after the condition in a single-selection if statement. if (bFlag); cout << "condition is true " << endl; • confusing = with == if (denominator == 0) cout << “Division by zero! “ << endl; else quotient = numerator/ denominator; • dangling else
Dangling Else if (grade > 68) if (grade >= 88) cout << “ You get an A” << endl; else cout << “You failed” << endl; what does programmer expect? grade = 75 what will really happen? fix with braces
1) What is the error in the following statement? Fix it. if (age >= 65); cout << “Age is greater than or equal to 65” << endl; 2) What is the error in the following statement? Fix it. if (count > 10 ) cout << “buffer is full” << endl; else; cout << “buffer is not full”; 3) What is displayed on the screen int x = 0; if (x = 0) cout << “x is equal to 0” << endl; else cout << “x is not equal to 0” << endl; 4) Create an if statement that checks a character variable and determines if it is an uppercase or lowercase letter a and displays “Found letter a!” if it is.
DeMorgan’s Laws Used to simplify logical expressions ! (condition1 && condition2) is logically equivalent to the expression (! condition1 || !condition2). Also, !(condition1 || condition2) is equivalent to the expression (!condition1 && ! condition2)
Group Exercise Use DeMorgan’s Laws to write equivalent expressions for each of the following: a) !(x < 5) && ! (y >= 7) b) ! (a == b) || ! (g != 5) c) !((x <= 8) && (y > 4)) d) !((i>4) || (j <= 6))