If Statement

Each part of the if statement must be a single statement.
If we want to execute more than one statement then we must combine them into a single block statement using curly brackets.
In C# the parentheses around the condition to be tested is compulsory.


If

if (bValue == true) 
{
   sVariable1 = "text";
}

You can put everything on one line, but this makes it much harder to read.

if (bValue == true) sVariable1 = "text"; 

If - Else

if (bValue == true) 
{
   sVariable1 = "text";
}
else
{
   sVariable2 = "text";
}

You can use the Tenary Operator

if (bValue == true) ? sVariable1 = "text" sVariable2 = "text"; 

If - ElseIf - Else

Very important to remember that "else if" is 2 words in C#

if (iValue < 10) 
{
}
else if (iValue < 20)
{
}
else
{
}

If (OR)

if ( ( a > b ) | ( c < d ) ) 
{
}

If (OR conditional)

You can use the "OrElse" if you do not want the second expression evaluated when the first expression is not true

if ( ( a > b ) || ( c < d ) ) 
{
}

If (AND)

if ( ( a > b ) & ( c < d ) ) 
{
}

If (AND conditional)

You can use the "OrElse" if you do not want the second expression evaluated when the first expression is not true

if ( ( a > b ) && ( c < d ) ) 
{
}

If Nested

if (bValue === false) 
{
   if (iValue < 10)
   {
   }
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext