Decision Making In C Sharp
if(expression)
{
// statements to execute if expression is true.
}
else
{
// statements to execute if expression is false.
}
{
// statements to execute if expression is true.
}
else
{
// statements to execute if expression is false.
}
using System;
namespace conditional_program
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
if (a < b)
{
Console.WriteLine("a is less than b");
}
else
{
Console.WriteLine("a is greater than b");
}
}
}
}
namespace conditional_program
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
if (a < b)
{
Console.WriteLine("a is less than b");
}
else
{
Console.WriteLine("a is greater than b");
}
}
}
}
a is less than b
- if..else..if
using System;
namespace conditional_program
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
if (a > b)
{
Console.WriteLine("a is less than b");
}
else if (a<b)
{
Console.WriteLine("a is less than b");
Console.ReadKey();
}
else
{
Console.WriteLine("both are same");
Console.ReadKey();
}
}
}
}
namespace conditional_program
{
class Program
{
static void Main(string[] args)
{
int a = 10;
int b = 20;
if (a > b)
{
Console.WriteLine("a is less than b");
}
else if (a<b)
{
Console.WriteLine("a is less than b");
Console.ReadKey();
}
else
{
Console.WriteLine("both are same");
Console.ReadKey();
}
}
}
}
Output
"a is less than b- nested if..else
In some cases , you need to check more than one condition one after another then we can use nested if..else. Using nested if..else we can check two or more conditions simultaneously. The treatment given in nested if else is if one condition is true then go to next condition and check if it is also true or control transfers to the else condition of the first if statement.
using System;
namespace constant_var_program
{
class Programv
{
static void Main(string[] args)
{
Console.WriteLine("Enter two values");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
if (a < b)
{
if (b > a)
{
Console.WriteLine("a is less than b and b is greater than a ");
Console.ReadKey();
}
else
{
Console.WriteLine("b is less then a ");
Console.ReadKey();
}
}
else
{
Console.WriteLine("a is greater than b ");
Console.ReadKey();
}
}
}
}
namespace constant_var_program
{
class Programv
{
static void Main(string[] args)
{
Console.WriteLine("Enter two values");
int a = int.Parse(Console.ReadLine());
int b = int.Parse(Console.ReadLine());
if (a < b)
{
if (b > a)
{
Console.WriteLine("a is less than b and b is greater than a ");
Console.ReadKey();
}
else
{
Console.WriteLine("b is less then a ");
Console.ReadKey();
}
}
else
{
Console.WriteLine("a is greater than b ");
Console.ReadKey();
}
}
}
}
Output
condition 1
Enter two values
10
20
a is less than b and b is greater than a
condition 2
Enter two values
20
10
a is greater than b
condition 1
Enter two values
10
20
a is less than b and b is greater than a
condition 2
Enter two values
20
10
a is greater than b
Decision Making In C Sharp
Reviewed by vishal
on
July 28, 2017
Rating:
No comments