Start learning free

LESSON 73 OF 347 · CONDITIONAL STATEMENTS

The And Operator in C#: Combining Conditions with &&

Lesson Read the full explanation

Sometimes you will need to test more than one thing at a time. The logical and operator (&&) returns true if and only if both conditions to the left and right of it are true. For example:

10 == 10 && 7 < 10 // Evaluates to true

We have two conditions separated by && operator:

  • 10 == 10, which evaluates to true
  • 7 < 10, which evaluates to true
As both conditions evaluate to true, the entire line will evaluate to true

An example inside an if statement:

int x = 10;
if(x != 7 && 12 < x) { // Evaluates to false
    Console.WriteLine("This is true!");
}

Inside the if, we have two conditions separated by && operator:

  • x != 7, equivalent to 10 != 7, which evaluates to true
  • 12 < x, equivalent to 12 < 10, which evaluates to false
As one of the conditions evaluates to false, the entire statement will evaluate to false, and so the program prints nothing.

Assignment
Follow the Coding Tutorial and play with the and operator.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    And Operator in C#, explained

    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.

    LeftRightLeft && Right
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsefalse

    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 bool expression. x != 7 && 12 < x is fine, but x > 5 && < 10 is 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.

    1. Write the combined condition. You need both checks to hold: weather == "sunny" and temperature > 85. Join them with &&.
    2. Print the beach message inside the if. Console.WriteLine("Going to the beach"); runs only when both checks pass.
    3. Add an else branch. 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 and instead of &&. The word and is Python. In C# it is a syntax error.
    • Repeating only half the condition. weather == "sunny" && temperature is 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 && b is true only when both a and b are true.
    • It stops early when the left side is false.
    • Each side of && must be a complete bool expression.
    • Combine it with else to cover all other cases.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free