Why does my SECOND for loop (in RED) work as written, but fail when I change the values of the variables? Here's the simple program (to determine prime numbers):
The issue is this: If the SECOND for loop is written asCode:#include <stdio.h>
#include<stdbool.h>
int main (int argc, const char * argv[]) {
int p, d;
_Bool isPrime;
for ( p = 2; p <= 50; ++p ) {
isPrime = 1;
for ( d=2; d<p; ++d)
if (p % d == 0)
isPrime = 0;
if (isPrime)
printf("%i is a prime number.\n",p);
}
return 0;}
the program exits without printing anything.Code:for ( d=1; d<50; ++d)
But this for loop works perfectly:
So my questions are:Code:for ( d=2; d<p; ++d)
1) Why can't looping begin with d = 1 ? Why does it need to begin with d = 2 ?
2) And why can't the limiting expression be d<50 (or some other number)? Why must it be p ?
I'm a noob, so the issue isn't obvious. I'm posting here after hours of googling turned up nothing.