Computer Science Canada how do you exit a loop in java? |
Author: | kims2 [ 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:
thanks for your help! |
Author: | Zren [ 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
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. |
Author: | rdrake [ 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.
Whereas
Edit: Gandalf demanded working code. |