1 / 28

CISC105 – General Computer Science

CISC105 – General Computer Science. Class 3 – 06/12/2006. Control Structures. Selection. if. if … else. statement. statement. (single selection). (double selection). T. F. T. F. switch. statement. (multiple selection). T. break. F. T. break. F. T. break. F.

broderickm
Download Presentation

CISC105 – General Computer Science

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. CISC105 – General Computer Science Class 3 – 06/12/2006

  2. Control Structures Selection if if…else statement statement (single selection) (double selection) T F T F switch statement (multiple selection) T break F T break F . . . T break F

  3. Control Structures (cont) Repetition (Chapter 5) … while statement do while statement for statement T F T T F F

  4. Conditions • Condition – an expression that is either true (usually represented as a 1) or false (usually represented as a 0) • Conditions are tested by either using a relational or equality operator

  5. Relational and Equality Operators • < Less Than (relational) • > Greater Than (relational) • <= Less Than or equal to (relational) • >= Greater Than or equal to (relational) • == Equal to (equality) • != Not equal to (equality)

  6. Logical Operators • || OR operator • && AND operator • ! NOT operator Note: Much of the math for Control Structures comes from the work of George Bool (Boolean Algebra)

  7. Operator “Truth Tables”

  8. Operator PrecedenceHighest to Lowest • Parenthesis • Function Calls • ! + - & (Unary Operators) • * / % • + - • < <= >= > • == != • && • || • = (note difference to ==)

  9. Operator Precedence

  10. Short Circuit Evaluation • Short-Circuit Evaluation – C will stop the evaluation of a logical expression as soon as the value can be determined • If a is true C will not evaluate b in the expression a || b • If a is false C will not evaluate b in the expression a && b

  11. Evaluate the following Assume a = 5 , b = 10, c = 15, flag = 1 • c == a + b || !flag • a != 7 && flag || c >= 6 • !(b <= 12) && a % 2 == 0 • !(a > 5 || c < a + b)

  12. De Morgan’s Theorem • Augustus De Morgan originally observed that in classical propositional logic the following relationships hold: • not (P and Q) = (not P) or (not Q) • not (P or Q) = (not P) and (not Q) • De Morgan's observation influenced the algebraisation of logic undertaken by George Boole, which cemented De Morgan's claim to the find, although a similar observation was made by Aristotle and was known to Greek and Medieval logicians (cf. Bocheński's History of Formal Logic). From: http://en.wikipedia.org/wiki/DeMorgans_Law

  13. The if statement • Single Statement – execute a statement if a condition holds true • Compound Statement – execute a series of statements if a condition holds true • Statement with two (or more) alternatives – execute a statement if a condition holds true or else run a separate statement if it is false (Hint: Flowcharts will help you with the step-by-step control flow)

  14. if statement flowcharts

  15. Single alternative if statement if (value > 100) printf(“Value is over 100\n”); The printf statement will only execute if value is greater than 100!

  16. if statement with 2 alternatives if (value > 100) printf(“Value is over 100\n”); else printf(“Value is under 100\n”); Will only execute one of the printf statements dependent on value. Any problem with the above statement? What if value == 100? Common Errors with not thinking through all error conditions!

  17. if with compound statements if (value1 == 100) { printf(“Value is 100\n”); printf(“That’s a relief\n”); } else { printf(“Value is NOT 100\n”); printf(“What ever could the value be?\n); }

  18. Debugging Tip • Hand-Trace – run a step-by-step simulation of the algorithms execution with pencil and paper • Example (What could this be used for?): if (x > y) { temp = x; x = y; y = temp; }

  19. Multiple Alternatives

  20. Nested if Statements • Insurance company wants to calculate driver risk if (maritial_status == ‘S’) if (gender == ‘M’) if (age >= 16 && age <= 25) printf(“Charge a lot more\n”); OR if ( maritial_status == ‘S’ && gender == ‘M’ && age >= 16 && age <= 25 ) printf(“Charge a lot more\n”); Both statements are equivalent – which is more readable?

  21. Nested if Statements if (road_status == ‘S’) { if (temp > 0) { printf(“Wet roads ahead\n”); printf(“Stop time doubled\n”); } else { printf(“Icy roads ahead\n”); printf(“Stop time quadrupled\n”); } } /* Are these brackets necessary? Why? */ else printf(“Please drive carefully\n”);

  22. Nested if • To show multiple alternatives using nested if after else if (value > 0) num_pos = num_pos + 1; else if (value < 0) num_neg = num_neg + 1; else num_zero = num_zero + 1;

  23. The switch statement • Switch statement can be used to select from one of several alternatives. • Useful when a controlling expressions (a simple expression or variable) value is used to determine the choice. • Commonly used for text based menus • Also known as Jump tables.

  24. The switch statement

  25. The default: Case • The default case is run when the input does not match any case! • Do you always need as default case?

  26. switch vs if • You could use a series of nested if statements to write the same logic as the switch statement • switch is more readable • switch can only handle a maximum of 10 cases (if can handle unlimited) • Away use a default label in a switch statement

  27. Common Programming Errors • Forgetting Operator Precedence Rules • if (0 <= x <= 4) /* will always be true! */ • if (0 <= x && x <= 4) /* is correct */ • Not using the correct operator • if (x = 10) /* sets x equal to 10 & is true */ • if (x == 10) /* is correct */ • if (x = 0) /* sets x equal to 0 and is false */ • Make sure you use braces for an else statement to be associated with correct if statement

  28. Lab01 • 15 short programs to write. • Programs 8, 14 & 15 require the use of repitition statements (while, do … while and/or for structures) that will be discussed next class (6/14/2006) • Questions or Concerns about the Labs?

More Related