• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A little help on looping

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about this type solving?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic