title: Using nested for loop to check if numbers are prime; and if so, add them to the set -- WITHOUT using a method for isPrime
body: <p>Summary: Attempting to learn alternative ways of doing things. The part of Java that I understand least is how each piece of code communicates with one another. As such, instead of using a method call in the classic isPrime for loop generation, I'm attempting to do so with using nested loops only. However, the closest I have gotten is to have it increment by 2 up to 100 -- it isn't checking for if it's prime. I have provided sample code, but I'm not sure why my code does not work as intended. If possible, please correct my code sample and explain your correction(s).</p>
  
  <p>What I'm trying to do: add all prime numbers 2-->100 into set A (2 has been added before beginning)</p>
  
  <p>Expected: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31...
  Actual: ...5,7,9,11,13,15,17... (+=2 all the way to 99)</p>
  
  <p>Why does this code only count by 2, instead of using the condition I have specified? </p>
  
  <p>My logic is:</p>
  
  <ol>
  <li>prepare everything (create stuff, add 2 to set)</li>
  <li>condition addNumber -- if true, add that to the set, if not, move on to next
  number</li>
  <li>outer loop -- n -- is the number being tested. start at 3,
  increment by 2s (no even numbers will be prime), up to 100</li>
  <li>inner loop -- i -- "i --> n" range is used for checking if prime. if n is
  divisible by i, the number is NOT prime</li>
  <li>if the number was not found to violate n%i==0, add it to the vector</li>
  <li>if it was found to violate the condition, exit inner loop and move onto the next number</li>
  </ol>
  
  <p>I don't understand why break; isn't working as I intend.
  I don't understand why, even if break somehow won't work, me specifying the termination condition doesn't work.
  I don't understand why it seems like my check is never being reached and it's just adding every 2 numbers...</p>
  
  <p>Things I've tried: played around with break and terminations. Moved things inside and outside of portions of the nested loops. </p>
  
  <p>All I can get to have happen is for it to count by 2, or for it to add up to 7 and then stop at the first value it shouldn't add (9)</p>
  
  <p>Research I've done: </p>
  
  <p><a href="https://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java">Breaking out of nested loops in Java</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5811151/why-do-we-check-upto-the-square-root-of-a-prime-number-to-determine-if-it-is-pri">Why do we check up to the square root of a prime number to determine if it is prime?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20435289/prime-number-generator-logic">Prime Number Generator Logic</a></p>
  
  <p>The ideal answer would be sample code with a correction of my code and an explanation. I am trying to do this WITHOUT using an isPrime method, the logic for that method must be inside the loop that adds the number. </p>
  
  <pre><code>public static void main(String[] args) {
       boolean addNumber = true; 
       for (int n = 3; n &lt; 100; n = n+2) { //outer loop -- argument
           for(int i=2; i&lt; n; i++){ //check if 2--&gt;n is a divisor
               if (n % i == 0){ //if it's NOT prime 
                   addNumber = false; //set condition to FALSE
                   i=n+1; //ensure that inner loop break condition is met
                   break; //literally tell it to break
               }
               else {
                   i=n+1;
                   break; //if the above is not met, ensure that the inner loop is broken
               }
  
           }//closes inner loop
  
           //before exiting loop, add the confirmed prime number to set
           if(addNumber) //if we should add it
               A.append(n); //add it 
       }//closes outer loop
  
       System.out.println(A);
  }
  </code></pre>
  
  <p>EDIT: It turns out using "break;" here is only a matter of <strong>efficiency</strong>. As soon as we determine that n is not a prime number, why continue running through all the remaining i values? I've continued playing around with this topic, and have come up with a scenario in which using break is actually necessary. </p>
  
  <p>Consider: instead of wanting to add prime numbers between 2 and 100 to the set, let’s say we want to add the first 25 prime numbers to the set. </p>
  
  <p>We need to change the outer loop to something “safe” (not exactly proper, but just for this example) let’s say n = 1000. This way, we know that we won’t run out of prime numbers to be added; there are definitely 25 prime numbers between 2 and 1000.</p>
  
  <pre><code>for (int n = 3; n &lt; 1000; n++) {
    addNumber = true;
    ....
  </code></pre>
  
  <p>NOW we actually need to use break! Before terminating the outer loop in the correct code sample below (by @Nani2015), we need to add: </p>
  
  <pre><code>if(A.size() == 25 )
    break; //break out of outer loop -- we have the desired number of elements
  </code></pre>
  
  <p>This checks whether or not the set we are working with, A, has the amount of elements we desire. If FALSE: do nothing. If TRUE: break (we are done)</p>
  
  <p>Hopefully this clarifies things. In my original question, using break was <strong>entirely unnecessary</strong>. All I needed to do was remove <em>both</em> breaks, and my code would have worked as intended. However, inputting breaks in incorrect areas caused issues. "break;" should be used when you wish to or need to terminate the loop <em>prior to</em> its termination condition being met.</p>