Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C# If Else Statement

by suresh

The C# If else control statement has two blocks. One is true block another one if false block.

In case condition in if block is true, the statements in if block executed, if it is false, the else block statements executed.

The syntax of the C# if else is

if <condition>
{
   Statements //These statements are executed if the condition is true
}
else
{
   Statements //These statements are executed if the condition is false
}

C# If Else Statement Example

Let us see an example on if else by finding whether the given input number is even or odd?

using System;

namespace CSharp_Tutorial
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Please enter any Numeric Value  ");
            int i = int.Parse(Console.ReadLine());

            if (i % 2 == 0)
            {
                Console.WriteLine("{0} is even number", i);
            }
            else
            {
                Console.WriteLine("{0} is odd number", i);
            }
        }
    }
}

OUTPUT

C# If Else Statement 1

In the above C# code, we have taken integer variable i

Since Console.ReadLine() returns a string, and we explicitly converted to an integer using Parse.int

C# If Else Statement 2

The input number is given 93

Since 93%2 = 1 the output is

93 is odd number

Placed Under: C#

  • Dot Net Framework
  • C# Basic Example Program
  • C# Variables
  • C# Constant
  • C# Keywords
  • C# Regular Expressions
  • C# Built in Data Types
  • C# Nullable Types
  • C# Data type Conversion
  • C# Date and Time Format
  • C# Enum or Enumerator
  • C# Value and Reference types
  • C# Operators
  • C# Arithmetic Operators
  • C# Assignment Operators
  • C# Bitwise Operators
  • C# Logical Operators
  • C# Null Coalescing operator
  • C# Relational Operators
  • C# Ternary Operator
  • C# Unary Operators
  • C# If Statement
  • C# If Else Statement
  • C# Else if Statement
  • C# Nested If Statement
  • C# Break Statement
  • C# Continue Statement
  • C# goto statement
  • C# Switch Case
  • C# While Loop
  • C# Do while loop
  • C# For Loop
  • C# Foreach loop
  • C# String Builder
  • C# String
  • C# String Functions
  • C# Array
  • C# Array Functions
  • C# Multi Dimensional Arrays
  • C# Jagged Array
  • C# OOPS Introduction
  • C# Constructor
  • C# Destructor
  • C# Access Modifiers
  • C# Inheritance
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy