title: Why is this variable inside of a for loop not being reset to it's initial value?
body: <p>I have this programme code which tries to create something similar to a monte carlo simulation. Essentially the gambler starts with given £ stake and places £1 fair bets until going broke or reaching a goal. </p>
  
  <p>The programme code works as expected however, the problem I have is with the statement "int cash = stake;" I would like to know how the variable "cash" is updated correctly with each iteration. </p>
  
  <p>I am confused as to why it isn't reset back to the initial value of "stake" which is a user input outside of the main for loop. For example, if the goal is 500 and the stake is 100, why doesn't cash always remain 100 and if it does, how will it reach the goal of 500?</p>
  
  <pre><code>public static void monteCarlo() {
      Scanner input = new Scanner(System.in);
      System.out.println("Please enter stake, goal, " + " and number of tries");
      int stake = input.nextInt();
      int goal = input.nextInt();
      int tries = input.nextInt();
      int wins = 0;
      // repeat experiment
      for (int i = 0; i &lt; tries; i++) {
          // do one gambler's ruin experiment
          int cash = stake;
          while (cash &gt; 0 &amp;&amp; cash &lt; goal) {
              // flip coin and update
              if (Math.random() &lt; 0.5) cash++;
              else cash--;
          }
  
          if (cash &gt;= goal) {
              wins++;
          }
      }
  
      System.out.println(wins + " wins of " + tries);
  }
  </code></pre>