# Problems in the Code with if command.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2011
Mr.Lnx
Problems in the Code with if command.
Hello everybody. I have difficulties with a C code

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; }```
I pose/put the value 0 in prime variable and after that I put it into if
how works??? From theory I knew that if command stops to work when find a zero value. And now the prime is 0!!!!!

Thank in advance. :)
• 10-01-2011
quzah
You need to fix your indentation.
Code:

```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++; }```
The orange is part of the for loop. The blue is not. Loop through counter times, as long as prime is not zero. After your loop breaks, see if prime is zero or not.

One thing you should pay attention to is precedence. ! happens before %:
Code:

`if((!start%primes[i]))`
So what you are really doing is:
Code:

` if( 0 % primes[ i ] )`
or
Code:

`if( 1 % primes[ i ] )`
Is that really what you want?

Quzah.
• 10-01-2011
Mr.Lnx
Quzah could I ask you something else first???

Originally I have found this exercise from one book of Unix ok.

If the if gives not zero value (the first if) we don't pose (prime=0) ?

For instance (with pencil and paper)

before while print -> 2 Hence the first output number is 2!

Now.... if the first if gives not Zero result => start / primes[i] => start / primes [0 ] => 3/2 = 1 Hence if (!1) => if(0) => gives 0 Hence we dont pose 0 into prime... and we the code will go on this line to elaborate
=>
Code:

```if(prime)              {                 printf("\n%d",start);                 primes[counter++]=start;          }                                    start++;```
Am I right on this thought ?
• 10-01-2011
quzah
Code:

```start = 3 prime[0] = 2 i = 0 for( i = 0; i < something && prime != 0; i ++ )     if( !3 % prime[ 0 ] )```
Turns into:
Code:

```for( ... )     if( 0 % 2 )```
!3 happens before %2 does, like I said. C Operator Precedence Table

Quzah.
• 10-01-2011
Mr.Lnx
awwww sorry you are right!!!!! The symbol "!"
• 10-01-2011
Mr.Lnx
Finally i dont understand how works this

Code:

``` while(counter<3) {         prime=1;         for(i=0; i<counter&&prime; i++)         if(!(start%primes[i]))                 prime=0;            if(prime)  // second if              {                 printf("\n%d",start);                 primes[counter++]=start;          }                                    start++; }```
My analysis is
===========================================
Originally print the number 2.

AFter that

1st (while loop) =>

prime = 1
for ( i = 0; i< 1 && 1 ; i ++ )
if ( ! ( start % primes[0] ) ) => if ( ! ( 3 % 2 ) ) => if ( !(1)) => if (0) (so , we dont pose 0 into prime variable below)

Hence (in the second if ) => if( 1 )
print => start => print 3
primes[1] = 3;
counter -> 2
start -> 4

2nd (while loop)
prime 1
(now i = 1 into for loop)
so for ( i = 1 ; i< 2 && 1 ; i++)
if ( ! ( start % primes[1] ) ) => if ( ! ( 4 % 3 ) ) => if ( !(1)) => if (0) (so , we dont pose 0 into prime variable below)
Hence (in the second if ) => if( 1 )
print => start => print 4
primes[2] = 4;
counter -> 3
start -> 5
==========================================

But I am wrong because the actual output is
=> 2 3 5
not
=> 2 3 4 :/

What is wrong in my thought???? :/
• 10-02-2011
quzah
You forgot about 4 % 2.

Quzah.
• 10-02-2011
Mr.Lnx
Quote:

Originally Posted by quzah
You forgot about 4 % 2.

Quzah.

4%2 ???

But primes[1]=3 . Isn't it???
For the 2nd pass into while loop
• 10-02-2011
quzah
Quote:

Originally Posted by Mr.Lnx
4%2 ???

But primes[1]=3 . Isn't it???
For the 2nd pass into while loop

Why do you think it would get to the second pass?
Code:

```    for(i=0; i<counter&&prime; i++)     if(!(4%2))         prime=0; ```

Quzah.
• 10-02-2011
Mr.Lnx
Quote:

Originally Posted by quzah
Why do you think it would get to the second pass?
Code:

```    for(i=0; i<counter&&prime; i++)     if(!(4%2))         prime=0; ```

Quzah.

Hmmmm because I have created this code before
Code:

```#include <stdio.h> int main() { int i , x , y ; i=0 , x=0 , y=1 ; while(x<3) {                 for(; i<3; i++)         {         if(i==2)         y=0;          if(y)         printf("\nHello FOlks");         else         printf("\nHa"); }         x++; }                 return 0;         }```
So , while x=0 .....
for i=0
while x=1
for i=1
etc.......
• 10-02-2011
quzah
Quote:

Originally Posted by Mr.Lnx
Hmmmm because I have created this code before
Code:

`...completely unrelated code...`

What's that have to do with anything in this discussion? You asked why the output wasn't what you expected, I showed you what you weren't seeing. Whatever you are posting now has nothing to do with this discussion.

Quzah.
• 10-02-2011
Mr.Lnx
Quote:

Originally Posted by quzah
What's that have to do with anything in this discussion? You asked why the output wasn't what you expected, I showed you what you weren't seeing. Whatever you are posting now has nothing to do with this discussion.

Quzah.

I agree. But the second code it was only for my personal testing before look a more "advanced" example such as the first code that I posted here .
Anyway I dont understand the 4 % 2. Where do you find it?
• 10-02-2011
quzah
First post, line 12. Sixth post, line five. Step through your code.

Quzah.
• 10-02-2011
Mr.Lnx
Quote:

Originally Posted by quzah
First post, line 12. Sixth post, line five. Step through your code.

Quzah.

Basically I can't understand the flow of algorithm , if you have time look my analysis and show me there where is my fault :/ Because I am using paper and pencil to understand the logic . The original example is more complexity than others (for instance the second code) for me because it has while , for and two if's simultaneously :)