Wednesday, April 2, 2014

Use logical operators to check conditions

1) The != operator works a lot like ==, except it’s true if the two things you’re comparing are not equal.

2) You can use > and < to compare numbers and see if one is bigger or smaller than the other.

3) The ==, !=, >, and < operators are called conditional operators. When you use them to test two variables or values, it’s called performing a conditional test.

4) You can combine individual conditional tests into one long test using the && operator for AND and the || operator for OR. So to check if i equals 3 or j is less than 5, do (i == 3) || (j < 5).

5)  E.g private void button2_Click(object sender, EventArgs e)
{
int x = 5;
if (x == 10)
{
MessageBox.Show(“x must be 10”);
}
else
{
MessageBox.Show(“x isn’t 10”);
}
}

private void button3_Click(object sender, EventArgs e)
{
int someValue = 4;
string name = “Bobbo Jr.”;
if ((someValue == 3) && (name == “Joe”))
{
MessageBox.Show(“x is 3 and the name is Joe”);
}
MessageBox.Show(“this line runs no matter what”);
}

No comments:

Post a Comment