Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

C# If Else


Certainly! In C#, if and else are control flow statements used for decision-making. They allow you to execute certain blocks of code based on conditions. Here's a simple explanation with an example:

  1. if statement: It evaluates a condition and executes a block of code if the condition is true.

  2. else statement: It follows an if statement and executes a block of code if the condition in the if statement is false.

Here's an example:

Example

using System;

class Program
{
static void Main()
{
int number = 10;

if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is not positive.");
}
}
}

Explanation:

You can also have multiple conditions using else if, which allows you to check additional conditions if the previous ones are false:

using System;

class Program
{
static void Main()
{
int number = 0;

if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}

In this example:



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java