• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little help on looping  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I need to print out these numbers:

10 70 20 65 30 60 40 55 50

I have two sets of for loops, one for the 10 20 30 40 50 sequence, then another for 70 65 60 55. Since you cant have only one loop since the numbers arent in descending/ascending order.

My problem is, how can you alternate it? Execute the first number of the first loop, then print out the first number on the 2nd loop.

I tried doing nested loop:

public class Prelim07_55

{

public static void main ( String [] args )
{
int i, x;

for (i=0; i<=40 ;)
{
i = i + 10;
System.out.print(" " + i);

for (x=75; x>=60 ;)
{
x = x - 5;
System.out.print(" " + x);
}

}


}

}

But it outputs ALL the numbers on the inner loop. I want the inner loop to only execute one at a time.

Help?
 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kielmaru Welcome to the ranch

You can use the break and continue statements when you want to break out of for loops.

If you put your code inside code tags it becomes much easier to read. Use the code button before posting.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Kielmaru, I hope this helps.

Try this



or this (just to demonstrate the break statement)


 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kielmaru vasquez wrote:
I have two sets of for loops, one for the 10 20 30 40 50 sequence, then another for 70 65 60 55.

where is that values? in array or list? if those values are in array/list, then why cant you loop both the list/array parallelly?
<edit>corrected spelling mistake...</edit>
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is that values? in array or list?


I think his objective is simply to print out these numbers:

10 70 20 65 30 60 40 55 50

One way to achieve this is to use two for loops, one for the 10 20 30 40 50 sequence, and another for the 70 65 60 55 sequence.

No need for array or list.

 
Greenhorn
Posts: 6
Eclipse IDE Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just a little variation to the above issue, of using One loop and with only one counter




Regards,
P Balakrishnan
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ogeh Ikem wrote:
I think his objective is simply to print out these numbers:

10 70 20 65 30 60 40 55 50


If that is the case, one way to do it is with a single System.out.println() statement - no need for loops.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:single System.out.println() statement - no need for loops.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one way to do it is with a single System.out.println() statement - no need for loops


Yes That's another way to do it. Kielmaru might be happy with that. However, I have a sneaking suspicion that he wants to accomplish the objective using one or more for loops. Why for loops? Maybe to demonstrate the use of the break and continue statements.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do the same with a while loop, but a for loop is usually easier to use for arrays.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's another way to do it. Java provides us with alternative ways to achieve the objective of printing out a bunch of numbers.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What about this type solving?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!