Using decision statements in C#

✓ Equality and relational operators

==   Equal to
!=   Not equal to
<   Less than
<=   Less than or equal to
>   Greater than
>=   Greater than or equal to

✓ Conditional logical operators

/*if the left operand of the && operator evaluates to false, the result of the entire expression must be false, regardless of the value of the right operand. */
bool validPercentage;
validPercentage = (percent >= 0) && (percent <= 100); 

/*if the value of the left operand of the || operator evaluates to true, the result of the entire expression must be true, irrespective of the value of the right operand.*/
bool invalidPercentage;
invalidPercentage = (percent < 0) || (percent > 100);

✓ IF statement

if (day == 0)
{
    dayName = "Sunday";
}
else if (day == 1)
{
    dayName = "Monday";
}
else if (day == 2)
{
    dayName = "Tuesday";
}
else if (day == 3)
{
    dayName = "Wednesday";
}
else if (day == 4)
{
    dayName = "Thursday";
}
else if (day == 5)
{
    dayName = "Friday";
}
else if (day == 6)
{
    dayName = "Saturday";
}
else
{
    dayName = "unknown";
}

✓ Switch statements

switch (day)
{
case 0 :
   dayName = "Sunday";
   break;
case 1 :
    dayName = "Monday";
    break;
case 2 :
    dayName = "Tuesday";
    break;
...
default :
    dayName = "Unknown";
    break;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s