title: Confused with variable scope for this particular program
body: <p>This is a snippet of my code. I already fixed this bug, but I just don't understand why it works now. I'm not sure if I explained this correctly, so please ask any question if necessary. The bug is that the variable <em>puncation</em> changes to the correct value <em>test</em> when the statement is located in the place where I commented, the statement works. But, it does not work when the statement is located in the place where I commented, the statement does not work. </p>
  
  <pre><code>     if (endOfSen) {
                  /////////////////The statement below works when it is here./////////////
                  /////////////////OUTPUT 1 - occurs when the statement is placed here.              
                   String puncation = null ;
                  /////////////////////
                  int orgSize = words.size(); //remember size of stack
                  //only runs if stack is not empty
                  if (!words.empty()) {
                      while (words.size() &gt; 0) {      //until last word
                          String word = words.pop();
                          ///The statement below does not work when it is here///////
                          //////OUTPUT 2- occurs when the statement is placed here.  
                          //String puncation = null ;
  
                          //if last word of sentence
                          if (orgSize == words.size() + 1) {
                              word = word.substring(0, 1).toUpperCase() + word.substring(1);
                              puncation = "test";     // just a test value
                              word = word.replace(puncation, "");
                              //////////////////test to see if works 
                              System.out.println("puncation: " + puncation);
                          }
                          //if first word of sentence
                          if (words.size() == 0) {
                              //////////////////test to see if works 
                              System.out.println("puncation: " + puncation);
                              word = word.toLowerCase();
                              word = word + "" + puncation;
                          }
                          newSen.push(word);
                      }
                  }
                  endOfSen = false;
              }
          }
  </code></pre>
  
  <p>OUTPUT 1 (Second <em>puncation</em> changes from original value) </p>
  
  <pre><code>puncation: test
  puncation: test
  </code></pre>
  
  <p>Output 2 (Second <em>puncation</em> does not change from original value) </p>
  
  <pre><code>puncation: test
  puncation: null
  </code></pre>