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

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

1 Conditionals

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

View Full Document
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.”
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."); }

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

View Full Document
4 if  statement flow chart
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!"); }

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

View Full Document
6 if / else  statement flow chart
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 ).

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
13 Interpreting truth in C When used as numbers,

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.

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