IF Statement to Execute multiple Conditions
I am wondering if this is possible in real time programming environment or if there really isn't a way to do it. I want to make an IF statement that triggers on multiple conditions.
ie.
Code:
IF statement
(condition 1 is true)
and (condition 2 is true)
{
// Perform action
}
Is there a way? If not through an "IF" statement, then any suggestions?
Thanks!
Re: IF Statement to Execute multiple Conditions
Hi,
For your querries you can make use of ElseIf condition where you need to specify one single main If condition then just have to describe the ElseIf condition and that may be any number of conditions. The Else If condition logic will be such that if your First IF condition does not satisfy then it will directly print the ending Else condition and if it satisfies then it will go into the inner loop and at the moment condition fails it will print the immediate else condition next to it.
hope this will clears your doubt.
Re: IF Statement to Execute multiple Conditions
Whatever the Khushal has explain in its earlier post is absolutely right like that way you can achieve the single If condition with the Multiple condition criteria.
Here i will explain some IFElse related things which i feel to be clear with you.Each else clause belongs to the nearest preceding if statement. Because of this, it is not possible to have one catch all else clause act as a net for multiple if statements just because it follows a series of them. One final else clause can, however, act as a catch all net as long as each if statement, beginning with the second, is preceded by an else. The final else will then perform as a catch all because all the if statements are now attached to each other by the else statements.
Code:
if (name == "mini") {
alert("your name is mini")
}
else if (name == "harold") {
alert("your name is harold")
}
else if (name == "fred") {
alert("your name is fred")
}
else {
alert("Your name is not mini, harold or fred")
}
Re: IF Statement to Execute multiple Conditions
I would use a switch statement for multiple conditions.
Code:
switch(varname)
{
case "once":
dothis();
break;
case "twice"
dosomethingelse();
break;
default:
dothat();
}