day07 - Conditionals 1 Conditionals If you eat your...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
    1 Conditionals
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    2 Conditionals “If you eat your vegetables, then you can have dessert.” “If you do your homework, then you may go outside to play, or else you’ll be grounded for life.”
Image of page 2
    3 The  if  statement if statement : A control structure that executes a block of statements only if a certain condition is true. General syntax: if ( <test> ) { <statement(s)> ; } Example: scanf("%lf", &gpa); if (gpa >= 3.0) { printf("Good job! Have a cookie."); }
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    4 if  statement flow chart
Image of page 4
    5 The  if / else  statement if/else statement : A control structure that executes one block of statements if a certain condition is true, and a second block of statements if it is false. We refer to each block as a branch . General syntax: if ( <test> ) { <statement(s)> ; } else { <statement(s)> ; } Example: scanf("%lf", &gpa); if (gpa >= 3.0) { printf("Good job! Have a cookie."); } else { printf("No cookie for you!"); }
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    6 if / else  statement flow chart
Image of page 6
    7 Boolean expressions The <test> used in an if or if/else statement is the same kind seen in a for loop. for (int i = 1; i <= 10 ; i++) { These tests evaluate to a Boolean value (either true or false ).
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    8 Relational expressions Relational expressions are Boolean expressions that compare two values by using the following relational operators: Operator Meaning Example Value == equals 1 + 1 == 2 true != does not equal 3.2 != 2.5 true < less than 10 < 5 false > greater than 10 > 5 true <= less than or equal to 126 <= 100 false >= greater than or equal to 5.0 >= 5.0 true
Image of page 8
    9 Evaluating relational expressions Relational operators have lower precedence than math operators. 5 * 7 >= 3 + 5 * (7 - 1) 5 * 7 >= 3 + 5 * 6 35 >= 3 + 30 35 >= 33 true
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    10 Logical operators Can combine relational operators with logical operators Operator Description Example Result && and (9 != 6) && (2 < 3) true || or (2 == 3) || (-1 < 5) true ! not !(7 > 0) false
Image of page 10
    11 Truth tables Truth tables of each operator used with truth values p and q p q p && q p || q true true true true true false false true false true false true false false false false p !p true false false true
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    12 Boolean expressions What is the result of each of the following expressions? int x = 42; int y = 17; int z = 25; y < x && y <= z x % 2 == y % 2 || x % 2 == z % 2 x <= y + z && x >= y + z !(x < y && x < z) (x + y) % 2 == 0 || !((z - y) % 2 == 0) Answers: true , false , true , true , false
Image of page 12
    13 Interpreting truth in C When used as numbers, true is represented as 1 and
Image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern