If else Logic with and

Gamer

New member
Joined
Nov 6, 2021
Messages
1
Programming Experience
Beginner
Hi EverYone, Can you please let me know what i am doing wrong . The code isnt executing
C#:
If((Display .Equals(“Ds”, StringComparison.OrdinalIgnoreCase))
    &&
    ((oType.Equals(“Abc”, StringComparision.OrdinalIgnoreCase))
    ||
    oType.Equals(Def”, StringComparison.OrdinalIgnoreCase))
    &&
    ((Place.Equals(“India, StringComparision.OrdinalIgnoreCase))
    ||
    (Place.Equals(“America”StringComparison.OrdinalIgnoreCase))))
{
MessageBox.show(“Code executed);
}

}
I want if display equals Ds and otype is abc or def and place is India or America then only execute the code..
 
Last edited by a moderator:
Welcome to the forum. In the future, please put your code in code tags to make it easier to read the code on screen.
 
Your grouping of parenthesis looks to be incorrect based on your criteria. The mess of extra comparison options, as well as the unneeded extra parenthesis makes reading what you wrote even harder to read and understand.

Here's how I would tackle the same thing:
Something more readable:
var display = Display.Normalize().ToLower();
var oftype = oType.Normalize().ToLower();
var place = Place.Normalize().ToLower();

if ((display == "ds") &&
    (oftype == "abc" || oftype == "def") &&
    (place == "india" || place == "america")
   )
{
    // do something
}
 
Back
Top Bottom