Introduction
Conditional statement is a set of Rule performed if a certain Condition met.
We can achieve this using IF-ELSE Statement.
Skeleton of IF-ELSE Statement
if(<condition-1>)
{
print message
}
else if(<condition-2>)
{
print message
}
else
{
print message
}
we can use only IF instead of IF-ELSE Statement as below.
if(<condition-1>)
{
print message
}
if(<condition-2>)
{
print message
}
if(<condition-3>)
{
print message
}
flowchart for if-else statement |
The above chart describe as below algorithm.
Step 1. Start
Step 2. Read two number a,b
Step 3. if(a>b) then
print " a is greater"
Step 4. else if(a=b) then
print "Both are same"
Step 5. else(a>b) then
print " b is greater"
Step 6. End
Lets Convert this algorithm to a C# program.
using System;
namespace ConsoleApp1
{
class ifelsecheck
{
public void CheckIfElse()
{
int a, b = 0;
Console.WriteLine("Enter two number..");
a = Convert.ToInt16(Console.ReadLine());
b = Convert.ToInt16(Console.ReadLine());
if (a > b)
Console.WriteLine(a + " is bigger");
else if (a == b)
Console.WriteLine(a + " and " + b + " are equal");
else
Console.WriteLine(b + " is bigger");
Console.ReadKey();
}
}
}
Limitations
- IF-ELSE check each and every condition till the condition is satisfied.
- Performance will degrade if more condition is there in the statement.
- ELSE is optional in the statement but IF is compulsory to use it.
- The difference between IF and IF-ELSE is as below.
- IF will check each and every condition
- IF-ELSE will check till condition satisfied.
- IF will check each and every condition
- IF-ELSE will check till condition satisfied.
Thanks.......
t.
No comments:
Post a Comment