• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't figure out the error in the for loop.  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am trying to print out the range of odd numbers between the 2 integers entered using the JOptionPane dialog. I have narrowed down the issue to the for loop, but I can't spot the error. It is able to compile, but no result occurs.
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your for loop continuation condition (not sure if that's what it is called - the middle one) looks wrong.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (b=num1a;b>=num2a;b++) {
says



Do you see your error now?

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Red's right. Remember the loop will continue while that condition is true, not until it is true. That for loop will either run zero times (if num1a < num2a), or forever (otherwise).
 
Angus Comber
Ranch Hand
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:

I am trying to print out the range of odd numbers between the 2 integers entered using the JOptionPane dialog. I have narrowed down the issue to the for loop, but I can't spot the error. It is able to compile, but no result occurs.


What would happen if num1 was less than num2? Think about the condition of your for loop.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: That for loop will either run zero times (if num1a < num2a), or forever (otherwise).


Not quite...

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Matthew Brown wrote: That for loop will either run zero times (if num1a < num2a), or forever (otherwise).


Not quite...

OK, fair enough . It seems unlikely that it was supposed to rely on integer overflow to end it, though.
 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the replies =) .

What an elementary mistake I have made.. Guess loops still ain't my thing yet =/ .
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WeiJie Lim wrote:Thanks for all the replies =) .

What an elementary mistake I have made.. Guess loops still ain't my thing yet =/ .


Everyone makes those mistakes. And we all end up using a debugger or System.out.println() in our code to see clearer what is happening. For instance, if you put "System.out.println("looping..."); inside the loop it will show if the loop is being executed, or if it is looping a finite amount of times, or if it is looping forever.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!