Conditional Statements in C#
Conditional
Statements
A statement that can be executed based on a condition is known as a “Conditional Statement”. The statement is often a block of code.
There
3 type of conditional statements
·
Conditional Branching
·
Conditional Loop
·
Jump Statements
Conditional
Branching:
C#
Conditional Branching primarily focuses on If and Switch statements, the main
mechanisms for executing or branching to different code depending on whether a
condition is true or false
In
C# are the following 2 conditional branching statements
1.
if statement
2. Switch statement
if
Statement
The
if statement allows you to test whether or not a specific condition is met.
Syntax
If(<Condition>)
<statements>;
Else
if(<Condition>)
<statements>;
---------------------
-----------------------
Else
<statements>;
WAP,
read a and b numbers and find greatest number.
using System;
class ifdemo
{
public static void Main()
{
int a,b;
Console.WriteLine("enter 2 no ");
a=Int.Parse (Console.ReadLine());
b=Int.Parse(Console.ReadLine());
if(a>b)
{
Console.WriteLine("a is
greather");
}
else If(a< b)
{
Console.WriteLine("b is
greather");
}
else
{
Console.WriteLine("both are
Equals");
}
Console.ReadLine();
}
switch
Statement
The
switch statement compares two logical expressions.
Syntax
switch(<Expression>)
{
Case
<Value> :
<stmts>
Break;
-----------------------
-------------------------
------------------------
Default
:
<stmts>
Break;
}
Programs
C# Program to find greater number
among two numbers.
using System;
namespace
Conditional
{
class Program
{
static void Main(string[] args)
{
int a = 2, b = 3;
if (a > b)
Console.WriteLine("a is
greater than b!");
else
Console.WriteLine("b is
greater than a!");
Console.ReadLine();
}
}
}
C# Program to find greatest number
among three numbers.
using System;
namespace
Conditional
{
class Program
{
static void Main(string[] args)
{
int a = 2, b = 1, c = 5;
if (a > b && a > c)
Console.WriteLine("a is
the greatest number!");
else if (b > a && b >
c)
Console.WriteLine("b is
the greatest number!");
else
Console.WriteLine("c is
the greatest number!");
Console.ReadLine();
}
}
}
C# Program to find greatest number
among three numbers using Nested if Statement.
using System;
namespace
Conditional
{
class Program
{
static void Main(string[] args)
{
int a = 2, b = 1, c = 5;
if (a > b)
{
if (a > c)
Console.WriteLine("a
is the greatest number!");
else
Console.WriteLine("c
is the greatest number!");
}
else
{
if (b > a)
Console.WriteLine("b
is the greatest number!");
else
Console.WriteLine("c
is the greatest number!");
}
Console.ReadLine();
}
}
}
Switch statement
C# Program to perform an operation of user choice for addition, subtraction, multiplication and division
using
System;
namespace
conditional
{
class Program
{
static void Main()
{
int a, b, choice;
Console.WriteLine("Enter first
number:");
a =
(int)Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter
second number:");
b =
(int)Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the
number operation you want to perform from the menu.");
Console.WriteLine("1)
Addition");
Console.WriteLine("2)
Subtraction");
Console.WriteLine("3)
Multiply");
Console.WriteLine("4)
Divide");
Console.Write("Choice:
");
choice =
(int)Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine(a + b);
break;
case 2:
Console.WriteLine(a - b);
break;
case 3:
Console.WriteLine(a * b);
break;
case 4:
Console.WriteLine(a / b);
break;
default:
Console.WriteLine("Invalid choice!");
break;
}
Console.ReadLine();
}
}
}
Comments
Post a Comment