# Beginner at C programming And operator and if statement Problem!

This is a discussion on Beginner at C programming And operator and if statement Problem! within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main( ) { int c, h, o, n, charge = 0; float weight; c = 3, ...

1. ## Beginner at C programming And operator and if statement Problem!

Code:
```#include <stdio.h>
int main( ) {
int c, h, o, n, charge = 0;
float weight;
c = 3, h = 7, n = 1, o = 2, weight = 89.0929;
if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929)){printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d\n", c, h, n, o, weight, charge);}
return 0;
}```
i am not sure what the problem is but it won't display the printf. The if statement seem to be correct so for.. when i compile it there is no errors.

2. Try with these fixes,
Note that each off these assignment should each be on separate line
c = 3, h = 7, n = 1, o = 2, weight = 89.0929;

Code:
```#include <stdio.h>
int main( ) {
int c=3, h=7, o=2, n=1, charge = 0;

float weight;

weight = 89.0929;

if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929))
{
printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d\n", c, h, n, o, weight, charge);
}

return 0;

}```

3. Floating Point
All floats are approximations, so it's unwise to try to compare them using ==

For example, weight = 89.0929; and weight == 89.0929 involves the initial demotion of a double constant to a float variable in the assignment, then the promotion of a float variable to a double to perform the comparison.

You might be able to get away with this, which forces the constants to be floats to begin with
Code:
```   weight = 89.0929f;

if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f))```

4. Originally Posted by Salem
Floating Point
All floats are approximations, so it's unwise to try to compare them using ==

For example, weight = 89.0929; and weight == 89.0929 involves the initial demotion of a double constant to a float variable in the assignment, then the promotion of a float variable to a double to perform the comparison.

You might be able to get away with this, which forces the constants to be floats to begin with
Code:
```   weight = 89.0929f;

if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f))```
that works great thank you!! i came across another problem in this set of code
Code:
```   do{
if (weight <= 204.2247){
years += 1;
c = 3, h = 7, n = 1, o = 2, weight = 89.0929, charge = 0;
}
else if (weight > 204.2247){
c = h = o = n = charge = 0;
weight = 0.0;
}
printf ("Alanine, C:%d H:%d N:%d O:%d, weight:%.4f charge:%d", c, h, n, o, weight, charge);

}while (years != 20 || (charge != 0 && weight >= 204.2247));```
i want it to end loop once the charge = 0 and weight <= 204.2247.

5. Then the test for continuing the loop should be the opposite of when to terminate.
You said you wanted it to end on charge = 0 and weight <= 204.2247... so the logic to invert that for continuing loop is: charge != 0 || weight > 204.2247. Note the change from 'and' to 'or' and the boolean inverse of <= is >.

6. Thanks a lot!! this is my last problem.. I was wondering if my code is right to printf the statement.. it doesn't work atm.
Code:
```#include <stdio.h>int moleculeFound(float weight,int charge, int c, int h, int o, int n);
int main( ) {
int c, h, o, n, atom,amino =0, years = 0, charge = 0;
float weight;
c == 3, h == 7, n == 1, o == 2, weight == 89.0929f, charge = 0;
moleculeFound(weight, charge, c, h, o, n);
return 0;
}
int moleculeFound(float weight, int charge, int c, int h, int o, int n){
if ((c == 3) && (h == 7) && (n == 1) && (o == 2) && (weight == 89.0929f)){printf ("C:%d H:%d N:%d O:%d, weight:%.4f charge:%d", c, h, n, o, weight, charge);}
}```

7. Code:
`c == 3, h == 7, n == 1, o == 2, weight == 89.0929f, charge = 0;`
You're mixing up the equality operator (==) with the assignment operator (=).

Popular pages Recent additions