Upgrade to Pro — share decks privately, control downloads, hide ads and more …

07-ConditionalStatements.pdf

Avatar for William Albritton William Albritton
September 04, 2015
120

 07-ConditionalStatements.pdf

Avatar for William Albritton

William Albritton

September 04, 2015
Tweet

Transcript

  1. Relational Operators  Relational operators  > (greater than) 

    < (less than)  >= (greater than or equal)  <= (less than or equal)  Will return a 0 (false) or 1 (true)
  2. Equality Operators  Equality operators  == (equal)  !=

    (not equal)  Will return a 0 (false) or 1 (true) int num3 = 3, num5 = 5; printf(“%d\n”,num3==num5); //0
  3. Common Error  WARNING: a common error is to use

    the assignment operator (=), instead of the equality operator (==)  Still compiles, so it is a difficult bug to find!  And it will change the value of the 1st operand int num3 = 3, num5 = 5; printf(“%d\n”,num3=num5); //5
  4. Boolean Operator not (!)  !x is the same as

    0==x  For the C language, zero is false  And anything non-zero is true  Determines if x is equal to zero, or not equal to zero int zero = 0, num5 = 5; printf(“%d\n”,!zero); //1 printf(“%d\n”,!num5); //0
  5. Logical Operators  AND (&&), OR (||), NOT (!) 

    Evaluated left to right  Returns true (1) or false (0)
  6. AND (&&) Operator  Has 2 operands  Returns true

    if both operands are true, otherwise false X Y X && Y True True True True False False False True False False False False
  7. AND (&&) Operator  A && B  People who

    like cats AND People who like dogs A (like cats) B (like dogs)
  8. OR (||) Operator  Has 2 operands  Returns true

    if at least one operands is true, otherwise false X Y X || Y True True True True False True False True True False False False
  9. OR (||) Operator  A || B  People who

    like cats OR People who like dogs A (like cats) B (like dogs)
  10. NOT (!) Operator  Has 1 operand  If false

    (0), returns true (1) and if true (not zero) returns false (1) x ! x false true true false
  11. Do not like cats NOT (!) Operator  !A 

    People who do NOT like cats A (like cats)
  12. if(condition){ statement1; statement2; statement3; …. statement n; } Simple if

    Statements true false condition statement statement statement statement
  13. if(condition) { statement1; } else { statement2; } if/else Statement

    true false condition statement 1 statement 2
  14. Comparing Numbers with Decimals  Because of precision and rounding

    differences in floating point numbers, do not use the equality operator (==) to compare two floating points double threshold = 0.00001; if(fabs(number) < threshold){ printf(“Close to zero”); }
  15. if Statements are for Ranges  if (x > 10)

     if (a>0) && (a <10) 0 10 -10 0 10 -10
  16. Take No Shortcuts  In C, it is better to

    not take any shortcuts to writing relational and logical statements  Do not write  If a<b is false, the condition becomes: if( 0 < c)  If a<b is true, the condition becomes: if( 1 < c) if(a < b < c) if((a < b)&&(b < c))
  17. Logical Operator Evaluation  C uses shortcuts with logical operators

    AND, OR, NOT (&&, | |, !), which are evaluated left to right
  18. Logical Operator Evaluation  Evaluation stops as soon as true

    or false is determined  Example:  Will stop if “a && b” is false.  Rewrite: if(a && b && c) if((a && b) && (b && c))
  19. Diagram for a switch Statement case 1 Expression statement list

    1 statement list 2 statement list 3 statement list N case 2 case 3 default
  20. switch Statements Syntax switch(expression) { case label1: //instructions case label2:

    //instructions case label3: //instructions default: //instructions }
  21. The break Statement  The break statement transfers control outside

    the structure  If no break statement is present ALL the code within the switch statement will execute, beginning in the matching case and ending whenever a break statement is found or the switch statement ends
  22. switch vs. if  Use switch statements for discrete values

     Use if statements for ranges  Switch functionality can be duplicated with if statements, many of them…