- Home /
How to make several conditions for an if statement?
I want to say something like
if(x == 1) and (y == 2)
how would I say that?
Answer by FLASHDENMARK · Dec 19, 2011 at 09:57 PM
If you want to say "and":
if(x == z && y == z){
//Do something
}
If you want to say "or":
if(x == z || y == 0){
//Do something
}
You can also do it this way you want both conditions to evaluate for some reason:
if ( x == z & isSomethingTrue() ) {
/* do something */
}
if ( x == z | isSomethingTrue() ) {
/* do something */
}
The other way is more useful and more common of course.
Answer by kevdotbadger · Dec 19, 2011 at 10:02 PM
A decent article on Microsofts site
Mod Edit: Took a stab at re-sourcing the link. Searched ms173145 and got Operators
Answer by markl · Dec 19, 2011 at 10:53 PM
I know that this wasn't the exact question but let's get a bit fancy - if you want to test your variables against multiple condition values say to see if x is 1,2 or 3 and y is 4,5 or 6 then you can do this just to give a bit of knowledge on how to use Generics and Linq which are fantastic :)
var x = 1;
var y = 4;
if ((new List<int>() {1,2,3}).Count(i => x == i) > 0 &&
(new List<int>() {4,5,6}).Count(i => y == i) > 0)
{
/* do something */
}
In terms of performance you should try to use built-in arrays with fixed lengths as far as possible. Very interesting stuff though, keep it up. :-)
$$anonymous$$y point was very simple and you missed it:
It's better to write code that is easy to read.
Readibility makes code easier to maintain (this is not a secret).
Generics and Linq are great.
I have been using generics constantly for years.
That doesn't mean I would use them to deter$$anonymous$$e if y equals 4.
It is very common for new programmers to think it's cool to write fancy looking code. Some people also like to do as many things as possible in one line. In my opinion these are horrible habits.
Who cares how many lines it takes to write something?
The important number is how many seconds it takes for somebody else to understand it.
$$anonymous$$eep in $$anonymous$$d, all this comes from somebody who has spent their career reading other peoples' code.
@markl: Engaging in the "my epeen is bigger than yours" argument technique combined with the "I'm so much smarter than you are, you just can't understand my awesome code" technique pretty much killed your credibility, I'm afraid. Read what I said again about being clever for the sake of being clever. Also I'm not aware of anyone here "taking offense" at anything. Although I will say that writing "you should learn Generics and stop using arrays" is absurd.
@markl: Who said anything about not using generics? I use them plenty. It would, however, be stupid for me to stop using arrays just because I know generics.
Answer by markl · Dec 20, 2011 at 12:42 AM
You can use the following but I wanted to give an example of lambda expressions :)
if ((new List() {1,2,3}).Contains(x) && (new List() {4,5,6}).Contains(y)) { / do something / }
Answer by jjohn8521 · Apr 24, 2021 at 12:27 AM
One thing I'm confused about is when you can or can't use && because I get errors sometimes when trying to use && with player input.
Show an example of when you get errors, you can always use &&... && is basically saying "and", || is basically saying "or".
Your answer
Follow this Question
Related Questions
if statement help! 2 Answers
If \ Else how does the program reads it? 2 Answers
|| conditional statement not working 1 Answer
How to make an if statement with two conditions? 1 Answer
String Scanning Switch Statement 1 Answer