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 conditionaland operator, 
is the conditionalor 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 
ShortCircuiting
When evaluating an expression involving &&
or 
operators, C# uses a practice called shortcircuiting. This means that C# will try to do as little work as possible when evaluating a boolean expression. The code "shortcircuits" 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 shortcircuiting 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 shortcircuiting, accessing the Length
property of the names
array would throw an exception if names
was null
. But with shortcircuiting, 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.

0:00
[MUSIC]

0:04
So far we've only been modeling an object's attributes.

0:08
But we also want to define what an object can do.

0:12
Remember the Pez dispenser example.

0:14
The Pez dispenser has both attributes and behaviors.

0:18
Pez dispensers have attributes like color, and how many pieces of Pez are in it.

0:23
Points and maps also have attributes.

0:26
A point's attributes are its x and y coordinates.

0:29
A map's attributes are its width and height dimensions.

0:33
The Pez dispenser also has some behaviors.

0:36
It can be refilled.

0:37
It can dispense Pez.

0:39
This is what the Pez dispenser can do.

0:41
In code, objects' behaviors are defined using methods.

0:46
One of the behaviors we want our map class to have

0:48
is the ability to determine if the point is on the map.

0:52
The best way to find that out is to ask the map

0:55
because the map class is responsible for the map boundaries.

0:59
To allow the map to answer this question, we need to write a method.

1:03
Let's add some methods to our classes.

1:07
I've opened up the Map.cs file in WorkSpaces.

1:10
Let's add a method to the map class called OnMap.

1:14
The OnMap method will take a point as a parameter and

1:17
will return true if the point is on the map and false if it isn't.

1:22
I'll say, public bool OnMap (Point point),

1:28
open curly brace, closing curly brace.

1:35
There we go. All right,

1:37
as you can see inside the parentheses of the OnMap method,

1:40
we have a parameter called point with a lowercase p.

1:44
Which is of type Point with an uppercase P.

1:47
This is very similar to how we specified parameters in the constructor.

1:51
Only this time, the point isn't one of the types built into C#,

1:54
it's one we created ourselves.

1:58
This method will return a value.

2:00
So we specified the return type as bool here.

2:04
It's a Boolean because this method will return only true or false.

2:09
Remember, we can write methods that don't return anything.

2:12
If that was the case, we would have written void here.

2:16
We'll see some of those later.

2:18
Finally our method needs to be public, so

2:21
that we can call it from outside of the Map class.

2:24
Now let's write the body of the method.

2:26
Given a point we need to determine if the point is within the bounds of the map.

2:31
Let's look at an example map again.

2:34
The map has a width of 8 and a height of 5.

2:37
So all points on this map need to have an X value that's greater than or

2:42
equal to 0 and less than 8.

2:44
The Y value must be greater than or equal to 0 and less than 5.

2:50
As you can see every point must pass these four conditions

2:54
in order to be considered on the map.

2:56
In code, let's start off

2:59
by declaring the variable called inBounds to hold the return value of our method.

3:06
Now we set this variable equal to the result of checking our four conditions.

3:10
A point is inBounds if point.x is greater than or

3:15
equal to 0, and if point.x is less than Width.

3:25
And if point.y is greater than or equal to 0, and if point.y is less than Height.

3:28
Finally we'll return the value stored in inBounds as a return value.

3:34
See how I put two ampersands in between each of these four conditions?

3:39
This is the conditional and operator.

3:41
The result of an and operation is true if and

3:44
only if both the left and the right side of the and operator is true.

3:50
So this expression will only evaluate to true if X greater than 0 is true and

3:55
X less than Width is true.

3:57
And Y greater than or equal to 0 is true, and Y less than Height this true.

4:03
If all four of these conditions are true,

4:05
then the entire expression evaluates to true.

4:08
And inBounds, will be set to true.

4:11
If any of them are false, then inBounds will be false.

4:14
That's what we want.

4:16
In other words, if any one of these conditions are false,

4:19
it means the point is outside the bounds of the map.

4:22
Another conditional operator is or, which is written as a double bar.

4:27
The result of an or operation is true if either the left or the right side is true.

4:34
For example if we wanted to check if the point was out of bounds,

4:38
then we could use the or operator like this.

4:42
The point is outOfBounds if

4:47
point.X is less than 0 or

4:51
point.x is greater than or equal to Width.

5:00
Or point.y is less than 0, or

5:05
point.y is greater than or equal to Height.

5:13
Pause the video, and take a minute to compare these two expressions.

5:17
This will give you a good idea of how the or operator is different than the and

5:22
operator.

5:27
Then we could check if the point is inBounds by using the not operator.

5:36
The exclamation mark gives us the opposite of a boolean.

5:41
It's sometimes called the boolean negation operator.

5:45
So after adding the not operator,

5:47
if outOfBounds was true, then it becomes false.

5:50
And if it was false, then it becomes true.

5:54
We could also just negate this entire expression by wrapping it in parentheses

5:58
and using the not operator to negate the entire thing.

6:20
Now, both these lines are equivalent to each other.

6:23
The difference is one's using and, and the other is using or with a not.

6:30
You might want to pause the video again to compare these expressions and

6:35
see why this works.

6:38
In the case of the OnMap method, using the and operator to check of the point is

6:42
inBounds is more straightforward, so that's what we'll do.

6:49
We'll see more examples of using or, and and not in the future.

6:53
But I recommend checking out the teacher's notes for

6:55
more information about boolean logic in C#.

6:58
Let's take a short break before we continue analyzing

7:01
the rest of this method.
You need to sign up for Treehouse in order to download course files.
Sign up