C# If statement
If statement is one of the flow control and conditional statements in C# programming language. Control flows change the execution flow of a programming language.
If someone for example, want to execute a particular statements based on some logic, then flow control and conditional statement come up.
The if statement is used to evaluate a Boolean expression before executing a set of statements. The statements are run if the expression evaluate to true else it will run another statement.
Syntax
if(boolean expression)
{
//statements to run
}
Code
using system
namespace csharpnaijaDemo
{
class program
{
static void main(string[] args)
{
int value=10;
if(value <= 10)
{
Consoile.WriteLine("value is less than or equal to 10");
}
Console.WriteLine();
}
}
}
if...else statement
if-else statement is a second part of the if statement in c#, that is the else. The else statement must follow if or else if statement.syntax
if(boolean expression)
{
//execute this code block if boolean expression evaluates to true
}
else
{
// execute this code block when if boolean expression evaluates to false
}
example
int i = 5, j = 10;
if (i > j)
{
Console.WriteLine("i is greater than j");
}
else
{
Console.WriteLine("i is either equal to or less than j");
}
else if statement
The if statement can also follow as else statement, if you want to check for another condition in the else part.
syntax
if(expression)
{
// logic to be executed
}
else if{ //logic to be executed}
example
int i = 5, j = 10;
if (i > j)
{Console.WriteLine("i is greater than j");}
else if (i < j)
{ Console.WriteLine("i is less than j");}
else if (i == j)
{ Console.WriteLine("i is equal to j");}
Well arranged codes
ReplyDeleteThanks
DeleteYou are most welcome
ReplyDelete