promotion image of download ymail app
Promoted
Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

C# if statement?

How do I write a C# IF statement with multiple conditions? I have two conditions that need to be satisfied, but I don't know what the AND character is.

If(a = 0 AND b = 1)

{Do Something Here}

I have tried AND, &, &&, comma and semicolons, but nothing works.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Your code should read as follows:

    if ((a == 0) && (b==1))

    {

    Console.WriteLine("Both tests were true.");

    }

    **Note that the conditions are separated by each being within their own set of parenthesis.

    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    Your should actually read:

    if (a==0 && b==1)

    { do stuff here

    }

    So you need the && along with == to test for equality. A single = is an assignment, which won't return a boolean.

    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    for c# 2005, it's &

    for short-circuit evaluation it's &&

    Maybe there's something else wrong with your code?

    Good Luck!

    Source(s): C# 2005 For Dummies
    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.