if condition

Hi everybody,

newValue = ( firstValue > secondValue ? firstValue : secondValue );

In this case, what happens if the firstValue equals to the secondValue ?

To be safe, is it better to write

newValue = ( firstValue >= secondValue ? firstValue : secondValue );
closed account (zb0S216C)
smelas wrote:
In this case, what happens if the firstValue equals to the secondValue ? (sic)

It won't make a difference. If the condition is true, firstValue is used. Otherwise, secondValue is used instead.

smelas wrote:
To be safe, is it better to write

newValue = ( firstValue >= secondValue ? firstValue : secondValue ); (sic)

It depends on what you're testing for.

Wazzak
That's right. Let me confirm.

newValue = ( firstValue > secondValue ? firstValue : secondValue );

so if it's like above and

firstValue=secondValue

then secondValue will be used, right ? Because, the statement says

if the firstValue > secondValue, use firstValue. Otherwise it's secondValue...

closed account (zb0S216C)
smelas wrote:
so if it's like above and

firstValue=secondValue

then secondValue will be used, right ? Because, the statement says

if the firstValue > secondValue, use firstValue. Otherwise it's secondValue... (sic)

Yes. Consider this example for clarity:

1
2
3
4
5
6
7
8
9
int main( )
{
    int Value( 0 );
    int FirstValue( 10 ), SecondValue( 23 );

    Value = ( ( FirstValue > SecondValue ) ? FirstValue : SecondValue );

    std::cout << "Biggest: " << Value << std::endl;
}

In this code, Value is assigned the value of SecondValue. If the condition is true, FirstValue would be assigned to Value. If the condition is false, SecondValue is assigned to Value.

Now, assigning FirstValue to the value of SecondValue won't make a difference except that Value will have the same value, be the condition true or false.

Wazzak
Last edited on
Topic archived. No new replies allowed.