Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for loop question...

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
US
I am writing a multiplicationa table for the first 10 numbers. I can't keep the program from going over 10. It just keeps going on and on. Here is my code:

Code:
public class MultiplicationTable {

	public static void main(String[] args) {
		int multiply = 1;
		System.out.print("\n\n");

		for (int start = 1; start <= 10; start++) {
			for (int lessStart = 10; start <= lessStart; lessStart = lessStart + 10) {
				System.out.print(&quot;\t&quot; + (start * multiply));
				multiply = multiply + 1;
			}
		System.out.print(&quot;\n&quot;);
		}
	}
}
&quot;and everything under the sun is in tune
but the sun is eclipsed by the moon.&quot; --Pink Floyd: Eclipse


&quot;I'm going to spend eternity
reinstalling Windows.&quot; --Reinstalling Windows: by some British guy
 
Well if you look at the logic of the inner loop you will find that your control variable, lessStart, is being incremented by 10 each time. Furthermore, lessStart is beginning at 10 and the control condition is based on start being <= to lessStart. Simple logic will tell you that if start can never be more than 10 (due to the outer loop) and lessStart begins at 10 and is incremented by 10 each time then the loop will always be infinite.

Go back and rethink your logic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top