Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: unreachable statement Again?

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default unreachable statement Again?

        public static void main(String[] args) {
     
            long targetNumber,
                 guessNumber;
     
            targetNumber = 34;
            System.out.println("Target Number Is:  " + targetNumber);
     
            do {
     
               guessNumber = (int) (Math.ceil(Math.random() * 100));
               System.out.println("Guess Number Is:  " + guessNumber);
            }
            while (true);
     
            if (guessNumber == targetNumber) {   // my compiler is displaying this part as "Unreachable statement"
     
                System.out.println("\n");
                System.out.println("Match Number Is:  " + guessNumber);
                System.exit(0);
            }
        }
    }

     if (guessNumber == targetNumber) {   // my compiler is displaying this part as "Unreachable statement"
    why?


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: unreachable statement Again?

    When do you think your code will get past this do while loop?

           do {
     
               guessNumber = (int) (Math.ceil(Math.random() * 100));
               System.out.println("Guess Number Is:  " + guessNumber);
            }
            while (true);

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: unreachable statement Again?

    Quote Originally Posted by chronoz13 View Post
        public static void main(String[] args) {
     
            long targetNumber,
                 guessNumber;
     
            targetNumber = 34;
            System.out.println("Target Number Is:  " + targetNumber);
     
            do {
     
               guessNumber = (int) (Math.ceil(Math.random() * 100));
               System.out.println("Guess Number Is:  " + guessNumber);
            }
            while (true);
     
            if (guessNumber == targetNumber) {   // my compiler is displaying this part as "Unreachable statement"
     
                System.out.println("\n");
                System.out.println("Match Number Is:  " + guessNumber);
                System.exit(0);
            }
        }
    }

     if (guessNumber == targetNumber) {   // my compiler is displaying this part as "Unreachable statement"
    why?

    it is a infinite loop. control never come out of do while loop upto stack overflow

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: unreachable statement Again?

    ahh ok ok.. its an infinite loop.The program will only read and stay at the loop's body..
    and it wont have any chance to execute or to reach the following statements after it because it will loop forever...

Similar Threads

  1. How to Use the Java switch statement
    By JavaPF in forum Java Programming Tutorials
    Replies: 6
    Last Post: April 18th, 2013, 05:19 PM
  2. unreachable statement?
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: September 11th, 2009, 10:06 AM
  3. If statement
    By Dave in forum Loops & Control Statements
    Replies: 2
    Last Post: August 27th, 2009, 10:11 AM
  4. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM
  5. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 05:13 AM