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 this on one line, but this makes it much harder to read.
if (bValue == true) sVariable1 = "text";
if (bValue == true) { sVariable1 = "text"; }
Or you can put this on two lines.
if (bValue == true)
{ sVariable1 = "text"; }
If - Else
if (bValue == true)
{
sVariable1 = "text";
}
else
{
sVariable2 = "text";
}
You could put this on 4 lines
if (bValue == true)
{ sVariable1 = "text"; }
else
{ sVariable2 = "text"; }
Or you can use the Conditional 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)
{
}
}
Continue
This can be used inside an if statement, when it is inside a loop.
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
Break
This can be used inside an if statement, when it is inside a loop.
The break statement can also be used to jump out of a loop.
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext