Order Numbers using ONLY If Statements

The question I am about to ask is pretty simple. It's part of a drill exercise I'm going through. I need to prompt the user to input 3 Integers, then output them in ascending order. This sounds like a very common question, but I would like to ask if it's possible ( and efficiently so )to code this program entirely using IF STATEMENTS. No loops, no arrays.

Does it require nesting IF STATEMENTS? More than 3 statements? How can you keep it to a reasonable size. In my first attempt, I've been doing impractical comparisons between A and B, B and C. Am I on the right track?

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;

	cout << "Enter Values to Order" << endl;
	cin >> a >> b >> c;
	
	if ( a >= b)
		if( b>= c)
		cout << c << "  " << b << "  " << a << endl;
		else
		cout << b << "  " << c << "  " << a << endl;
	else
		if (a >= c)
		cout << c << "  " << a << "  " << b << endl;
		else
		cout << a << "  " << c << "  " << b << endl;

	return 0;		


I get wrong outputs when I use negative values, 0, and -1 though.
Last edited on
It can be done quite easily. There is something called "loop unrolling". In this case you know exactly how many iterations of the loop there are, you could just write the loop code three times (though adjusted to use your three variables instead of array indices)
Topic archived. No new replies allowed.