The if
statement in C# is used to execute a block of code if a certain condition is true. The syntax for an if
statement is as follows:
1 2 3 4 5 6 | if (condition) { // Code to execute if the condition is true } |
The condition
is an expression that evaluates to a boolean value (either true
or false
). If the condition is true
, the code inside the curly braces will be executed. If the condition is false
, the code will be skipped.
You can also use an else
statement to specify a block of code to be executed if the condition is false
. The syntax for an if
…else
statement is as follows:
1 2 3 4 5 6 7 8 9 10 | if (condition) { // Code to execute if the condition is true } else { // Code to execute if the condition is false } |
Here is an example of an if
…else
statement:
1 2 3 4 5 6 7 8 9 10 11 | int x = 10; if (x > 0) { Console.WriteLine("x is positive"); } else { Console.WriteLine("x is not positive"); } |
This code will output the following:
1 2 3 | x is positive |
You can also use an else if
clause to specify additional conditions to check. The syntax for an if
…else if
…else
statement is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition1 is false and condition2 is true } else { // Code to execute if both condition1 and condition2 are false } |
Examples
Here are ten examples of the if
and else
statements in C#:
Simple if
statement:
1 2 3 4 5 6 | if (x > 0) { Console.WriteLine("x is positive"); } |
if
…else
statement:
1 2 3 4 5 6 7 8 9 10 | if (x > 0) { Console.WriteLine("x is positive"); } else { Console.WriteLine("x is not positive"); } |
if
…else if
…else
statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if (x > 0) { Console.WriteLine("x is positive"); } else if (x < 0) { Console.WriteLine("x is negative"); } else { Console.WriteLine("x is zero"); } |
Nested if
statements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | if (x > 0) { if (x < 10) { Console.WriteLine("x is between 0 and 10"); } else { Console.WriteLine("x is greater than 10"); } } else { Console.WriteLine("x is negative"); } |
if
statement with a compound condition:
1 2 3 4 5 6 | if (x > 0 && x < 10) { Console.WriteLine("x is between 0 and 10"); } |
if
statement with a bool
variable:
1 2 3 4 5 6 7 | bool isPositive = x > 0; if (isPositive) { Console.WriteLine("x is positive"); } |
if
statement with a nullable
type:
1 2 3 4 5 6 7 8 9 10 11 | int? y = null; if (y.HasValue) { Console.WriteLine("y has a value"); } else { Console.WriteLine("y is null"); } |
if
statement with an enum
type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | enum Color { Red, Green, Blue } Color c = Color.Green; if (c == Color.Green) { Console.WriteLine("c is green"); } |
if
statement with a string
:
1 2 3 4 5 6 7 | string s = "hello"; if (s.Length > 5) { Console.WriteLine("s has more than 5 characters"); } |
if
statement with a switch
statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | switch (x) { case 1: Console.WriteLine("x is 1"); break; case 2: Console.WriteLine("x is 2"); break; default: Console.WriteLine("x is not 1 or 2"); break; } |