# How can i make multiple loops?

**URL:** <https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235>\
**Category:** Java\
**Created:** [July 20, 2021, 7:28am UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235 "2021-07-20T07:28:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sarah](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Sarah](https://forum.codewithmosh.com/u/Sarah)\
**Post date:** [July 20, 2021, 7:28am UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/1 "2021-07-20T07:28:48Z")

</div>

I am facing a problem with java array loops well, this is the problem

Use the space below to write main method that reads 5 integer values into an array named list. Then, use the array to output the positive values followed by the negative values and ignore zeros. Sample Output Enter 5 numbers: 10 -3 0 -4 9 List: 10 9 -4

and this is the answer:

```auto
int [] list = new int[5];
// 1pt
Scanner read = new Scanner (System.in);

// 3pts
System.out.println("Enter 5 numbers:");
for(int i=0; i<list.length;i++)
 list[i] = read.nextInt();

// 3 pts
for(int i=0; i<list.length; i++)
 if(list[i] > 0)
 System.out.println(list[i]);

// 3 pts
for(int i=0; i<list.length; i++)
 if(list[i] < 0)
 System.out.println(list[i]);

```

but i do not know how to make them work together if you get me

---

<div class="post-metadata">

**Author:** ![Gintsm](https://avatars.discourse-cdn.com/v4/letter/g/f05b48/32.png) [@Gintsm](https://forum.codewithmosh.com/u/Gintsm)\
**Post date:** [July 20, 2021, 6:09pm UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/2 "2021-07-20T18:09:13Z")

</div>

I guess You should use continue to exclude 0. And than You don’t need 3 loops. To make a loop in loop You should use brackets.

---

<div class="post-metadata">

**Author:** ![sufian.babri](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/sufian.babri/32/1028_2.png) [@sufian.babri](https://forum.codewithmosh.com/u/sufian.babri)\
**Post date:** [July 20, 2021, 9:59pm UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/3 "2021-07-20T21:59:20Z")

</div>

I think you can use a single for-loop and maintain the list of numbers in a string.

```auto
String positiveNumbers = "";
String negativeNumbers = "";
for (int i = 0; i < 5; i++) {
  // here you will spend numbers to the relevant
  // variables
} 
// here you will print the values of the strings.

```

One of my teachers used to say that if you can solve a certain problem yourself, you can make a computer do it too; you just need to break down your approach into smaller steps.

---

<div class="post-metadata">

**Author:** ![Gintsm](https://avatars.discourse-cdn.com/v4/letter/g/f05b48/32.png) [@Gintsm](https://forum.codewithmosh.com/u/Gintsm)\
**Post date:** [July 21, 2021, 5:47am UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/4 "2021-07-21T05:47:52Z")

</div>

Here what i was thinking yesterday, also solution

```auto
for (int i = 0; i < list.length; i++) {
                if (list[i] == 0) {
                    continue;
                }
                for (int j = i + 1; j < list.length; j++){
                    int temp;

                    if (list[i] < list[j]){
                        temp = list[i];
                        list[i] = list[j];
                        list[j] = temp;
                    }
                }
                System.out.println(list[i]);

```

---

<div class="post-metadata">

**Author:** ![wcroteau](https://avatars.discourse-cdn.com/v4/letter/w/e36b37/32.png) [@wcroteau](https://forum.codewithmosh.com/u/wcroteau)\
**Post date:** [July 26, 2021, 1:35pm UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/5 "2021-07-26T13:35:50Z")

</div>

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:

```auto
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

```auto
if (a > b) 
   do something

```

Then later they realize that they need to add a statement:

```auto
if (a > b)
   do something
   do something else

```

and BOOM because now they need brackets:

```auto
if (a > b)
{
   do something
   do something else
}

```

So if you always start with:

```auto
if (a > b)
{
   do something
}

```

you’ll never make that mistake

---

<div class="post-metadata">

**Author:** ![ashima08](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@ashima08](https://forum.codewithmosh.com/u/ashima08)\
**Post date:** [July 26, 2021, 1:53pm UTC](https://forum.codewithmosh.com/t/how-can-i-make-multiple-loops/6235/6 "2021-07-26T13:53:53Z")

</div>

```auto
class Main {
  public static void main(String[] args) {

    int weeks = 3;
    int days = 7;

    // outer loop prints weeks
    for (int i = 1; i <= weeks; ++i) {
      System.out.println("Week: " + i);

      // inner loop prints days
      for (int j = 1; j <= days; ++j) {
        System.out.println(" Day: " + j);
      }
    }
  }
}

```

[Sales Training Programs in India](https://www.yatharthmarketing.com/sales-training-programs.html)
