Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how do you exit a loop in java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kims2




PostPosted: Fri Jun 26, 2009 8:17 pm   Post subject: how do you exit a loop in java?

I need to write a program that asks the user for a starting number and an ending number and the program outputs all the numbers inbetween:

example: start value 5, end value 10 = 5 6 7 8 9 10

I've done everything but I do not know how you exit a loop in java. So here's the small problem I have: everytime I enter a higher starting value than the end value. The program will do 5 4 3 2 1 0 1, how do I make it stop at the end value?

This is part of what I have:

code:
        int count = (numberA);
            while (count >= numberB)
                {
                c.println(count);
                count = count - 1;
                }
            while (count <= numberB)
                {
                c.println(count);
                count = count + 1;   
                }


thanks for your help!
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Fri Jun 26, 2009 9:35 pm   Post subject: Re: how do you exit a loop in java?

It's actually your logic really.
numA = 5
numB = 0

code:

int count = (numberA); // 5
while (count >= numberB) // 5>=0 true,  ... 0>=0 true, -1>=0 false ----> Exit loop.
{
   c.println(count); // print 5, ... 0
   count = count - 1; // 4, ... -1
}

// count = -1

while (count <= numberB) // -1<=0 true, 0<=0 true, 1<=0 false ----> Exit loop.
 {
  c.println(count); // -1, 0
  count = count + 1;  // 0, 1
}


Truthfully, the code SHOULD be outputting:
5 4 3 2 1 0 -1 0

The thing isn't that you should break a loop INSIDE of it, but rather that you never enter the loop in the first place. You kinda need to do one loop or the other. Which calls for a simple selection statement (If/Else).

EDIT:
Er, I guess your example is from 5..1. Didn't realize before.
As for exit a loop beforehand, I think it's break; but I can't be sure. Though your exit conditions are while ( here ) anyways.
rdrake




PostPosted: Sat Jun 27, 2009 12:18 am   Post subject: RE:how do you exit a loop in java?

If you want to "break" out of a loop and exit it, use break. If you want to skip an iteration of the loop use continue.

Java:
for (int num : new int[] { 1, 2, 3, 4, 5 }) {
    if (num == 4) break;
    System.out.println(num);
}
Should print 1 2 3.

Whereas

Java:
for (int num : new int[] { 1, 2, 3, 4, 5 }) {
    if (num == 4) continue;
    System.out.println(num);
}
Should print 1 2 3 5.

Edit: Gandalf demanded working code.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: