So if I use OR statement like this:
if (option1 == true || option2 == true)
{
Print("would like to print the option that triggered above");
}
How do I figure out which option out of these 2 has been triggered?
Thanks
You can do it using else if condition.
E.g.:
if(option1 && !option2)
{
//Option 1
}else if(!option1 && option2)
{
//option 2
}else if(option1 && option2)
{
//both
}
else
{
//no option
}
I'm sorry but you can't.
But instead you can split the if statement
if (option1 == true)
{
Print("option1 is true");
}
if (option2 == true)
{
Print("option2 is true");
}
or put additional checks inside of the if statement(but it's useless).
if(option1 == true || option2 == true){
if (option1 == true)
{
Print("option1 is true");
}
if (option2 == true)
{
Print("option2 is true");
}
}
also you can use
if(option1){}
instead of
if(option1 == true){}
oh, and you could also use this to check if both are true
if (option1 == true && option2 == true)
{
Print("both options are true");
}
if (option1 == true || option2 == true)
{
if(option1)
Print("Option 1");
else
Print("Option2");
}
You could possibly get in dangerous water, though, as you might end up with a situation where option1 == true and option2 == true.
If you really need to know which one was triggered, I would suggest you branch out. if(option1 == true) { ... } else if(option2 == true) { ... } else { ... }.
You could also do this:
if(option1 == true || option2 == true) {
if(option1 == true) {}
if(option2 == true) {}
}
You'd have to test them both again inside the if statement.
Note the short circuit 'or' statement you have used (||). So if option1 one is true, it wont test the second condition.
if (option1 == true || option2 == true)
{
if (option1 == true)
{
Print("option1");
}
else
{
Print("option2");
}
}
You can not find which statement is trigger in your code, you need 2 statements
if (option1 == true || option2 == true)// statement 1
{
Print( option1 ? "option 1 triggered above" : "option 2 triggered above");// statement 2
}
Related
I was playing a little in Unity on a project and I stumbled upon an issue I can't address. Please keep in mind I am a beginner, and my understanding of Unity is fairly limited.
So the issue is this..
I wanted to test some if statement that went like this:
void Update()
{
if (isRow1Good() || isRow2Good() || isRow3Good() || isRow4Good() || isRow5Good() ||
isRow6Good() || isRow7Good() || isRow8Good() || isRow9Good() || isRow10Good())
{
Debug.Log("LOL");
}
}
The content of the functions is this:
Piece p1 = row1[0].ReturnPiece();
Piece p2 = row1[1].ReturnPiece();
Piece p3 = row1[2].ReturnPiece();
Piece p4 = row1[3].ReturnPiece();
if (p1.isTall && p2.isTall && p3.isTall && p4.isTall)
{
return true;
}
else if (p1.isRed && p2.isRed && p3.isRed && p4.isRed)
{
return true;
}
else if (p1.isHollow && p2.isHollow && p3.isHollow && p4.isHollow)
{
return true;
}
else if (p1.isCylinder && p2.isCylinder && p3.isCylinder && p4.isCylinder)
{
return true;
}
else
{
return false;
}
And the others are the same, just instead of row1[] it's row2[].
If the first function is true, the console logs the "LOL" message, but if the second or the third and so on are true, the value is not getting outputted. I tried changing the functions' places, every time it only cares if the first one is true, and the rest are ignored.
What would you say I am doing wrong? :D
else if (p1.isRed && p2.isRed && p3.isRed && p4.isRed)
{
return true;
}
else if (p1.isHollow && p2.isHollow && p3.isHollow && p4.isHollow)
{
return true;
}
else if (p1.isCylinder && p2.isCylinder && p3.isCylinder && p4.isCylinder)
{
return true;
}
if I'm understanding you correct if all four pieces in a row are red its not returning true. You need to double check if the 'isRed' is set properly. if p1.isRed and p2.isRed and p3.isRed and p4.isRed then it will return true no matter what. Also double check if you meant to put || instead of &&.
I'm new here and I have a problem (I think the solution is simple, but I can't solve this problem alone). I have to throw a few checkbox on userform (that is simple) but when I write something like this:
if(checkBox1.Checked)
{
MessageBox.Show("ok1");
}
else if(checkBox1.Checked && checkBox2.Checked)
{
MessageBox.Show("ok2");
}
else
{
MessageBox.Show("co nie tak");
}
always get "ok1" MsgBox...
Any ideas what I'm doing wrong? Thanks for helping.
The if statement will always go into the first block that is true. So if checkbox1 is checked you will always get "ok1". You can never get into the second block ( "ok2" ) because if it is true, the first check would also be true.
I think you want to switch your checks:
if(checkBox1.Checked && checkBox2.Checked)
{
MessageBox.Show("ok2");
}
else if(checkBox1.Checked)
{
MessageBox.Show("ok1");
}
else
{
MessageBox.Show("co nie tak");
}
You may also be looking to build up your string by adding to it. The += means add to the end of the string.
My example code is just an example, since I don't really know what you are trying to do, but it might give you some ideas.
if (checkBox1.checked )
{
mic.HTMLBody = "1) Example1";
}
if ( checkBox2.checked )
{
mic.HTMLBody += "<br>"""2) Example2";
if ( ComboBox2.Text == "Pan" )
{
mic.HTMLBody += "<br>Pana";
}
}
from the code i see if checkbox1.checked is true then "ok1" should display and the second an third evaluations would never be evavluated. if checkbox1.checked is false then only the third option would be evaluated and the second option should never be evaluated at all. Should be more like:
if (checkBox1.Checked)
{
if (checkBox2.Checked)
{
MessageBox.Show("ok2");
}
else
{
MessageBox.Show("ok1");
}
}
else
{
MessageBox.Show("co nie tak");
}
As it is currently written, it is impossible for it to enter the else if, because whenever the else if condition is true, the if condition is also true.
It goes from up to down, the first that is true is entered and the rest are ignored.
Instead you should switch their place as follows:
if(checkBox1.Checked && checkBox2.Checked)
{
MessageBox.Show("ok2");
}
else if(checkBox1.Checked)
{
MessageBox.Show("ok1");
}
else
{
MessageBox.Show("co nie tak");
}
A little unclear what you really want shown, but if you want it to first show ok1 and then ok2 then you can do this:
if(checkBox1.Checked)
{
MessageBox.Show("ok1");
if(checkBox2.Checked) {
MessageBox.Show("ok2");
//MessageBox.Show("ok1 ok2"); //If you want to show them both at the same time
}
}
else
{
MessageBox.Show("co nie tak");
}
I tried this one on a Console and it might be the logic you need. The inline comments will give you a bit of insight of what is doing what:
check1 = true;
check2 = true;
if (check1)
{
if (check2)
{
// Prints if BOTH check1 and check2 are TRUE
Console.WriteLine("ok2");
}
else
{
// Prints if ONLY check1 is TRUE
Console.WriteLine("ok1");
}
}
else
{
// Prints if BOTH check1 and check2 are FALSE
Console.WriteLine("co nie tak");
}
bool Check = checkBox1.Checked;
bool Check2 = checkBox2.Checked;
if (Check == true && Check2 == true)
{ MessageBox.Show("ok 1 & 2"); }
if (Check == true)
{ MessageBox.Show("ok 1"); }
if (Check2 == true)
{ MessageBox.Show("ok 2 "); }
else
{ MessageBox.Show("Not Checked"); }
I have a form where I would like to check for validation before processing the form. My form has 2 sections so I want to make sure that at least one item from each section is selected when they press submit button, and if they did then go to Dashboard.aspx. Even if I put all the required info when it checks for result1 and result2, I get false. Result1 and Result2 won't get the correct value. Even if the values are True again it passes false.
Here is my code:
protected void btnSumbit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
bool result1 = false;
bool result2 = false;
CheckWireFromValidation(result1);
CheckWireToValidation(result2);
if (result1 == true && result2 == true)
{
Response.Redirect("~/DashBoard.aspx");
}
}
public bool CheckWireFromValidation (bool result1)
{
if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
{
result1 = true;
}
else
{
result1 = false;
ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
}
return result1;
}
public bool CheckWireToValidation(bool result2)
{
if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
{
result2 = true;
}
else
{
ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
}
return result2;
}
You're not using the results of CheckWireToValidation. You're using the false value you allocate initially.
Try this
bool result1 = false;
bool result2 = false;
if (CheckWireFromValidation(result1) && CheckWireToValidation(result2))
{
Response.Redirect("~/DashBoard.aspx");
}
Edit
The behavior you're expecting is that of the out parameter modifier. But please don't write code that way ...
I edited your code to get rid of .. em .. cruft. This should be more readable.
protected void btnSumbit_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
if (CheckWireFromValidation() && CheckWireToValidation())
{
Response.Redirect("~/DashBoard.aspx");
}
}
public bool CheckWireFromValidation ()
{
if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
{
return true;
}
else
{
ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
return false;
}
}
public bool CheckWireToValidation ()
{
if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
{
return true;
}
else
{
ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
return false;
}
}
Since you are passing Result1 and Result2 in as parameters instead assigning them. The results will never be set.
Here's one correct way of doing this
bool result1 = CheckWireFromValidation(result1);
bool result2 = CheckWireToValidation(result2);
if (result1 == true && result2 == true)
{
Response.Redirect("~/DashBoard.aspx");
}
and also a side note. I think we can safely remove the boolean parameter from the CheckWireFromValidation methods. Since the return value doesn't depends on the input variable.
Hope this helps.
Suppose I have this if statement:
foreach (MyModel m in SomeList)
{
if (m.pID != SomeValue && (m.xID != 0 && SomeFunction(m.xID) == false)
{
return false;
}
}
I only want SomeFunction(m.xID) == false to evaluate if m.xID != 0 so if m.xID == 0 then don't evaluate SomeFunction
For the moment, I have this if statement broken down into 2 statements and I'm looking to see how I can combine these into just one while preserving the logic. This is the original:
foreach (MyModel m in SomeList)
{
if (m.xID != 0 && SomeFunction(m.xID) == false)
{
return false;
}
if (m.pID != SomeValue)
{
return false;
}
}
If that is all your loop is doing then I would be inclined to remove the loop entirely.
Also, instead of comparing something to false, just use the ! operator.
if (SomeList.Any(MyModel m=>m.xID != 0 && !SomeFunction(m.xID) || m.pID != SomeValue))
return false;
you are looking for this || Operator (C# Reference)
if ((m.xID != 0 && SomeFunction(m.xID) == false) || (m.pID != SomeValue))
{
return false;
}
Right now I'm trying to make it so I've got 1 character and 3 enemies if my character dies it's supposed to give game over or something but I can't make it to work (if enemies die it works tho I don't know why).
Here is what I'm doing:
bool Exit = false;
bool CharDead = false;
Heroe Heroe1 = p.ElementAt(0);
Enemigo Enemigo1 = l.ElementAt(0);
Enemigo Enemigo2 = l.ElementAt(1);
Enemigo Enemigo3 = l.ElementAt(2);
a.Agregar(comienza);
List<Items> item = new List<Items>();
do
{
if (Heroe1.HP > 0)
AccionesHeroe1(l, p);
if (Enemigo1.HP > 0)
AccionesEnemigo1(l, p);
if (Enemigo2.HP > 0)
AccionesEnemigo2(l, p);
if (Heroe1.HP > 0)
AccionesHeroe1(l, p);
else
CharDead = true;
if (Enemigo3.HP > 0)
AccionesEnemigo3(l, p);
if (Heroe1.HP <= 0)
{
CharDead = true;
}
if (Enemigo1.HP <= 0 && Enemigo2.HP <= 0 && Enemigo3.HP <= 0)
{
Exit = true;
}
} while (Exit == false || CharDead == false);
Your ending condition is:
(Exit == false || CharDead == false);
This will only exit when CharDead AND Exit are both true.
You probably want to rework it to be:
(Exit == false && CharDead == false);
This way, as soon as Exit is not false or CharDead is not false, you'll exit.
I think you want to change your while loop expression to
do
{
} while(!Exit && !CharDead)
change
while (Exit == false || CharDead == false);
to
while (Exit == false && CharDead == false);
This would work too, right?
while(!(Exit || CharDead));