Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-15-2012, 01:21 AM   PM User | #1
dannyboi
New Coder

 
Join Date: Jul 2012
Location: NYC
Posts: 23
Thanks: 1
Thanked 0 Times in 0 Posts
dannyboi is an unknown quantity at this point
My loop statement keeps looping forever?

public class Main {

public static void main (String [] args) {

int number = 2;
while (true)
{
if (number == 122121)
break ;
{

System.out.println(number + " " );
number +=2;
}
System.out.println();

}
}
}


I don't know why, I included a break in the code?
dannyboi is offline   Reply With Quote
Old 08-15-2012, 01:30 AM   PM User | #2
oneguy
New Coder

 
Join Date: Jul 2012
Location: Ukraine
Posts: 68
Thanks: 1
Thanked 18 Times in 17 Posts
oneguy is an unknown quantity at this point
You first assign 2 to number, then number is incremented only by 2 by the statement number +=2; so number will always be even. But the condition for break is number == 122121; 122121 is odd, so the condition will never be met.
oneguy is offline   Reply With Quote
Reply

Bookmarks

Tags
java, java programming

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:45 AM.


Advertisement
Log in to turn off these ads.