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

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

View Full Document Right Arrow Icon
    1 Conditionals
Background image of page 1

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

View Full DocumentRight 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.”
Background 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."); }
Background image of page 3

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

View Full DocumentRight Arrow Icon
    4 if  statement flow chart
Background 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!"); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
    6 if / else  statement flow chart
Background 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 ).
Background image of page 7

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

View Full DocumentRight 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
Background 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
Background image of page 9

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

View Full DocumentRight 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
Background image of page 10
    11 Truth tables Truth tables of each operator used with truth values p and 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
Background image of page 11

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

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

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

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

This note was uploaded on 04/29/2009 for the course EE 142 taught by Professor Bensollimketkai during the Spring '09 term at Washington State University .

Page1 / 36

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

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online