> Why is my if statement only running once?

Why is my if statement only running once?

Posted at: 2014-12-18 
The logic is wrong; you're using two sequential loops instead of a single, all-encompassing one.

For example if the initial guess is too high, the program will skip entirely over the first loop, and unless you happen to guess correctly, the program will quit without any message if you guess too low even once, because it'll never go back to the first while loop.

The fact that you think only the first run works is curious, because the program will absolutely exhibit the same flawed behavior during every run.

Running a program like this multiple times cannot produce different result unless something external changes (which it doesn't).

Here's a fixed version: http://pastebin.com/W1S7rAUR

To get the desired output you will have to put it in to loop.

At correct answer or Escape loop should terminate.

The code runs fine, the first run. That being the first run after I boot up the IDE; which is codeRunner by the way. (It does the same thing in JCreator too.) The while loops run perfectly no matter what run. But the if statement only runs the first time. So the loop continues over and over until the right number is guessed, like so:

I'm thinking of a number from 0-40. What is it: 10

Too low, try again: 20

Too low, try again: 35

Too low, try again: 38

Too high, try again: 37

You have guessed the correct number!

That is how it's supposed to be, but it only does it on the first run. On my second run, and every run after I would get this:

I'm thinking of a number from 0-40. What is it: 10

Too low, try again: 20

Too low, try again: 35

Too low, try again: 38

Too high, try again: 37

The "You have guessed the correct number!" will not be printed even though the if statement is true. It realizes it's true and instead just terminates the program without running the if statement.