C-sharp If Else Statements (C#)

C-sharp if else statements are useful in decision making in C#. You can execute the block of codes based on the specified logical conditions is true or false. There can be different decisions in C# and different codes are executed based on these decisions.

The C-sharp if else statements are based on the following condition:-

  1. if
  2. if else
  3. if else if else...

Let’s find out these C-sharp if else statements in detail one-by-one.

C# if Statement

C# if statement executes the block of code based on the single condition is true. There is only a single block of code with a single condition to check within the if statement.

Syntax

The above syntax shows the variable ‘condition’ that can be true or false to check.

Let’s find out the statement with the example given below.

Output

a is greater than b

The above example first declares the two variables with their values. After that, it uses the if statement with a single condition ‘a < b ‘. It only executes the block of code when the specified condition is true.

The result shows that the value of a is greater than the value of b.

C-sharp (C#) if else statements

The if else statement executes the block of code inside the if statement when the condition is true. If the given condition is not true, it executes the code placed inside the else statement.

Syntax

The syntax given above contains a single condition to check within the if statement.

Let’s see a simple example to learn the use of if else statement.

Output

a is less than b

In the above example, there are two variables that have certain values. The if statement contains the condition to check whether the value of a is greater than or not. If the value of a is not greater than b, it executes the code placed within the else statement.

The result shows that the value of a is less than the value of b.

C-sharp (C#) if else if else Statements

There are different if conditions in this statement. It checks which if condition is true to execute one block of code. If all the condition is false, it executes the block of code given within the else statement.

Syntax

The above syntax shows that there can be many if conditions to check to execute the block of codes. If the condition is false, it runs the block of code placed inside the else statement.

Let’s see a simple example to learn the use of the if else if else statement.

Output

a is less than b

When you check the above example, it contains the two conditional statements within different if andelse if statement. If any condition is true, it executes the block of code placed within it. However, when all the conditions are false, it executes the block of code within the else statement.

The above result shows that the second if condition is true to execute the block of code placed within it.

You May Also Like to Read

Reference

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.