{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

This preview shows pages 1–4. Sign up to view the full content.

1 1 Conditionals 2 Conditionals square6 “If you eat your vegetables, then you can have dessert.” square6 “If you do your homework, then you may go outside to play, or else you’ll be grounded for life.” 3 The if statement square6 if statement : A control structure that executes a block of statements only if a certain condition is true. square6 General syntax: if ( <test> ) { <statement(s)> ; } square6 Example: scanf("%lf", &gpa); if (gpa >= 3.0) { printf("Good job! Have a cookie."); } 4 if statement flow chart 5 The if / else statement square6 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 . square6 General syntax: if ( <test> ) { <statement(s)> ; } else { <statement(s)> ; } square6 Example: scanf("%lf", &gpa); if (gpa >= 3.0) { printf("Good job! Have a cookie."); } else { printf("No cookie for you!"); } 6 if / else statement flow chart

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

View Full Document
2 7 Boolean expressions square6 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++) { square6 These tests evaluate to a Boolean value (either true or false ). 8 Relational expressions square6 Relational expressions are Boolean expressions that compare two values by using the following relational operators: true 5.0 >= 5.0 greater than or equal to >= false 126 <= 100 less than or equal to <= true 10 > 5 greater than > false 10 < 5 less than < true 3.2 != 2.5 does not equal != true 1 + 1 == 2 equals == Value Example Meaning Operator 9 Evaluating relational expressions square6 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 10 Logical operators square6 Can combine relational operators with logical operators !(7 > 0) (2 == 3) || (-1 < 5) (9 != 6) && (2 < 3) Example not or and Description false ! true || true && Result Operator 11 Truth tables square6 Truth tables of each operator used with truth values p and q true false true false false false true p && q false false true q false false true true true true p || q p true false false true !p p 12 Boolean expressions square6 What is the result of each of the following expressions? int x = 42; int y = 17; int z = 25; boxshadowdwn y < x && y <= z boxshadowdwn x % 2 == y % 2 || x % 2 == z % 2 boxshadowdwn x <= y + z && x >= y + z boxshadowdwn !(x < y && x < z) boxshadowdwn (x + y) % 2 == 0 || !((z - y) % 2 == 0) square6 Answers: true , false , true , true , false
3 13 Interpreting truth in C square6 When used as numbers, true

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

day07 - Conditionals Conditionals If you eat your...

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

View Full Document
Ask a homework question - tutors are online