In the last lesson, we covered if
and else
. In this lesson, we'll explore logical operators we can use for more complex branching. We'll create an application that tells a user how expensive a hotel stay is likely to be.
Our application will estimate if someone's hotel stay is likely to be expensive based on the season and day of the week. After all, hotels are usually more expensive during peak travel season (summer) and on weekends.
Let's start by collecting information from the user and saving several boolean variables.
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";
}
}
In addition to collecting user input and storing it in variables, we also define two booleans using the data type bool
:
A summer
boolean will be set to true
if the season
collected from the user is equal to the string "summer"
.
A weekend
boolean will be true
if the dayOfWeek
collected from the user is equal to the string "weekend"
.
&&
Let's return different statements to the user based upon the combined values of these two booleans using the &&
operator:
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
{
Console.WriteLine("Your stay might be expensive, but it could be worse!");
}
}
}
In our if
statement, if (summer && weekend)
, we use the &&
operator. This checks whether each of these statements are true. If so, the entire statement evaluates to true
. If one or both statements evaluate to false
, the entire statement evaluates to false
.
If both the summer
and the weekend
booleans are true
, our program will tell the user, "Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend.". We can test this out by launching our program:
$ dotnet run
Note that user input needs to be lowercase for any of our boolean statements to return true. We'll discuss how to normalize user input later on.
else if
We can do more complex branching with the else if
keyword:
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!");
}
}
}
In C#, we can use as many else if
s as necessary.
In the code above, if both summer
and weekend
are true, the application will state "Your stay is probably going to be pretty expensive. It's both peak travel season AND the weekend." However, if they aren't both true, our program will look at the next condition.
If just summer
is true, the program will print "Your stay might be more expensive than normal!. If it is not true
, the program will move to the next statement.
If just weekend
is true, the program will print "Your stay might be more expensive than normal!". If it isn't true
, the program will move to the next statement.
If none of the previous conditionals are true, the program will reach the else
statement and print "Your stay might be expensive, but it could be worse!".
&&
operator: Used to check if multiple statements are true. For instance, in if (summer && weekend)
, if both statements are true, the whole conditional statement evaluates to true. If one or both statements are false, the whole statement evaluates to false.
else if
: We can use else if
to add more than two conditions.
Lesson 2 of 9
Last updated more than 3 months ago.