Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    NYC
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •