**Bummer!** This is just a preview. You need to be signed in with a Basic account to view the entire video.

Start a free Basic trial

to watch this video

Methods define an object's behaviors.

In this video we used three different conditional operators. `&&`

is called the conditional-and operator, `||`

is the conditional-or operator, and `!`

is the logical negation operator. These are also known as boolean logic operators. When learning boolean logic it is often helpful to see the truth tables of the operations. Truth tables list all the possible values of the operands that the operators can take. The far right column of the table shows the result of doing the operation.

X | Y | X && Y |
---|---|---|

true | true | true |

true | false | false |

false | true | false |

false | false | false |

X | Y | X || Y |
---|---|---|

true | true | true |

true | false | true |

false | true | true |

false | false | false |

X | !X |
---|---|

true | false |

false | true |

**Short-Circuiting**

When evaluating an expression involving `&&`

or `||`

operators, C# uses a practice called short-circuiting. This means that C# will try to do as little work as possible when evaluating a boolean expression. The code "short-circuits" differently depending on the operator being used.

For example, consider this expression:

```
x || y || z
```

If `x`

is true, then C# won't even consider the values of `y`

or `z`

, because when using `||`

only one of the values must be true in order for the entire expression to evaluate to true. Look at the truth table for `||`

to see this fact.

Likewise, when using `&&`

:

```
x && y && z
```

If `x`

is false, then C# won't look at the values of `y`

or `z`

, because when using `&&`

only one of the values must be false in order for the entire expression to evaluate to false. Look at the truth table for `&&`

to see this fact.

This is especially important to understand when the conditional expression contains more complex code. Consider this example:

```
x || SomeMethodThatReturnsABoolean()
```

If `x`

is true, then the method will not be run. If `x`

is false, then C# will continue evaluating the expression looking for the first operand that evaluates to true, and the method will be run as part of that evaluation.

In general it's best practice to keep conditional expressions as simple as possible. There are a few examples of times when short-circuiting can be very helpful. Consider the following example where I only want to do something if the `names`

array contains at least one item.

```
if(names != null && names.Length > 0)
{
// Do something with names
}
```

Without short-circuiting, accessing the `Length`

property of the `names`

array would throw an exception if `names`

was `null`

. But with short-circuiting, `names.Length`

will never get run if `names`

is `null`

so it's safe to put both these expressions together. Just remember that order is important. Boolean expressions are evaluated from left to right, with respect to parenthesis of course.

[MUSIC] 0:00 So far we've only been modeling an object's attributes. 0:04 But we also want to define what an object can do. 0:08 Remember the Pez dispenser example. 0:12 The Pez dispenser has both attributes and behaviors. 0:14 Pez dispensers have attributes like color, and how many pieces of Pez are in it. 0:18 Points and maps also have attributes. 0:23 A point's attributes are its x and y coordinates. 0:26 A map's attributes are its width and height dimensions. 0:29 The Pez dispenser also has some behaviors. 0:33 It can be refilled. 0:36 It can dispense Pez. 0:37 This is what the Pez dispenser can do. 0:39 In code, objects' behaviors are defined using methods. 0:41 One of the behaviors we want our map class to have 0:46 is the ability to determine if the point is on the map. 0:48 The best way to find that out is to ask the map 0:52 because the map class is responsible for the map boundaries. 0:55 To allow the map to answer this question, we need to write a method. 0:59 Let's add some methods to our classes. 1:03 I've opened up the Map.cs file in WorkSpaces. 1:07 Let's add a method to the map class called OnMap. 1:10 The OnMap method will take a point as a parameter and 1:14 will return true if the point is on the map and false if it isn't. 1:17 I'll say, public bool OnMap (Point point), 1:22 open curly brace, closing curly brace. 1:28 There we go. All right, 1:35 as you can see inside the parentheses of the OnMap method, 1:37 we have a parameter called point with a lowercase p. 1:40 Which is of type Point with an uppercase P. 1:44 This is very similar to how we specified parameters in the constructor. 1:47 Only this time, the point isn't one of the types built into C#, 1:51 it's one we created ourselves. 1:54 This method will return a value. 1:58 So we specified the return type as bool here. 2:00 It's a Boolean because this method will return only true or false. 2:04 Remember, we can write methods that don't return anything. 2:09 If that was the case, we would have written void here. 2:12 We'll see some of those later. 2:16 Finally our method needs to be public, so 2:18 that we can call it from outside of the Map class. 2:21 Now let's write the body of the method. 2:24 Given a point we need to determine if the point is within the bounds of the map. 2:26 Let's look at an example map again. 2:31 The map has a width of 8 and a height of 5. 2:34 So all points on this map need to have an X value that's greater than or 2:37 equal to 0 and less than 8. 2:42 The Y value must be greater than or equal to 0 and less than 5. 2:44 As you can see every point must pass these four conditions 2:50 in order to be considered on the map. 2:54 In code, let's start off 2:56 by declaring the variable called inBounds to hold the return value of our method. 2:59 Now we set this variable equal to the result of checking our four conditions. 3:06 A point is inBounds if point.x is greater than or 3:10 equal to 0, and if point.x is less than Width. 3:15 And if point.y is greater than or equal to 0, and if point.y is less than Height. 3:25 Finally we'll return the value stored in inBounds as a return value. 3:28 See how I put two ampersands in between each of these four conditions? 3:34 This is the conditional and operator. 3:39 The result of an and operation is true if and 3:41 only if both the left and the right side of the and operator is true. 3:44 So this expression will only evaluate to true if X greater than 0 is true and 3:50 X less than Width is true. 3:55 And Y greater than or equal to 0 is true, and Y less than Height this true. 3:57 If all four of these conditions are true, 4:03 then the entire expression evaluates to true. 4:05 And inBounds, will be set to true. 4:08 If any of them are false, then inBounds will be false. 4:11 That's what we want. 4:14 In other words, if any one of these conditions are false, 4:16 it means the point is outside the bounds of the map. 4:19 Another conditional operator is or, which is written as a double bar. 4:22 The result of an or operation is true if either the left or the right side is true. 4:27 For example if we wanted to check if the point was out of bounds, 4:34 then we could use the or operator like this. 4:38 The point is outOfBounds if 4:42 point.X is less than 0 or 4:47 point.x is greater than or equal to Width. 4:51 Or point.y is less than 0, or 5:00 point.y is greater than or equal to Height. 5:05 Pause the video, and take a minute to compare these two expressions. 5:13 This will give you a good idea of how the or operator is different than the and 5:17 operator. 5:22 Then we could check if the point is inBounds by using the not operator. 5:27 The exclamation mark gives us the opposite of a boolean. 5:36 It's sometimes called the boolean negation operator. 5:41 So after adding the not operator, 5:45 if outOfBounds was true, then it becomes false. 5:47 And if it was false, then it becomes true. 5:50 We could also just negate this entire expression by wrapping it in parentheses 5:54 and using the not operator to negate the entire thing. 5:58 Now, both these lines are equivalent to each other. 6:20 The difference is one's using and, and the other is using or with a not. 6:23 You might want to pause the video again to compare these expressions and 6:30 see why this works. 6:35 In the case of the OnMap method, using the and operator to check of the point is 6:38 inBounds is more straightforward, so that's what we'll do. 6:42 We'll see more examples of using or, and and not in the future. 6:49 But I recommend checking out the teacher's notes for 6:53 more information about boolean logic in C#. 6:55 Let's take a short break before we continue analyzing 6:58 the rest of this method. 7:01

You need to sign up for Treehouse in order to download course files.

Sign up