Sort three numbers using only if-statements Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using ifs. Any thoughts on how to improve this?

#include &lt;iostream&gt;

using namespace std;
int main ()
{
    int num1, num2, num3;
    int smallest, middle, biggest;
    cin &gt;&gt; num1 &gt;&gt; num2 &gt;&gt; num3;

    cout &lt;&lt; endl;

    if ((num1 &lt; num2) &amp;&amp; (num1 &lt; num3))
    {
        smallest = num1;
        if (num2 &gt; num3)
        {
            biggest = num2;
            middle = num3;
        }
    }
    if ((num1 &lt; num2) &amp;&amp; (num3 &lt;&lt; num1))
    {
        smallest = num1;
        if (num2 &lt; num3)
        {
            middle = num2;
            biggest = num3;
        }
    }
    if ((num1 &gt; num2) &amp;&amp; (num3 &gt; num1))
    {
        middle = num1;
        if (num2 &lt; num3)
        {
            smallest = num2;
            biggest = num3;
        }
    }
    if ((num1 &lt; num2) &amp;&amp; (num3 &lt; num1))
    {
        middle = num1;
        if (num2 &gt; num3)
        {
            biggest = num2;
            smallest = num3;
        }
    }
    if ((num1 &gt; num2) &amp;&amp; (num1 &gt; num3))
    {
        biggest = num1;
        if (num3 &gt; num2)
        {
            middle = num3;
            smallest = num2;
        }
    }

    if ((num1 &gt; num2) &amp;&amp; (num1 &gt; num3))
    {
        biggest = num1;
        if (num2 &gt; num3)
        {
            middle = num2;
            smallest = num3;
        }
    }

    cout &lt;&lt; smallest &lt;&lt; ", " &lt;&lt; middle &lt;&lt; ", " &lt;&lt; biggest &lt;&lt; endl;


    return 0;
}
I will try to take several things into account but to keep things simple. A few remarks:


First, it seems that you have a typo here:

if ((num1 &lt; num2) &amp;&amp; (num3 &lt;&lt; num1))


I think that you meant num3 &lt; num1 instead of num3 &lt;&lt; num1 in your second condition.
As @Josay says, you better write small functions. I would add that you better separate the input/output operations (that you can keep in main) and the sorting function. In my opinion, this would be a good enough signature for the sorting function:

void sort3(int&amp; a, int&amp; b, int&amp; c);


You would give three variables to it and it would sort them in-place so that you end up with \$ a \le b \le c \$.
Now, let's choose an algorithm. For three values, the easiest is to implement a bubble sort which shouldn't be really slower than other algorithms (when you want to sort more values, it becomes horribly slower though).

void sort3(int&amp; a, int&amp; b, int&amp; c)
{
    if (a &gt; b)
    {
        std::swap(a, b);
    }
    if (b &gt; c)
    {
        std::swap(b, c);
    }
    if (a &gt; b)
    {
        std::swap(a, b);
    }
}


Most sorting algorithms heavily rely on swapping values. The one I just implemented sorts your values with only three comparisons and at most three swaps and is, in my opinion, far simpler to understand than what you had.

Note that there are more efficient algorithms but I deliberately chose to present one that is not that bad while easy to understand.
This is not a problem in your case, but we can't stress out enough that using namespace std; is often considered bad practice. It is the case when used in a header file, especially in a library header file since it will pollute the global namespace of every file including it. That's not a problem for you since you're probably doing everything in a .cpp file but it's better to keep that in mind.
You don't need to return 0; at the end of main. If the compiler reaches the end of the main function without having encountered a return statement, it automagically adds a return 0; for you. Note that it only works with main though. Dropping this line is an interesting way to document that your program cannot return error codes and that it will only ever return 0.