Using an If statement to find prime numbers
<p>The following is a piece of code that shows an output stating whether a number, entered by a user, is a prime number or not.</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int a,b;
int main(void) {

 printf("Enter number: ");
 fflush(stdout);
 scanf("%d",&amp;a);

 for (b = 2; b &lt; a; b++)
 {
   if (a % b == 0)
    break;
 }

 if (b&lt;a)
 {
   printf ("%d is divisible by %d\n", a, b);
 }

 else
 {
   printf ("%d is prime \n", a);
 }
return 0;
}
</code></pre>

<p>The piece of code, written above, is not mine and it identifies a prime number successfully everytime (i.e the <strong>printf</strong> statement of the <strong>else</strong> clause gets printed).</p>

<p>My understanding of the <strong>if</strong> statement is that an <strong>else</strong> clause in an <strong>if-else</strong> statement belongs to the nearest <strong>if</strong> statement which doesn't already have an <strong>else</strong> clause. And so, having said that, I believe the <strong>else</strong> clause, in the above piece of code, belongs to the nearest <strong>if</strong> statement.</p>

<p>My question is this: If the user enters a prime number like 31 or 37 or any other prime number, how does the <strong>printf</strong> statement of the <strong>else</strong> clause get printed?
The condition <code>if (b&lt;a)</code> (of the second <strong>if</strong> statement) will always be true considering that b will only be incremented to <code>(a-1)</code>. And so if the user enters the number 31, the variable <strong>b</strong> will only be incremented to 30. Shouldn't it be the case that the <strong>printf</strong> statement of the second <strong>if</strong> statement gets printed, regardless of whether the number entered by the user is prime or not, considering that the condition <code>if (b&lt;a)</code> will always be true?</p>

<p>How does the above piece of code print all the prime numbers correctly, and therefore, work alright? (when, according to my limited understanding of the way the <strong>if</strong> statement works, it shouldn't)</p>