Presentation is loading. Please wait.

Presentation is loading. Please wait.

IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.

Similar presentations


Presentation on theme: "IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators."— Presentation transcript:

1 IF STATEMENTS AND BOOLEAN EXPRESSIONS

2 BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators like AND and OR

3 RELATIONAL OPERATORS <Less than >Greater than <=Less than or equal to >=Greater than or equal to ==Equals Not to be confused with the assignment operator = !=Not equal to

4 BOOLEAN OPERATORS ||Or as in if a equals b or b equals c if (a == b || b == c) If either comparison is true than the equation is true. If both comparisons are false than the whole equation is false &&And as in if a equal b and b equals c If (a == b && b == c) If either comparison is false than the equation is false. Both comparisons must be true for the equation to be true

5 SIMPLE IF STATEMENT if (coin == 0) MessageBox.Show("Heads"); If the value of coin is 0 then a message box is displayed. If coin has a value of anything else nothing is displayed.

6 ADDING THE ELSE OPTION if (coin == 0) MessageBox.Show("Heads"); else MessageBox.Show("Tails"); If coin holds a 0 then Heads is displayed otherwise Tails is displayed

7 CODE BLOCKS Enclosed in curly brackets { } Allow multi statements to be treated as a single block of code Recommended even for single statement “blocks” May be used with simple if statements or more complex if/else and if/else if/else statements

8 ADDING CODE BLOCKS If (coin == 0) { heads++; MessageBox.Show("Heads"); } else { tails++; MessageBox.Show("Tails"); }

9 ELSE IF if (coin == 0) { heads++; MessageBox.Show("Heads"); } else if (coin == 1) { tails++; MessageBox.Show("Tails"); } else MessageBox.Show("Whoops");

10 BOOLEAN EXPRESSION – OR OPERATOR if (coin == 1 || coin == 0) MessageBox.Show("Good roll"); If either comparison is true the message is displayed. Only if neither comparison is true is nothing displayed

11 BOOLEAN EXPRESSIONS – AND OPERATOR if (coin == 1 && last == coin) MessageBox.Show("Two tails in a row"); The message is displayed only if both comparisons are true If the first comparison is false the second comparison will not be tried This is called short circuiting

12 LET’S START WITH THE CODE HUNT PUZZLES Click on the Capture Code button to test the code

13

14

15


Download ppt "IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators."

Similar presentations


Ads by Google