You are on page 1of 15

Decision Control

Instruction-I
Objectives
What are Control Instructions.
What are decision control Instruction.
If, else keyword
Relational operators.
Instructions
The instructions that are available in C# are as follows
• Type Declaration Instruction.
• Control Instructions
• Synchronization Instructions.
• Locking Instructions.
out of these instruction first is done in previous
section and control structure in this section.
Control Instructions
Control instructions are the instructions which control
the sequence of execution of instructions in program
They are different types of control instructions are as
follows.
• Sequence control instruction.
• Decision control instruction.
• Repetition control instruction.
• Case control instruction.
Normal program
 using System;
class sample
{
 static void Main(string[] args)
 {
 int qty; float price;
 String str;
 Console.WriteLine("enter quantity and price ");
 str = Console.ReadLine();
 qty = Convert.ToInt32(str);
 price = Convert.ToSingle(str);
 Console.WriteLine("quantity and price : {0} {1} ",qty ,price );
 Console.ReadLine();
}
}
Normal C# program
The instruction in previous program are executed
one after another which nothing but sequence
control instruction. No step in between can be
skipped. Each step depends on the previous step for
execution.
Decision Control Instruction
 using System;
 class totalexpenses
 {
 static void Main(string[] args)
 {
 int qty; float price;
 String str;
 Console.WriteLine("enter quantity and price ");
 str = Console.ReadLine();
 qty = Convert.ToInt32( str);
 price = Convert.ToSingle(str);

 }
 }
using System;
public static void Main()
{
int qty; double price;
int dis; double totexp;
Console.Writeline(“Enter values for qty”);
qty=Convert.ToInt32(Console.ReadLine());
Console.Writeline(“Enter values for price);
price=Convert.ToDouble(Console.ReadLine());
if(qty >=1000)
dis=10;
else
dis=0;
totexp=qty*price*qty*price*disc/100;
Console.Writeline(“Total exp=Rs”,totexp);
Console.ReadLine();
}
Tips
 if and else both are the keywords. General form of if
is as follows
 if(condition)
 statement1;
 else
 statement2;
The condition next to if is true then statement inside
if is executed else statement under else part get
executed.
Relation operators
The condition that we write along with an if build
using <,>,<=,>=,==,!= which together are called as
relational operators.
They are called as relational operator because they
help to test relationship between two entities.
 a=b is assigning the value of b to a so ‘=‘ is called as
assignment operator.
 a==b is comparing value of a with the value of b,so
‘=‘used as comparison operator.
Relation operator
The relational operators are as follows:

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
One More Way
using System;
public static void Main()
{
int qty; double price; int dis=0; double totexp;
Console.Writeline(“Enter values for qty”);
qty=Convert.ToInt32(Console.ReadLine());
Console.Writeline(“Enter values for price);
price=Convert.ToDouble(Console.ReadLine());
price
if(qty >=1000)
dis=10;
else
dis=0;
totalexp=qty*price*qty*price*dis/100;
Console.Writeline(“Total exp=Rs”,totexp);
Console.ReadLine();
}
Calculation of gross salary
using System;
 class Salary

{
 public static void Main()
{
 double bs,hra,ca,da,gs;
Console.Writeline(“Enter values for bs”);
bs=Convert.ToDouble(Console.ReadLine());
if(bs>1000)
{
da=bs*95/100;
hra=bs*20/100;
ca= bs*12/100;
}
else
{ da=bs*92/100;
hra=bs*15/100;
ca=200;
gs=bs + hra +da+ca;}
Console.WriteLine(“Gross salary = Rs . “+gs);
Console.ReadLine();
}
}


Default scope is the next statement after if and the
next statement after else. To override the default
scope, we need to make use of pair of braces. There is
no one to one correspondence between if and else
block. There can be 100 statements in if block and 2
statement in else block.
One More Form
 if(condition)
 {
 statement1;
 statement2;
 }
 else
 {
 statement 3;
 statement4;
}

You might also like