1 / 18

Administrative

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. .

hanne
Télécharger la présentation

Administrative

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. Basic Control Structures • Control order of execution of statements • sequential • selection • iteration

  3. Sequential Statement 1 Statement 2 Statement 3

  4. Selection Statement 1 Statement 2 Statement 2 Statement 3 Statement 3

  5. iteration statements (sequential, selective, or iterative

  6. 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

  7. Boolean Data Expressions • constant (true or false ) • variable bool notDone = true; • relational (5 >= 3) • logical (a || b)

  8. Single-selection if statement if (condition) statement1 if (input < 0) cout << “Invalid input” << endl; (Note: indentation)

  9. 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;

  10. 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;

  11. 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;

  12. Compound Statements if (condition) { statement1; statement2; } else { statement3; statement4; } Documentation standard

  13. 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.

  14. 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

  15. 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

  16. 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.

  17. 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)

  18. 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))

More Related