C# If Statements

tutorial and example
tutorial and example

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false then else block gets executed.

There are four types of if statements:

  1. If Statements
  2. If Else Statements
  3. Nested If Statement
  4. If else if ladder

If Statement:

In C#, If statement checks the condition and executes If block on the true condition.

Syntax:

C# If statement Example :

Output

  1. if else statement:

if the condition provided in if statement is true then if block gets executed and if it fails then else block gets executed.

Syntax:

C# If else Statement Example

Output

  1. Nested If statement:

if(<condition>)

{

if(<condition>)

{

//statements

}

if(<Condition>)

{

//some statements

}

}

else

{

if(<Condition>)

{

//statements

}

else(<Condition>)

{

//Statements

}

} 

  1. if else if ladder statements:

if else if provides various options in operations. It checks multiple condition on an operand and provides the result.

Syntax:

C# If Else If ladder Statement:

Output: