Conditional Statement In C#



Conditional Statement


A conditional statement allows you to control the flow of your application by selecting the statement that
is executed, based on the value of a Boolean expression. Syntax:
a. If(boolean-expression) statement
b. If(boolean-expression) statement1
else statement2
c. If(boolean-expression) statement1
else if(boolean-expression) statement2
else statement3
Example 01:
bool condition;
if (condition == true)
{
Console.WriteLine("condition is true");

}
else if (condition == false)
{

Console.WriteLine("condition is false");
}
else
{

Console.WriteLine("unknown value");

}

d. If((boolean-expression) operator (boolean-expression)) statement
Example:
if (scoreOutOfHundred >= 60 && scoreOutOfHundred < 80)
{
return "A+";
}
e. (boolean-expression) ? (statement1) : (statement2)
Example:
bool value = true;
Console.WriteLine(value ? "True Value" : "False Value");

Exmaple 02:
using System;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Please enter English Score : ");
int englishScore = Convert.ToInt32(Console.ReadLine());
string result = String.Empty;
if (englishScore > 100 || englishScore< 0)
{
result = "You have entered wrong number . Please check and try again";
}
else if (englishScore >= 50)
{
result = "Congratulation , You have Passed";
}
else
{
result = "Sorry , You have Failed and try again";
}
Console.WriteLine(result);
Console.ReadKey();
}
}
}


Post a Comment

Previous Post Next Post