# Thread: Problems in the Code with if command.

1. here the 2nd if that is,if(prime) is not part of the for loop.
Code:
```#include<stdio.h>

int main()

{
int primes[4];
int counter=1;
int start=3;
int prime;
int i;

primes[0]=2;
printf("2");

while(counter<4)
{
prime=1;

for(i=0; i<counter&&amp;prime; i++)

{
if((!start%primes[i]))

prime=0;   // here is my problem

if(prime)      // How if works ???? prime = 0

{

printf("\n%d",start);

primes[counter++]=start;

}

start++;

}
}

printf("\n");

return 0;

}```
correct one

2. > for(i=0; i<counter&&amp;amp;prime; i++)
Do you even bother to test, or are you just copy/pasting dodgy HTML?

3. Originally Posted by Salem
> for(i=0; i<counter&&amp;amp;prime; i++)
Do you even bother to test, or are you just copy/pasting dodgy HTML?
I Have given an analysis of this algorith and an addition second code and you still really believe that I am copying something to paste here?

Anyway , thnak you guys. I will find the solution alone.
I just asked if anyone knows the flow.. The flow is my problem , the particular flow and not
to read general articles from Wikipedia .
Thank you.

4. Because , I didn't understand the first code... I tried to make a more simple code with if's and while... (the first code that I have posted here is from BOOK it is not mine ) .

Code:
```while(x<3)
{
y=1;
for(; i<2; i++)
if(x)
y=0;   //
printf("\n%d",y);
if(y)
{
printf("\nHello FOlks");
printf("\nHow are you");
}
x++;
}```
Because I dont understand the flow of the original algorithmo (which originates from Book and not from me) I am attempting to create a more simple code to understand and I am using a printf to show me the values... first time y->0 but second y->1... And I dont understand why.
I tried to explain that is my original problem with the code.... (that I didn't understand why second if works ) only this.

5. > Basically I can't understand the flow of algorithm ,
Well it is pretty tough, when your indentation is poor, and you're lazy with the use of braces.

For example, this should have 3 separate indent levels, not zero.
Code:
```	for(; i<2; i++)
if(x)
y=0;   //```

This is your original code, indented.
Code:
```#include<stdio.h>

int main()
{
int primes[4];
int counter = 1;
int start = 3;
int prime;
int i;
primes[0] = 2;
printf("2");
while (counter < 4) {
prime = 1;
for (i = 0; i < counter && prime; i++)
if (!(start % primes[i]))
prime = 0;              // here is my problem
if (prime)                  // How if works ???? prime = 0
{
printf("\n%d", start);
primes[counter++] = start;
}
start++;
}
printf("\n");
return 0;
}```
Notice that I changed the position of the not operator if (!(start % primes[i])). Again, this would have been so much clearer expressed as if ( start % primes[i] == 0 ).

Now this is your code, with braces in all the places you left them out.
This is I guess what vikasvijayan tried to do, but made a mess of it.
Code:
```#include<stdio.h>

int main()
{
int primes[4];
int counter = 1;
int start = 3;
int prime;
int i;
primes[0] = 2;
printf("2");
while (counter < 4)
{
prime = 1;
for (i = 0; i < counter && prime; i++)
{
if (!(start % primes[i]))
{
prime = 0;              // here is my problem
}
}
if (prime)                  // How if works ???? prime = 0
{
printf("\n%d", start);
primes[counter++] = start;
}
start++;
}
printf("\n");
return 0;
}```
> // How if works ???? prime = 0
if ( prime ) is merely short-hand for if ( prime != 0 )

6. Ok Salem thank you.

I just looked the second code (not the first)
and I am trying to apply some test values cases in
x , y variables In order to understand the flow of the code.

The brackets has major sense! Anyway.... may I have done wrong to post originally the first code without have the sure knowledge about the if flow... the while flow... etc.... understand me????

Sorry I am trying to improve my english simultaneously.
I have some queries about the second code...

7. Originally Posted by quzah
I am sorry but you were absolutely right .
I read Sieve of Eratosthenes yesterday and I understand some basic concepts.

Thank you