Multiple Conditions

Combine conditions with && (and), || (or) and ! (not), and group them with parentheses so they mean what you intend.

Apprentice · C# · Updated

After this lesson you can

  • Combine conditions with &&, || and !

Key takeaways

  • && is true only when both sides are true, || when at least one side is, and ! flips a condition from true to false or back.
  • && is worked out before ||, so put an "or" that belongs together in parentheses, as in (dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday").
  • == compares strings by their exact text, so "Saturday" and "saturday" are not equal.
  • C# stops as soon as it knows the answer, so itemsSold > 0 && ... keeps the division on its right from running when itemsSold is 0.

Sometimes a decision depends on more than one thing. Maybe you only go to the steakhouse on the weekend, and you still need enough money. C# lets you join conditions together with logical operators.

And, or, not

Operator Name true when
&& and both sides are true
\|\| or at least one side is true
! not the condition after it is false
int availableCash = 60;
bool isWeekend = false;

Console.WriteLine(availableCash > 50 && isWeekend);
Console.WriteLine(availableCash > 50 || isWeekend);
Console.WriteLine(!isWeekend);
False
True
True

Comparing text

In C#, == compares two strings by their text, so you can test a string the same way you test a number:

string dayOfTheWeek = "Saturday";
Console.WriteLine(dayOfTheWeek == "Saturday");
Console.WriteLine(dayOfTheWeek == "saturday");
True
False

The comparison is exact: capital letters count.

Grouping with parentheses

Here's the steakhouse rule: enough money and a weekend day. A weekend day is Saturday or Sunday. Put the "or" in parentheses, so it's worked out first, as one condition:

int availableCash = 100;
string dayOfTheWeek = "Wednesday";

if (availableCash > 50 && (dayOfTheWeek == "Saturday" || dayOfTheWeek == "Sunday"))
{
    Console.WriteLine("Let's go to the steakhouse.");
}
else if (availableCash > 20)
{
    Console.WriteLine("Let's get pizza.");
}
else
{
    Console.WriteLine("Let's get tacos.");
}
Let's get pizza.

Without the parentheses, && is worked out before ||, just as * comes before +. The condition would mean "enough money on a Saturday, or any Sunday at all", and on a Sunday you'd be off to the steakhouse with no money.

C# stops as soon as it knows

With &&, if the left side is false, the whole condition must be false, so C# doesn't look at the right side. With ||, a true left side settles it. This is called short-circuit evaluation. It lets you guard a risky check:

int itemsSold = 0;
decimal totalSales = 0;

if (itemsSold > 0 && totalSales / itemsSold > 10)
{
    Console.WriteLine("Big orders today.");
}
else
{
    Console.WriteLine("No big orders yet.");
}
No big orders yet.

Dividing by zero would stop the program with an error. Because itemsSold > 0 is false, the division never happens.

Check yourself

2 questions to try on the lesson page.

So what

&&, || and ! let one if express a real rule with several parts, and parentheses make sure C# reads the rule the way you mean it.

Key terms

Cheat sheets