Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Posly · Dec 19, 2011 at 09:52 PM · ifif statementstatement

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

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
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Dec 19, 2011 at 10:00 PM 0
Share

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.

avatar image Posly · Dec 19, 2011 at 10:12 PM 0
Share

Thanks a bunch!

avatar image
2

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Dec 19, 2011 at 10:26 PM 0
Share

Thanks! I didn't know about ?? and ?:

avatar image watercolorheartdev · Mar 10, 2018 at 05:11 AM 0
Share

The page you requested cannot be found.

avatar image
0

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 */
 }
Comment
Add comment · Show 43 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jahroy · Dec 19, 2011 at 11:55 PM 2
Share

That's some of the least readable code I've ever seen.

avatar image save · Dec 20, 2011 at 12:14 AM 1
Share

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. :-)

avatar image jahroy · Dec 20, 2011 at 12:30 AM 4
Share

$$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.

avatar image Eric5h5 · Dec 20, 2011 at 01:22 AM 1
Share

@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.

avatar image Eric5h5 · Dec 20, 2011 at 01:54 AM 1
Share

@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.

Show more comments
avatar image
-1

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 / }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Smurfj3 · Apr 24, 2021 at 12:55 AM 0
Share

Show an example of when you get errors, you can always use &&... && is basically saying "and", || is basically saying "or".

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges