title: Variables in a do while loop
body: <p>Below is some code asking the user to input an integer.</p>
  
  <pre><code>public int getValidInput() {
      Scanner user_input = new Scanner(System.in);
  
      do {
          System.out.println("Enter an integer &gt;=1 and &lt;=10: ");
          int number = user_input.nextInt();
  
      } while (number &gt; 10 || number &lt; 0);
  
      return number;
  }
  </code></pre>
  
  <p>The code as shown does not work although when I initialise number outside the do command i.e. set it to int number; and then in the do loop set <code>number = user_input.nextInt();</code> it does. Why does it work in one case and not the other?</p>