Oh, I see. Well, there are several ways to do conditional code without using if statements.
The first way is to use the short-circuting feature of boolean logic. As you may be aware, with x && y, first x is tested. If it's false, the whole expression is false, no matter what the value of y, so y isn't even evaluated and the expression is given a value of false. If x is true, then y is evaluated. The expression is given a true value if y is true.
Usually one uses the value of the expression, like this:
Code:
if(x && y) do_something();
But you can use it just like this:
Code:
cout << "the difference is";
difference > 100 && cout << "much ";
cout << "greater than...";
The easier way is to use the conditional operator, which is actually designed for this sort of thing. It's C and C++'s only tertiary operator, which means it takes not one or two but three expressions. ! takes one: if(!x). && takes two: if(x && y). But ?: takes three.
The syntax for the conditional operator is this:
Code:
(test) ? (true-expression) : (false-expression);
First, test is evaluated. If it's true, true-expression is evaluated; otherwise, it's false, and false-expression is evaluated.
Usually the ?: operator is used in place of code like this:
Code:
if(x > 100) y = 100;
else y = x;
where it's actually simpler to go
Code:
y = x > 100 ? 100 : x;
But you can use it like this:
Code:
cout << "difference is " << (difference > 100 ? "much " : "") << "greater than ...";
As you can see, the conditional operator works best when you have both a true-expression and a false-expression. If you don't you might as well use a boolean operator like && or ||.