If statement
The if statement creates a simple branch in the code based on a provided condition. If the condition is true, the contents of the if statement are executed. It can have an accompanying else statement, which executes if the condition is false.
If statements can be the target of a break statement, but only if they are labeled. The break statement can be used from inside of both branches.
If both branches are present, if statements can yield a value (effectively like a "ternary operator" from other C-like languages) which is very useful for writing short and expressive code.
Example
This example loops through an array of numbers, and counts how many of them are odd, and how many are even.
The following example shows how to skip the execution of an if statement once entered by using the break statement. This happens in lines 7 and 9 below. Note that to break out of just an if statement, and not the containing loop, the if statement's label must be provided to the break statement. Otherwise, it will break out of the innermost loop.
Branch hinting
When writing code, it might be obvious that some if statement's condition might be, on average, much more likely to execute one branch over the other. In such cases, you can use branch hinting, which could improve code execution performance when the favoured branch is taken.
Branch hinting has no effect on the semantics of the program. When the branch hint is incorrect (e.g. the condition frequently evaluates to true, but has the [Unlikely] attribute), the performance of the program might be impacted negatively.