in c sharp programming language what does the ? symbol do?
i saw it used in an example like :
hour = ((h >= 0 && h< 24) ? h : 0 );
and i have no idea what the question mark symbol means here
THanks
Printable View
in c sharp programming language what does the ? symbol do?
i saw it used in an example like :
hour = ((h >= 0 && h< 24) ? h : 0 );
and i have no idea what the question mark symbol means here
THanks
Statements using ? and : have two parts and look like this:
condition ? if-true : if-false
So it checks the condition and if the condition is true then it returns if-true, otherwise returning if-false.
So for example:
Will assign x to be equal to 2, because 5 is not greater than 7 (that is 5 > 7 = false)Code:int x = (5 > 7) ? 1 : 2;
In the code you posted:
It will assign whatever the value of h is to hour when h is between 0 and 24. Otherwise, it will assign it to zero. This could equivalently be rendered:Code:hour = ((h >= 0 && h< 24) ? h : 0 );
This block is more verbose so the notation using ? and : is simplified.Code:if( h >= 0 && h < 24 )
hour = h;
else
hour = 0;