PPS Unit-2 Decision Control Statements
PPS Unit-2 Decision Control Statements
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
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 8
DIFFERENCE B/W IF ELSE AND CONDITIONAL OPERATOR
In if else statement else is optional Both expr2 and expr3 are needed.
02/19/2025 9
CONDTIONAL OPERATOR
02/19/2025 10
GOTO STATEMENT
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)
int n; getch();
clrscr(); }
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
All data types are permit able in Float value is not allowed as case
condition label.
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