- Home /
How do i make two if statements
how do i make two if statements for one set of actions?
Answer by Proclyon · Nov 18, 2010 at 12:53 PM
It's called nesting.
if (condition1) { if(condition2) { //executes this code when the first condition is true AND the second is true }
//executes this code when the first condition is true
}
Almost the same as:
if(condition1 && condition2)
{
//Whether condition 1 is true or not doesn't matter, it only matters now if both conditions are true
//Making long strands of || && &! |* is called boolean logic
}
Mind you that
if (!false)
{
//this code will be excuted
}
what matters is the RESULT of the condition
And one more thing.
With all due respect, This question is not a question for the U N I T Y answers community, it's a basic all languages question. Please refrain from polluting the Q&A in the future if you can please. There's a whole load of descriptions just what and what not to post and even more double questions. They all try to achieve a single goal. Good Q&A quality. No more no less. I try the same so don't take it personal.
Now that you mentioned it I think you ve got a point there... This qn isn't really for the community...
Good to hear my view backed up on the quality of the Q&A process. Thanks
Answer by denewbie · Nov 18, 2010 at 12:53 PM
Anyway, hers some stuff you can follow:
var a:int = 1;
var b:int = 2;
var c:int =3;
Heres an example of a standard if-else:
if (1 == a)
{
// Do a set of actions
}
else
{
// Do another set of actions
}
Here's an example of multiple if-else statements:
if (1 == a)
{
// Do a set of actions
}
else if (2 -- b)
{
// Do another set of actions
}
else
{
// Do YET another set of actions
}
Here's an example of a nested "if":
if (1 == a)
{
if (2 ==b)
{
// Do a set of actions
}
}
Answer by oliver-jones · Nov 18, 2010 at 02:05 PM
Simple:
Var EyeColour = "Brown"; Var HairColour = "Blonde";
if(EyeColour == "Brown") //Which it does, so its going to return true { if(HairColour == "Pink") // Which it doesn't thus going to return false { print("Hair Colour is Pink"); } else if(HairColour == "Blonde") // Which is true { print("Hair Colour is blonde"); } } else { print("Eye Colour is not Brown"); }
Note: Because eye colour is Brown, it will return value of 'true', so when I ask javascript if EyeColour == "Brown" its going to check to see if it is, and if it is true, it will then ask about the hair colour.
Hair colour does NOT = pink, therefore anything within that if statement will be ignored, thus it will not print "Hair Colour is Pink", however, it will print "hair colour is blonde" simply because that if statement is true.
Another thing to remember is that, if the EyeColour did not equal brown, then none of this if statement will be called and it will go to the else statement and print "Eye Colour is not Brown".
(Colour = Color, I'm British)
--- Edit ---
This is basic JavaScript, I would advise you begin learning JavaScript before you dive into Unity
how do you type those two vertical lines? e.g: if(Condition1 /dubble line / Condition2){} like an "if or if this" statement
Your answer
Follow this Question
Related Questions
What if-statement to choose and how to involve. 2D 0 Answers
Only able to jump when grounded.,Only doing something if something is true 1 Answer
Use a #if statement to check if a script exists in project? 0 Answers
if statement not working 2 Answers
Cant assign global variable to method parameter variable 1 Answer