What the and operator does
Sometimes one comparison is not enough to make a decision. You may want to go to the beach only if it is sunny and hot. The logical and operator, written &&, joins two conditions into one. The whole expression is true only when both sides are true.
| Left | Right | Left && Right |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
A simple example is 10 == 10 && 7 < 10. The left side is true and so is the right side, so the entire expression is true.
Using it in an if statement
int x = 10;
if (x != 7 && 12 < x) {
Console.WriteLine("This is true!");
}
Substituting the value of x gives 10 != 7 && 12 < 10. The first part is true, but 12 is not less than 10, so the second part is false. One false is enough to make the whole thing false, and the program prints nothing.
Short-circuit evaluation
C# evaluates && from left to right and stops as soon as the answer is known. If the left side is false, the result must be false, so the right side is never evaluated. This is useful for guarding a risky check:
if (divisor != 0 && total / divisor > 2) {
Console.WriteLine("big ratio");
}
When divisor is zero, the first part is false and the division never runs, so there is no DivideByZeroException. The order of the two sides matters.
C# specifics
- Use two ampersands. A single
&exists too, but it is a different operator that always evaluates both sides. For conditions,&&is the one you want. - Each side must be a complete
boolexpression.x != 7 && 12 < xis fine, butx > 5 && < 10is not. - Strings compare with
==by their text:weather == "sunny"is true only if the characters match exactly, including case.
The exercise, step by step
The lesson gives you two variables: string weather = "rainy"; and int temperature = 90;. It is hot, but it is raining.
- Write the combined condition. You need both checks to hold:
weather == "sunny"andtemperature > 85. Join them with&&. - Print the beach message inside the
if.Console.WriteLine("Going to the beach");runs only when both checks pass. - Add an
elsebranch. It prints"Learning to code"when either check, or both, fail.
string weather = "rainy";
int temperature = 90;
if (weather == "sunny" && temperature > 85) {
Console.WriteLine("Going to the beach");
} else {
Console.WriteLine("Learning to code");
}
Here weather == "sunny" is false because the weather is rainy, so the whole condition is false and the program prints Learning to code. The temperature test is never even evaluated, thanks to short-circuiting. Change weather to "sunny" and the program prints Going to the beach.
Three conditions and more
You can chain as many && operators as you like: a && b && c is true only if all three are true. C# works through them from left to right and stops at the first false. If you also use other operators, remember that the comparison operators are evaluated before &&, so x > 5 && x < 10 needs no parentheses. Add them anyway when they make a longer condition easier to read.
Ranges with and
A very common use of && is a range check. To test that age is from 13 to 19 inclusive, write age >= 13 && age <= 19. Both comparisons must hold, so ages outside the range make one side false. This is the shape you will meet again in later exercises.
Trying all four outcomes
The exercise has four combinations of weather and temperature. Sunny and 90 prints Going to the beach. Sunny and 70 prints Learning to code, as does rainy and 90, and rainy and 70. Only one of the four combinations satisfies both checks, and that is exactly what the truth table above says.
Common mistakes
- Writing
andinstead of&&. The wordandis Python. In C# it is a syntax error. - Repeating only half the condition.
weather == "sunny" && temperatureis not valid; each side needs its own comparison. - Misspelling the string.
"Sunny"and"sunny"are different texts, so the comparison would be false. - Using a single
=. Comparisons use==.
Key takeaways
a && bis true only when bothaandbare true.- It stops early when the left side is false.
- Each side of
&&must be a completeboolexpression. - Combine it with
elseto cover all other cases.