×

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

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.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute 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# 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# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

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

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# 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# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes 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# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.