In this lesson, we'll continue building our hotel cost application from our previous lesson. We'll explore two new C# operators along the way: ||
and !
Currently, our two else if
statements result in the same response to the user:
using System;
class HotelCost
{
static void Main()
{
Console.WriteLine("In what season are you booking a stay?");
string season = Console.ReadLine();
Console.WriteLine("On the weekend or a weeknight?");
string dayOfWeek = Console.ReadLine();
bool summer = season == "summer";
bool weekend = dayOfWeek == "weekend";
if (summer && weekend)
{
Console.WriteLine("Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend.");
}
else if (summer)
{
Console.WriteLine("Your stay might be more expensive than normal!");
}
else if (weekend)
{
Console.WriteLine("Your stay might be more expensive than normal!");
}
else
{
Console.WriteLine("Your stay might be expensive, but it could be worse!");
}
}
}
||
We can DRY up our code by using the ||
operator. ||
means or. Check it out:
using System;
class HotelCost
{
static void Main()
{
Console.WriteLine("In what season are you booking a stay?");
string season = Console.ReadLine();
Console.WriteLine("On the weekend or a weeknight?");
string dayOfWeek = Console.ReadLine();
bool summer = season == "summer";
bool weekend = dayOfWeek == "weekend";
if (summer && weekend)
{
Console.WriteLine("Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend.");
}
else if (summer || weekend)
{
Console.WriteLine("Your stay might be more expensive than normal!");
}
else
{
Console.WriteLine("Your stay might be expensive, but it could be worse!");
}
}
}
Both ||
and &&
evaluate two separate booleans. While &&
only evaluates to true
if both booleans are true
, ||
will evaluate to true
if either boolean is true
. For ||
to evaluate to false
, both booleans must be false
.
In the code above, the first conditional still uses the &&
operator. If both summer
and weekend
are true, the program prints "Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend."
Otherwise, the program moves to the next conditional. This conditional now states else if ( summer || weekend )
. If either weekend
or summer
are true, this conditional will print "Your stay might be more expensive than normal!".
If neither conditional is true, we can assume both are false. The program moves to the else
statement, printing "Your stay might be expensive, but it could be worse!".
!
OperatorFinally, there is the bang operator !
. This operator means "not". We can prepend this to any boolean and it will reverse the value of that boolean. For example:
using System;
class HotelCost
{
static void Main()
{
Console.WriteLine("In what season are you booking a stay?");
string season = Console.ReadLine();
Console.WriteLine("On the weekend or a weeknight?");
string dayOfWeek = Console.ReadLine();
bool summer = season == "summer";
bool weekend = dayOfWeek == "weekend";
if (summer && weekend)
{
Console.WriteLine("Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend.");
}
else if (!(summer || weekend))
{
Console.WriteLine("Your stay will likely be the cheapest possible!");
}
else
{
Console.WriteLine("Your stay might be more expensive than normal!");
}
}
}
By adding the bang operator like this (!(summer || weekend))
, we're looking for the opposite of (summer || weekend)
. In other words, neither boolean can be true for this condition to be met. Here, the else
statement would run if one or the other is true, but not both.
||
operator: We can use the or operator (||
) to check if one of the statements are true. For instance, if (summer || weekend)
will evaluate to true if either or both of the statements are true.
!
operator: The bang operator means "not". We can prepend this to any boolean and it will reverse the value of that boolean.
Lesson 3 of 9
Last updated more than 3 months ago.