Decision Making and Branching
In programming the order of execution of instructions may have to be changed depending on certain conditions. This involves a kind of decision making to see whether a particular condition has occurred or not and then direct the computer to execute certain instructions accordingly.
The if statement is a powerful decision making statement and is used to control the flow of execution of statements.
if (expression)
The expression is evaluated and depending on whether the value of the expression is true (non zero) or false (zero) it transfers the control to a particular statement.
if (test expression)
{
statement block;
}
statement n;
if (code = = 1)
{
salary = salary + 500;
}
printf("%d",salary);
The statement block may be a single statement or a group of statements. If the test expression is true, the statement block will be executed otherwise the statement block will be skipped and the execution will jump to the statement n. But if the condition is true, both the statement block and statement n will be executed. Thus if the expression is false only statement n will be executed.
The if... else statement is an extension of the simple if statement
if (code = = 1)
{
salary = salary + 500;
}
else
{
salary = salary + 250;
}
printf ("%d', salary);
If the code is equal to 1, the statement salary = salary + 500 is executed and the control is transferred to statement printf("%d",salary) after skipping the else part.
If the code is not equal to 1, the statement, salary = salary + 500 is skipped and the statement in the else part, salary = salary + 250 is executed before the control reaches the statement printf ("%d", salary);
When a series of conditions are to be checked, we may have to use more than one if... else statement in the nested form.
If the test condition 1 is true then, test condition 2 is checked and if it is true, then the statement block 1 will be executed and the control will be transferred to statement m and it will executed and then statement x will be executed.
If the test condition 1 is true but test condition 2 is false, statement block 2 will be executed and the control is transferred to statement m and it will be executed and then statement x will be executed.
If the test condition 1 is false, then test condition 3 is checked and if it is true, statement block 3 will be executed, then control is transferred to statement n and it will be executed and then statement x will be executed.
If the test condition 1 is false and test condition 3 is also false, statement block 4 will be executed, then the control is transferred to statement n and it will be executed and then statement x is executed.
When many conditions are to be checked then using nested if...else is very difficult, confusing and cumbersome.So C has another useful built in decision making statement known as switch. This statement can be used as multiway decision statement.The switch statement tests the value of a given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed.
Syntax:
The expression is an integer expression or characters. Value 1, value 2, ...... are constants , constant expressions (evaluable to an integral constant) or character and are known as case labels. Each of these values should be unique within a switch statement, statement block 1, statement block 2, ........ are statement list and may contain 0 or more statements. There is no need to put braces between these blocks. The case labels end with a colon (:).
When the switch is executed, the value of the expression is successively compared against the values value 1, value 2, ...... If a case is found whose value matches with the value of the expression, then the block of statement that follows that case are executed. The break statement at the end of each block, signals the end of a particular case and causes an exit from the switch statement transferring the control to the statement n following the switch block. The default is an optional case. When present, it will be executed if the value of the expression does not match with any of the case values and then the statement n will be executed. If not present no action takes place if all matches fails and the control goes to statement n.
# include<stdio.h>
<conio.h>
void main ()
{
float basic , da , salary ;
int code ;
char name[25];
da=0.0;
printf("Enter employee name\n");
scanf("%[^\n]",name);
printf("Enter basic salary\n");
scanf("%f",&basic);
printf("Enter code of the Employee\n");
scanf("%d",&code);
switch (code)
{
case 1:
da = basic * 0.10;
break;
case 2:
da = basic * 0.15;
break;
case 3:
da = basic * 0.20;
break;
default :
da = 0;
}
salary = basic + da;
printf("Employee name is\n");
printf("%s\n",name);
printf ("DA is %f and Total salary is =%f\n",da, salary);
getch();
}
This statement is used to branch unconditionally from one point to another in the program.This statement goto requires a label to locate the place where the branch is to be made.A label is any valid identifier and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred.
Different ways of using goto statement are given below:
void main( )
{
int a, b, c;
float disc, root1, root2, img1;
clrscr();
printf("Enter the value for a, b and c \n");
scanf("%d%d%d",&a,&b,&c);
disc=b*b-(4*a*c);
if(disc==0.0)
{
root1=b/(2.0*a);
root2 = root1;
printf("The roots are equal %f and %f \n", root1, root2);
}
else
if (disc>0)
{
root1=(-b+sqrt(disc))/(2.0*a);
root2=(-b-sqrt(disc))/(2.0*a);
printf("The roots are real %f and %f\n", root1, root2);
}
else
{
if(disc<0)
{disc=abs(disc);
root1= b/(2.0*a);
img1=sqrt(disc)/(2.0*a);
printf("The roots are imaginary %f+i%f\n",root1, img1);
printf("The roots are imaginary %f-i%f\n",root1, img1);
}
}
getch( );
}
Output:
void main( )
{
int a, b, c, d, n;
clrscr();
printf("Enter the number \n");
scanf("%d",&n);
if (n<20)
{
switch (n)
{
case 1: printf("one "); break;
case 2: printf("Two "); break;
case 3: printf("Three "); break;
case 4: printf("Four "); break;
case 5: printf("Five "); break;
case 6: printf("Six "); break;
case 7: printf("Seven "); break;
case 8: printf("Eight "); break;
case 9: printf("Nine "); break;
case 11: printf("Eleven "); break;
case 12: printf("Twelve "); break;
case 13: printf("Thirteen "); break;
case 14: printf("Fourteen "); break;
case 15: printf("Fifteen "); break;
case 16: printf("Sixteen "); break;
case 17: printf("Seventeen "); break;
case 18: printf("Eighteen "); break;
case 19: printf("nineteen "); break;
}
}
if(n>19 && n<1000)
{
a=n/100;
b=n%100;
c=b/10;
d=b%10;
switch(a)
{
case 1: printf("One Hundred "); break;
case 2: printf("Two Hundred "); break;
case 3: printf("Three Hundred "); break;
case 4: printf("Four Hundred "); break;
case 5: printf("Five Hundred "); break;
case 6: printf("Six Hundred "); break;
case 7: printf("Seven Hundred "); break;
case 8: printf("Eight Hundred "); break;
case 9: printf("Nine Hundred "); break;
}
if(b>10 && b<20)
{
switch(b)
{
case 11: printf("Eleven "); break;
case 12: printf("Twelve "); break;
case 13: printf("Thirteen "); break;
case 14: printf("Fourteen "); break;
case 15: printf("Fifteen "); break;
case 16: printf("Sixteen "); break;
case 17: printf("Seventeen "); break;
case 18: printf("Eighteen "); break;
case 19: printf("nineteen "); break;
}
}
else
{
switch (c)
{
case 1: printf("Ten "); break;
case 2: printf("Twenty "); break;
case 3: printf("Thirty "); break;
case 4: printf("Forty "); break;
case 5: printf("Fifty "); break;
case 6: printf("Sixty "); break;
case 7: printf("Seventy "); break;
case 8: printf("Eighty "); break;
case 9: printf("Ninety "); break;
}
switch(d)
{
case 1: printf("one "); break;
case 2 : printf("Two "); break;
case 3 : printf("Three "); break;
case 4 : printf("Four "); break;
case 5 : printf("Five "); break;
case 6 : printf("Six "); break;
case 7 : printf("Seven "); break;
case 8 : printf("Eight "); break;
case 9 : printf("Nine "); break;
}
}
getch();
}
}
C Storage Classes<< Previous
Next >> C Decision Making & Looping
Our aim is to provide information to the knowledge seekers.