As a professional software developer with over 25 years of experience let me give you a little bit of advice:
Do NOT overly complicate your code… (Google the KISS principal if you’re not familiar)
@Sarah’s initial answer is by far the best way to go with this simple case. It’s WAY more readable and understandable and therefore WAY easier to maintain. Too many people write code that’s slick and/or complicated at the expense of maintainability.
Write code with efficiency in mind absolutely. But eeking out a single millisecond of performance in this kind of an app is unnecessary and ultimately a waste of time initially and a maintenance nightmare afterwards. Address performance bottlenecks when you KNOW they exist.
Not only that but it introduces errors as well. @Gintsm’s answer is both inefficient and incorrect for example.
The problem is that the inner loop iterates 24 times AND introduces a temp variable with assignment. Furthermore, this is a basic sort (which wasn’t required) which is entirely dependent on the initial input order (so it only coincidentally sorts).
By comparison, @Sarah’s original answer only iterates 10 times (5 per for loop) - which is more efficient then?
AND sorted output was never even required…
Finally to answer @Sarah’s original question which (academically) asks how to combine for loops (if I understand correctly), then @Gintsm’s first answer is correct - you can nest loops by using brackets. eg:
for (int i = 0; i < list.length; i++)
{
for (int j = 0; j < list.length; j++)
{
...
}
}
In fact I would recommend using {}'s for ALL control blocks because it’s another common source of bugs.
Too many times I’ve seen someone write
if (a > b)
do something
Then later they realize that they need to add a statement:
if (a > b)
do something
do something else
and BOOM because now they need brackets:
if (a > b)
{
do something
do something else
}
So if you always start with:
if (a > b)
{
do something
}
you’ll never make that mistake