day07 - Conditionals Conditionals If you eat your...

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

View Full Document Right Arrow Icon
1 1 Conditionals 2 Conditionals s “If you eat your vegetables, then you can have dessert.” s “If you do your homework, then you may go outside to play, or else you’ll be grounded for life.” 3 The if statement s if statement : A control structure that executes a block of statements only if a certain condition is true. s General syntax: if ( <test> ) { <statement(s)> ; } s Example: scanf("%lf", &gpa); if (gpa >= 3.0) { printf("Good job! Have a cookie."); } 4 if statement flow chart 5 The if / else statement s 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 . s General syntax: if ( <test> ) { <statement(s)> ; } else { <statement(s)> ; } s 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
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 7 Boolean expressions s 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++) { s These tests evaluate to a Boolean value (either true or false ). 8 Relational expressions s 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 s 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 s 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 s Truth tables of each operator used with truth values p and q true false true false false false true false false true q false false true true true true p || q p true false false true !p p 12 Boolean expressions s What is the result of each of the following expressions? int x = 42; int y = 17; int z = 25; b b x % 2 == y % 2 || x % 2 == z % 2 b b b (x + y) % 2 == 0 || !((z - y) % 2 == 0) s Answers: true , false , true , true , false
Background image of page 2
3 13 Interpreting truth in C s When used as numbers, true is represented as 1 and false is represented as 0
Background image of page 3

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

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

This note was uploaded on 04/05/2010 for the course CSE cse142 taught by Professor Benson during the Spring '10 term at Punjab Engineering College.

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 Right Arrow Icon
Ask a homework question - tutors are online