title: Incorrect output due to incorrect declaration?
body: <p>I am executing the code to display Prime numbers from 1 to 100. When I execute the below code, it does not show all numbers. Here is the code</p>
  
  <pre><code>public class PrimeNumberSeries {
  
      public static void main(String[] args) {
           boolean flag = true;
          for(int i=1; i &lt;= 100; i++){
                  for(int j=2; j&lt;= i-1; j++){
                    if(i%j==0){
                      flag = false;
                      break;
                  }
              }
              if(flag == true){
                  System.out.println("The number is prime number. Value = "+ i);
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>I am confused about declaration of the flag variable. The program shows me a correct output if I declare the variable inside the first for loop, that is,           </p>
  
  <pre><code>for(int i=1; i &lt;= 100; i++){
                   boolean flag = true;
                      for(int j=2; j&lt;= i-1; j++){
  </code></pre>
  
  <p>What is the reason behind it?
  If the variable is declared as a Class variable (<code>static boolean flag</code>) above the main method, then also it displays the same output.  </p>