Unit-2 Decision Control
Statements
Programming For Problem
Solving
02/19/2025 1
DECISION, SELECTION OR BRANCHING CONTROL INSTRUCTION
• The control instruction which help to check some instruction, condition and take
decision about what to execute.
There are four types of branch control instruction.
• if else
• switch statement
• conditional operation
• goto statement
02/19/2025 2
IF ELSE STATEMENT(Different forms of if)
1 if
if(test expression) else
{ { if(test expression)
true_block statement(s); {
} }
2 if-else else
if(test expression) {
{ }
true_block statement(s); }
}
4. If else ladder
else
if(test expression)
{
{
false_block statement(s);
true_block statement(s);
}
}
3. Nested if-else
else if(test expression)
if(test expression)
{
{
}
true_block statement(s);
else
}
{
}
02/19/2025 3
}
IF STATEMENT EXAMPLE
WAP to find total cost of n items if cost
>1000 discount 100 rs.. if(t>1000)
#include<stdio.h> {
#include<conio.h> d=100;
void main() }
{ t=t-d;
int n,p,d=0,t; print(“total cost=%d”,t);
clrscr(); getch();
printf(“enter the price and total item ”); }
scanf(“%d%d”,&p,&n);
t=n*p;
02/19/2025 4
IF ELSE STATEMENT EXAMPLE
WAP to find larger of two no.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter two no.”);
scanf(“%d%d”,&a,&b);
if(a>=b)
printf(“%d is larger”,a);
else
printf(“%d is larger”,b);
getch();
}
02/19/2025 5
Nested IF ELSE STATEMENT EXAMPLE
WAP to find largest of three no. printf(“%d is largest”, c);
#include<stdio.h> }
#include<conio.h> else
void main() {
{ if(b>=c)
int a,b,c; printf(“%d is largeest”,b);
clrscr(); else
printf(“enter three no.”); printf(“%d is largest”, c);
scanf(“%d%d%d”,&a,&b,&c); }
If(a>=b) getch();
{ }
if(a>=c)
printf(“%d is largeest”,a);
else
02/19/2025 MIET Group of Institutions. 6
• #include <stdio.h>
• void main()
• { int a,b,c;
• printf("enter the numbers:");
• scanf("%d%d%d",&a,&b,&c);
• if (a>b && a>c)
• {
• printf("%d is greatest",a);
• }
• else if (b>a && b>c)
• { printf("%d is greatest",b); }
• else { printf("%d is greatest",c);
• }
• }
02/19/2025 MIET Group of Institutions. 7
IF ELSE LADDER STATEMENT EXAMPLE
WAP to find largest of three no. printf(“%d is largest”, c);
#include<stdio.h> }
#include<conio.h> else if(b>=c)
void main() printf(“%d is largeest”,b);
{ else
int a,b,c; printf(“%d is largest”, c);
clrscr(); }
printf(“enter three no.”); getch();
scanf(“%d%d%d”,&a,&b,&c); }
If(a>=b)
{
if(a>=c)
printf(“%d is largeest”,a);
else
02/19/2025 8
DIFFERENCE B/W IF ELSE AND CONDITIONAL OPERATOR
If else Conditional operator
In if else statement else is optional Both expr2 and expr3 are needed.
N statement can be written within if 1 statement can be written within exp2
and else block. and expr3 block.
Simple with nested conditions Complex with nested conditions
Slow execution speed. Fast execution speed.
02/19/2025 9
CONDTIONAL OPERATOR
*It is known as ternary operators since they take 3 argument exp1?erp2:erp3.
*if exp1 is true or 1,then exp2 is executed otherwise exp3.
*The limitation of this operator is only one statement can occur
Example- int a,b,c; c=(a>b?0:1)
Void main()
{ int a,b,c;
printf(“enter value of a,b,c”);
Scanf(“%d%d%d”,&a,&b,&c);
a>b?( a>c?printf(“a is largest”):printf(“c is largest”) ) : ((b>c?printf(“b is
largest”):printf(“c is largest”));
getch( );
}
02/19/2025 10
GOTO STATEMENT
• C support goto statement to branch unconditionally from one point to another in
the program.
• The goto statement requires a label in order to indentify the place a label is any
valid name and must be followed by a colon
• Like
goto label; label:statement
label:statement goto label;
Forward jump Backward jump
02/19/2025 11
GOTO STATEMENT CONTINUE..
#include <stdio.h>
void main()
{
int i,j;
for(i=1;i<=2;i++)
{
For(j=1;j<=3;j++)
{
if(l==2&&j=3)
goto out;
}
}
out:printf(“out of last loop”);
}
*goto is mainly used to come out of nested loop directly
02/19/2025 12
/ *PROGRAM TO PRINT LARGEST OF 3 NO*/
#include<stdio.h> }
#include<conio.h> if(b>a&&b>c)
void main() {
{ printf(“\n %d is largest”,b);
int a,b,c; }
clrscr(); if(c>a&&c>b)
printf”\n enter 3 no \n”); {
scanf(“%d%d%d”,&a,&b,&c); printf{“\n%d is largest,c);
if(a>b&&a>c) }
{ getch();
printf(“\n%dislargest”,a); }
02/19/2025 13
/ *WAP TO PRINT NO EVEN OR ODD */
#include<stdio.h> if(n%2==0)
#include<conio.h> printf(“\n %dis even no”,n);
void main() else
{ printf(“\n %d is odd no”,n);
int n; getch();
clrscr(); }
printf(“enter the no”);
scanf(“%d”,&n);
02/19/2025 14
/*PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION*/
#include<stdio.h> x2=(-b-sqrt(d))/(2*a);
#include<conio.h> printf(“\n root1=%f”,x1);
#include<math.h> printf(“\nroot2=%f”,x2);
void main() }
{ int a,b,c; else
float d,x1,x2; { d=-d;
clrscr(); printf(“\nimagimary root\n”);
printf(“\n Enter value of a,b,c \n”); x1=-b/(2*a);
scanf(“%d%d5d”,&a,&b,&c); x2=sqrt(d)/(2*a);
d=((b*b)-(4*a*c)); printf(“root1=%f+i%f”,x1,x2);
if(d>=0) printf(“root2=%f-i%f”,x1,x2);
{ printf(“\n real roots\n”); }
x1=(-b+sqrt(d))/(2*a); getch;
}
02/19/2025 15
PROGRAM TO CHECK WHETHER THE YEAR IS LEAP YEAR OR
NOT WITHOUT USING LOGICAL OPERATOR
#include<stdio.h>
#include<conio.h>
void main() else
{ {
int yr;
if(yr%4==0)
clrscr();
printf(“\n enter year\n”); printf(“%d is leap year”,yr);
scanf(“%d”,&yr);
else
If(yr%100==0)
{ printf(“%d is not leap year”,yr);
If(yr%400==0) }
printf(“\n%d is leap year”,yr);
else getch();
printf(“%d is not leap yr”,yr); }
}
02/19/2025 16
SWITCH STATEMENT
*The control statement that allows us to make a decision from the number of
choices is called the switch case statement.
Rules for switch statement
*the switch case must be constant or a constant expression.
*the case label must be constant and unique.
*case label must end with colon(:) and each statement with semi colon(;)
*case label can be int or char constant but it cannot be float.
*using break and default is optional but break should use otherwise after matching
all case will be true
02/19/2025 17
SWITCH STATEMENT CONTINUE…
switch(integer exp)
{
case value1:
block 1;
break;
case value2:
block 2;
break;
case value n:
block n;
break;
Default:
Block x;
}
02/19/2025 18
DIFFERENCE BETWEEN IF ELSE AND SWITCH
If else switch
works on the basis of true/ false ( work on the basis of equality
zero/non-zero) operator
decision control instruction case control instruction
Less readable More readable
allow all sort of condition Case like (i<5) is not permitted.
No need of break break statement is essential
All data types are permit able in Float value is not allowed as case
condition label.
Keyword like “switch”, “case”, “break”,
*keywords like “if” “else” is used.
“default”, are used.
02/19/2025 19
PROGRAM TO DESIGN A CALCULATOR
#include <stdio.h>
case 1 : c=a+b;
#include<conio.h>
printf(“sum is :%d\n",c);
void main()
break;
{
case 2 : c=a-b;
int a,b,c,ch;
printf("Sub is : %d\n",c);
printf("\nEnter 1 for addition:\n ");
break;
printf("Enter 2 for subtraction:\n ");
case 3 : c=a*b;
printf("Enter 3 for multiply:\n");
printf(“Mul is%d\n",c);
printf("Enter 4 for division:\n ");
break;
scanf("%d",&ch);
case 4 : c=a/b;
printf("Enter a number:\n");
printf("div is : %d\n",result);
scanf("%d",&a);
break;
printf("Enter second number:\n");
default: printf("wrong input\n");
scanf("%d",&b);
}
Switch(ch)
getch();
{
}
02/19/2025 20
02/19/2025 21