|
-
April 10th, 2011, 10:04 PM
#1
C sharp programming- what does the ? symbol do?
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
-
April 10th, 2011, 11:31 PM
#2
Re: C sharp programming- what does the ? symbol do?
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:
Code:
int x = (5 > 7) ? 1 : 2;
Will assign x to be equal to 2, because 5 is not greater than 7 (that is 5 > 7 = false)
In the code you posted:
Code:
hour = ((h >= 0 && h< 24) ? h : 0 );
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:
if( h >= 0 && h < 24 )
hour = h;
else
hour = 0;
This block is more verbose so the notation using ? and : is simplified.
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|