# 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 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; 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
13 Interpreting truth in C When used as numbers, true is represented as 1 and

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 ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern