Thread: Ways to Write IF Statements...

1. Ways to Write IF Statements...

Just like the topic says, I was wondering if there are any different ways to write IF statements besides the following:

if(x > 0)
{
cout << "X is Greater than 0";
}
else
{
cout << "X is Less than 0";
}

OR...

cout << (x > 0 ? "X is Greater than 0" : "X is Less than 0");

OR...

x > 10 ? cout << "It is Greater than 0" : cout << "It is Below 10";

Thanks for the Help!!

Mike

2. Not as far as I know. This is it.

You do have a problem in your example:

if x > 0 then you print "x is bigger than 0"

else you print "x is smaller than 0"

But in the case of your else x could be either smaller of equal to 0.

3. else if, switch, etc.

i'm not sure if this is what you're looking for but this is another way:

switch(x<0)
{
case true:
cout << "X is negative";
case false:
cout << "X is positive";
}

or if you wanted to test for zero separately you could do

if(x < 0)
cout << "X is negative";
else if(x ==0)
cout << "X is zero";
else
cout << "X is positive";