Re: C - <= and >= Problems
Quote:
Originally Posted by
boydale1
Code:
if (Val[i]<=90 || Val[i]>=65);
There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?
Re: C - <= and >= Problems
can you provide your input string also.
Re: C - <= and >= Problems
Quote:
Originally Posted by
treuss
There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?
Agreed. But you also have a ; at the end of your if statement!!
Meaning the code in the { } will always run.
Edit: In fact, you do this numerous times.
Re: C - <= and >= Problems
Quote:
Originally Posted by
boydale1
Code:
if ( 65 <= Val[i] <= 90 )
This might not do what you expect it to do either.
it basically evaluates to
Code:
if (( true or false ) <= 90)
you could as well write
try
Code:
if ( Val[i] >= 65 && Val[i] <= 90 )
Kurt
Re: C - <= and >= Problems
Quote:
Originally Posted by
ZuK
Code:
if ( Val[i] >= 65 && Val[i] <= 90 )
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.
Re: C - <= and >= Problems
Quote:
Originally Posted by ninja9578
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code.
Personally, I feel that it is not ambiguous because the relative precedence of >= and && should be well known, thus grouping with parentheses clutters the code. This is also a matter of taste, since one might think that despite the clutter ensuring against misinterpretation by a reader is more important. As for mathematics: I recall that in the mathematical notation that I was taught, the relative precedence of these operators is the same as in C++; but then mathematics should not be a factor here anyway.
Re: C - <= and >= Problems
The input to the function is as follows.
Code:
char A[] = "100";
int * Value;
convert( A, 10, Value );
Thanks for the help. I cleaned up my code a lot.
Things like:
( 65 <= Val[i] <= 90 )
I picked that up in python which is correct, but in C doesn't work.
thanks for the help.
Re: C - <= and >= Problems
Quote:
Originally Posted by
ninja9578
This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.
That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?
Re: C - <= and >= Problems
Quote:
Originally Posted by
boydale1
The input to the function is as follows.
Code:
char A[] = "100";
int * Value;
convert( A, 10, Value );
Thanks for the help. I cleaned up my code a lot.
Things like:
( 65 <= Val[i] <= 90 )
I picked that up in python which is correct, but in C doesn't work.
thanks for the help.
You only created a pointer to an int, not an actual int.
Your program will most likely crash if you try to assign a value to it.
It should read:
Code:
char A[] = "100";
int Value;
convert(A,10,&Value);
Edit: Actually you never even touch *InValue so why are you passing it?
Nor are you returning any values. I have no idea how to help you.
Re: C - <= and >= Problems
Quote:
Originally Posted by
GCDEF
That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?
I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
Re: C - <= and >= Problems
I agree that its not ambiguous at all.
How do the parenthesis force anything?
Re: C - <= and >= Problems
Quote:
Originally Posted by
JohnW@Wessex
I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
But that case is really simple. To me, the parenthesis imply there's more going on than is readily obvious. Simple is always best IMHO.
Re: C - <= and >= Problems
A simple example
A && B || C
You could assume that the coder knows the operator precedence and it does what he intended.
If he were to type A && (B || C) then it doesn't matter whether the coder remembers && or || has the higher precedence, it will work as intended.
Now imagine a much longer and more complicated logic test.
I stick to this principle because it alleviates having to debug silly mistakes and lets me concentrate on the bigger issues.
If parenthesis are superfluous, then what about spaces and carriage returns?
Code:
#include<iostream>
#include<string>
int main(){std::string name;std::cout<<"Name?";std::cin>>name;std::cout<<"Hello "<<name;}
It's not about whether things are superfluous or not, it's about readability and conciseness of meaning;
Re: C - <= and >= Problems
Quote:
Originally Posted by
JohnW@Wessex
A && (B || C)
Sure, but the question is whether it's worthwhile to type
(A) && ((B) || (C)).
I'd say no; everyone should know that the logical operators have lower precedence than comparison operators.
If you had a bitwise operator in there, then it might make sense for clarity.