C# if else

if() {...} else if () {...} else {...} check conditions, and then execute commands that satisfy the condition. If there is only one command after the condition, {} may not be needed.

int sum=0;
int sum2=0;
int sum3=0;
for(int ii=0;ii<10;ii++)
{
if (ii % 2 == 0)
{
sum2 += ii;
sum2 *= 2;
}
else if (ii % 3 == 0)
sum3 += ii;
else sum += ii;
}

The condition must be a boolean, integer and strings are not correct.
int i=3;
if (i>5) {...}
if (i) {...} //error
bool b=true;
if (b) {...}
if (!b) {...}

C# operators for comparison:
Symbol
Description
!
not, if not satisfy the condition, return true
||
or, whether one of the conditions is satisfied
&&
and, whether two conditions are both satisfied
<
Comparison operator,Less than
>
Comparison operator,Greater than
<=
Comparison operator,Less than or equal to
>=
Comparison operator,Greater than or equal to
!=
Comparison operator,Not equal to
==
Comparison operator, equal
n++
Increment, return original value. e.g. i=2;x=i++; Now x=2, i=3
++n
Increment, return incremented value. i=2;x=++i; Now x=3, i=3
n--
decrement, return original value. e.g. i=2;x=i--; Now x=2, i=1
--n
decrement, return decremented value. i=2;x=--i; Now x=1, i=1
endmemo.com © 2017  Terms of Use | Home