How to use C# if else statements

The if-else statement in C# allows you to control the flow of your program based on whether a certain condition is true or false.

The basic logic behind an if-else statement is as follows:

if_else_csharp
  1. Evaluate the condition: The condition is evaluated to determine whether it is true or false.
  2. Execute the appropriate code: If the condition is true, the code inside the first set of curly braces (after the if statement) will be executed. If the condition is false, the code inside the second set of curly braces (after the else statement) will be executed.

C# if statement

The if statement in C# is used to execute a block of code if a given condition is true.

Syntax:
if (condition) { // Run this part if condition is true }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 75; if (mark >= 60) { Console.WriteLine("You passed the exam."); } } } //Output: You passed the exam.

Here, the condition is a Boolean expression that returns either true or false. If the condition evaluates to true, the code inside the curly braces { and } will be executed. If the condition evaluates to false, the code inside the curly braces will be skipped.

C# if else statement

In C#, you can also use an if-else statement to execute different blocks of code based on whether a condition is true or false.

Syntax:
if (condition) { // Run this part if condition is true } else { // Code to be executed if condition is false }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 75; if (mark >= 60) { Console.WriteLine("You passed the exam."); } else { Console.WriteLine("You failed the exam."); } } } //Output: You passed the exam.

Here, the code inside the first set of curly braces will be executed if the condition is true, and the code inside the second set of curly braces will be executed if the condition is false.

Multiple if-else statements

You can also chain multiple if-else statements together to create more complex conditionals.

Syntax:
if (condition1) { // Run this part if condition1 is true } else if (condition2) { // Run this part if condition1 is false and condition2 is true } else { // Run this part if both condition1 and condition2 are false }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 75; if (mark >= 90) { Console.WriteLine("You got an A."); } else if (mark >= 80) { Console.WriteLine("You got a B."); } else if (mark >= 70) { Console.WriteLine("You got a C."); } else { Console.WriteLine("You got a D or lower."); } } } //Output: You got a C.

In the above example, the first if statement checks condition1. If condition1 is true, the code inside the first set of curly braces will be executed. If condition1 is false, the second if statement checks condition2. If condition2 is true, the code inside the second set of curly braces will be executed. If both condition1 and condition2 are false, the code inside the final set of curly braces will be executed.

Nested if-else statement in C#

In C#, you can also use nested if-else statements, where an if or else statement is contained within another if or else statement. This allows you to create more complex conditions and perform different actions based on multiple criteria.

Syntax:
if (condition1) { // Run this part if condition1 is true if (condition2) { // Run this part if condition1 is true and condition2 is true } else { // Run this part if condition1 is true and condition2 is false } } else { // Run this part if condition1 is false }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 75; int attendance = 80; if (mark >= 60) { if (attendance >= 75) { Console.WriteLine("You passed the course."); } else { Console.WriteLine("You failed bcs of poor attendance."); } } else { Console.WriteLine("You failed the course due to low scores."); } } }

In this example, the first if statement checks condition1. If condition1 is true, the second if statement checks condition2. If both condition1 and condition2 are true, the code inside the first set of nested curly braces will be executed. If condition2 is false, the code inside the second set of nested curly braces will be executed. If condition1 is false, the code inside the final set of curly braces will be executed.

You can nest if-else statements to any depth, allowing you to create complex conditions and take different actions based on multiple criteria. However, it's important to keep the code organized and readable, as nested if-else statements can quickly become difficult to understand.

Conditional Operators in C# if statement

In C#, you can use the conditional OR () and conditional AND ( && ) operators in if statements to combine multiple conditions into a single expression.

Conditional or ( || )

The (conditional OR) operator returns true if either of the conditions is true, and false if both conditions are false. The basic syntax of using the operator in an if statement is as follows:

Syntax:
if (condition1 condition2) { // Run this part if condition1 is true or condition2 is true }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 55; int attendance = 65; if (mark >= 60 attendance >= 75) { Console.WriteLine("You passed the course."); } else { Console.WriteLine("You failed the course."); } } }

In above example, the condition mark >= 60 attendance >= 75 is evaluated using the conditional OR () operator. If either of the conditions is true, the code inside the first set of curly braces (after the if statement) will be executed and the message "You passed the course." will be printed. If both of the conditions are false, the code inside the second set of curly braces (after the else statement) will be executed and the message "You failed the course." will be printed.

Conditional and (&&)

The && (conditional AND) operator returns true if both conditions are true, and false if either of the conditions is false. The basic syntax of using the && operator in an if statement is as follows:

Syntax:
if (condition1 && condition2) { // Run this part if condition1 is true and condition2 is true }
Example:
using System; public class HelloWorld { public static void Main(string[] args) { int mark = 75; int attendance = 80; if (mark >= 60 && attendance >= 75) { Console.WriteLine("You passed the course."); } else { Console.WriteLine("You failed the course."); } } } //Output: You passed the course.

In the above example, the condition mark >= 60 && attendance >= 75 is evaluated using the conditional AND (&&) operator. If both conditions are true, the code inside the first set of curly braces (after the if statement) will be executed and the message "You passed the course." will be printed. If either of the conditions is false, the code inside the second set of curly braces (after the else statement) will be executed and the message "You failed the course." will be printed.

You can use these operators to combine multiple conditions and create more complex expressions in your if statements.

Example:
if (age >= 18 && (gender == "male" gender == "female")) { // Code to be executed if the person is 18 years or older and is either male or female }

In the above example, the if statement checks whether the person's age is greater than or equal to 18 and whether their gender is either "male" or "female". If both conditions are true, the code inside the curly braces will be executed.