×

C# If Statements

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:
if(<Condition>)
{
//statements
}
C# If statement Example :
using System;
public class IfExample1
{
public static void Main (string[] args)
{
Console.WriteLine("Enter Your Age??");
int age=Convert.ToInt32(Console.ReadLine()); //reading data from console
if(age>=18)  //checking for age less the 18
{
Console.WriteLine("YES, YOU ARE ELIGIBLE FOR VOTE ");
}
if(age<18)
{
Console.WriteLine("NO, YOU CAN’T VOTE, YOU SHOULD WAIT FOR SOMETIME");
}
}
}
Output
Enter your age ?
12
No, YOU CAN’T VOTE, YOU SHOULD WAIT FOR SOMETIME
19
YES, YOU ARE ELIGIBLE FOR VOTE
  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:
if(<Condition>
{
//Statements will be executed if condition passes
}
else
{
//other statements will be executed if condition fails
}
C# If else Statement Example
using System;
public class IfElseExample
{
public static void Main (string[] args)
{
int num=Convert.ToInt32(Console.ReadLine());
if(num%2==0) //whether number is completely divisible  by 2 
{
Console.WriteLine("the number is even");
}
else
{
Console.WriteLine("The number is odd");
}
}
}
Output
2
The number is even
5
The number is od
  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:
if(<Condition>)
{
//if the condition is true then this block will be executed
}
else if(<Condition>)
{
//if the if condition fails and this passes then this block will be executed
}
else if(<Condition>)
{
//Some other statements
}
else
{
//some statements
}
C# If Else If ladder Statement:
using System;
public class IfElseExample
{
public static void Main (string[] args)
{
int num=Convert.ToInt32(Console.ReadLine());
if(num%2==0) //whether number is completely divisible  by 2 
{
Console.WriteLine("the number is even");
}
else
{
Console.WriteLine("The number is odd");
}
}
}
Output:
2
The number is even
5
The number is odd

Related Topics

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

Boxing and Unboxing in C#

The boxing and unboxing are the terms that refers to the data types in the c# language. The c# language is the frontend language for developing applications. We know that the...

3 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on...

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# If Statements

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...

2 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

1 minute read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.