The syntax of the if statement is as follows:
if(expression)
{
//Statements1
}
else{
//statements 2
}
In the preceding syntax,
if: Refers to the if the keyword that indicates an if statement.
expression: Refers to the Boolean expression that acts as a condition for the if statement.
{}: Refers to a pair of curly braces, which is required when you want to execute a block of statements in the if statement. A block of statements refers to a group of multiple statements that are treated as a single statement.
statemtns1: Refers to the set of statements that is executed when an expression evaluates to true.
The set of statements can be a single statement or a block of statements.
statement2: Refers to the set of statements that is executed when the expression evaluates to false.
This set of statements is also optional and can be a single statement of the block of statements.
The preceding syntax is the syntax of a simple if statement that involves only one if clause and one else clause. However, C# allows you to nest one if statement within another if statement. You can nest multiple if statements in the if or else clause of the if statements. Note that in such cases, the else clause corresponds to the last or the most recent if statement. This is because the compiler associates the else clause with the last unmatched if statement (if statement without an else clause).
The syntax of nested if statements are as follows:
If(expression)
{
If(expression2)
{
//statemtns1
}
else
{
//statements2
}
}
else
{
If(expression3)
{
//statements3
}
else
{
//statements4
}
}
In the preceding syntax:
if : Refers to the if keyword that indicates an if statement
else: Refers to the else keyword that indicates the else clause of the if statement
expression1,expression2,expressioin3 :Refer to the Boolean expression of the if statements
statements1,statements2, statements3, statements4 : Refer to the sets of statements
For example
Check odd or event number by c# console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number :");
int number = Convert.ToInt32(Console.ReadLine());
if (number%2==0)
{
Console.WriteLine(number + " is an even number");
}
else
{
Console.WriteLine(number + "is an odd number");
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationDemo
{
class Program
{
static void Main(string[] args)
{
int enteredNumber;
Console.WriteLine("Enter the number rang of 0 to 5");
enteredNumber = Convert.ToInt32(Console.ReadLine());
if (enteredNumber==0)
{
Console.WriteLine("you entered ZERO");
}
else if (enteredNumber==1)
{
Console.WriteLine("you entered ONE");
}
else if (enteredNumber == 2)
{
Console.WriteLine("you entered TWO");
}
else if (enteredNumber == 3)
{
Console.WriteLine("you entered THREE");
}
else if (enteredNumber == 4)
{
Console.WriteLine("you entered FOUR");
}
else if (enteredNumber ==5)
{
Console.WriteLine("you entered FIVE");
}
else
{
Console.WriteLine("Your number is grater then 5");
}
Console.ReadKey();
}
}
}
Comments