1 . I F S T A T E M E N T
2 . S W I T C H S T A T E M E N T
3 . C O N D I T I O N A L O P E R A T O R S T A T E M E N T
4 . G O T O S T A T E M E N T
Decision Making & Branching
TYPES OF IF STATEMET
The if statement is implemented indifferent
forms depending on the complexity of
conditions to betested:
• Simple if statement
• if…..elsestatement
• Nested if…..else statement
• Else if ladder
SIMPLE IF STATEMENT
if(test-expression)
{
statement-block;
}
Statement-x;
Test
expression?
Entry
False
True
FLOW CHART OF SIMPLE IF
Statemet - Block
Entry
True
Test expression?
False
Statement-x
Next Statement
W.A.P to display a number if user enters negative number
If user enters positive number, that number won't be
displayed
intmain()
{
int number;
printf("Enter an integer:");
scanf("%d", &number);
// Testexpression is true if number is less than 0
if (number < 0)
{
printf("You entered %d.n", number);
}
printf(“Enter positive number.");
return0;
}
Output1
Enter an integer:-2
You entered -2.
Enter Positive number
W.A.P to check whether 2 nos
are equal
IF – ELSE STATEMENT
The if….elsestatement isan extensionof the simple if statement.
The general formis:
if(test-expression)
{
True-block statement(s);
}
else
{
False-block statement(s);
}
Statement-x;
FLOWCHART OF IF - ELSE
True-Block Statements
Entry
Test expression?
TrueFalse
Statement-x
Next Statement
False-Block Statements
W.A.P to check whether 2 nos
are equal or not
W.A.P whether given num is
even or odd
W.A.P to check whether person
is eligible for voting or not
Program to check number is
divisible by 5 and 11
int main()
{
int num;
/* Reads number from user */
printf("Enter any number: ");
scanf("%d", &num);
if((num % 5 == 0) && (num % 11 == 0))
{
printf("Number is divisible by 5 and 11");
}
else
{
printf("Number is not divisible by 5 and 11");
}
return 0;
}
Program to find maximum of 2 nos
intmain()
{
int num1, num2; printf("Enter two
numbers:");
scanf("%d%d", &num1, &num2);
if(num1 > num2)
{
// True part means num1 >num2
printf("%d is maximum", num1);
}
else
{
// False part means num1 <num2
printf("%d is maximum", num2);
}
return0;
}
NESTED IF - ELSE
if(test condition 1)
{
if(test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Statement-x;
FLOW CHART OF NESTED IF - ELSE
Test condition 1?
Entry
True
False
Statement-3
Test condition 2?
Statement-1
Statement-x
Next Statement
Statement-2
TrueFalse
W.A.P to check whether two
numbers are >,<,= or !=
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 !=var2)
{
printf("var1 is not equal to var2");
//Below – if-else is nested inside another if block
if (var1 >var2)
{
printf("var1 is greater than var2");
}
else
{
printf("var2 is greater than var1");
}
}
else
{
printf("var1 is equal to var2");
}
W.A.P to find maximum of 3 nos
int main()
{
int num1, num2, num3, maximum;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
maximum = num1;
}
else
{
maximum = num3;
}
}
else
{
if(num2 > num3)
{
maximum = num2;
}
else
{
maximum = num3;
}
}
printf("Maximum among all three
numbers = %d", maximum);
return 0;
}
W.A.P to read three values from keyboard and print out the largest of them without using
if statement.
void main()
{
int x,y,z;
clrscr();
printf("Enter Three Numbers:--n");
scanf("%d %d %d",&x,&y,&z);
((x>y)&&(x>z))?printf("Largest is x :--
%d",x):((y>x)&&(y>z))?printf("Largest is y :-- %d",y):printf("Largest is z :-- %d",z);
}
Output:--
Enter Three Numbers:--
3 4 5
Largest is z :-- 5
ELSE – IF LADDER
if (test condition 1)
{ statement –1 ; }
elseif (test condition 2)
{ statement –2 ; }
elseif (test condition 3)
{ statement – 3 ; }
-------------
elseif (test condition n)
{ statement – n ; }
else
{ default-statement; }
Statement – x;
FLOWCHART OF ELSE-IF LADDER
Condition-2
Condition-n
Statement-1
Statement-2
Statement-n
Default-statement
Statement-x
True
True
True False
Condition-1
False
False
Program to check whether 2 nos are
=,<,>,!=main(){
int var1,var2;
printf("Input the value ofvar1:");
scanf("%d", &var1);
printf("Input the value ofvar2:");
scanf("%d",&var2);
if (var1 ==var2)
{
printf("var1 is equal tovar2");
}
else if (var1 >var2)
{
printf("var1 is greater thanvar2");
}
else if (var2 > var1)
{
printf("var2 is greater thanvar1");
}
else
{
printf("var1 is not equal tovar2");
}}
Write a C program to check whether given character is a digit,
lowercase alphabet or an uppercase alphabet.
void main()
{
charch;
clrscr();
printf("Enter the character:");
scanf("%c",&ch);
if((ch>='0')&&(ch<='9'))
printf("the number is %cdigit",ch);
else if((ch>='a')&&(ch<='z'))
printf("the character is %clowercase",ch);
else if((ch>='A')&&(ch<='Z'))
printf("the character is %cuppercase",ch);
else
printf("thecharacter is %cothertype");
getch();
}
Output:
Enter the character:7
the number is 7 digit
Enter thecharacter:R
the character is Ruppercase
SWITCH STATEMENT
Switch ( expression ) //expression is any integerexpression orcharacters
{
case value-1 : //valuesare constantsorconstantexpressionsevaluableto an integral constant
block-1;
break;
case value-2 :
default:
block-2;
break;
//default is optional..executed when nocasevalue matches
default-block;
break;
}
Statement-x;
Implement a Calculator using Switch Case
void main()
{
int a, b, c, d, e, f, ch;
printf("Enter the first no. :");
scanf("%d", &a);
printf("Enter the second no. :");
scanf("%d", &b);
printf("Enterthechoiceas n 1.Addn 2.Sub
n 3.Multiplyn 4.Dividen");
scanf("%d", &ch);
switch(ch)
{
case1:
c=a+b;
printf("%d",c);
break;
case2:
d=a-b;
printf("%d",d);
break;
case 3:
e=a*b;
printf("%d", e);
break;
case 4:
f=a/b;
printf("%d", f);
break;
default:
printf("Wrong Input");
break;
}
getch();
}
Program to find number of days in a month using switch case
int main()
{
int month;
printf("Enter month number(1-12): ");
scanf("%d", &month);
switch(month)
{
case1:
case3:
case5:
case7:
case8:
case10:
case 12: printf("31days");
break;
case 4:
case 6:
case 9:
case 11: printf("30 days");
break;
case 2: printf("28/29 days");
break;
default: printf("Invalid input! Please enter
month number between 1-12");
}
return 0;
}
GOTO STATEMENT
If you want to jumpsome wherewithoutchecking
any condition thengoto is used.
statements;
gotolabel1;
Statements;
lable1 :
statements;
statements;
label1 :
statements;
goto label1;
Statements;
GoTo Example
intmain()
{
intage;
Vote:
printf("you are eligible forvoting");
NoVote:
printf("you are not eligibletovote");
printf("Enter yourage:");
scanf("%d", &age);
if(age>=18)
gotoVote;
else
gotoNoVote;
return 0;
}
Output:
Enter your age: 23
you are eligible for voting
Enter your age: 15
you are not eligible to vote

Decision making and branching in c programming

  • 1.
    1 . IF S T A T E M E N T 2 . S W I T C H S T A T E M E N T 3 . C O N D I T I O N A L O P E R A T O R S T A T E M E N T 4 . G O T O S T A T E M E N T Decision Making & Branching
  • 2.
    TYPES OF IFSTATEMET The if statement is implemented indifferent forms depending on the complexity of conditions to betested: • Simple if statement • if…..elsestatement • Nested if…..else statement • Else if ladder
  • 3.
  • 4.
    FLOW CHART OFSIMPLE IF Statemet - Block Entry True Test expression? False Statement-x Next Statement
  • 5.
    W.A.P to displaya number if user enters negative number If user enters positive number, that number won't be displayed intmain() { int number; printf("Enter an integer:"); scanf("%d", &number); // Testexpression is true if number is less than 0 if (number < 0) { printf("You entered %d.n", number); } printf(“Enter positive number."); return0; } Output1 Enter an integer:-2 You entered -2. Enter Positive number
  • 6.
    W.A.P to checkwhether 2 nos are equal
  • 7.
    IF – ELSESTATEMENT The if….elsestatement isan extensionof the simple if statement. The general formis: if(test-expression) { True-block statement(s); } else { False-block statement(s); } Statement-x;
  • 8.
    FLOWCHART OF IF- ELSE True-Block Statements Entry Test expression? TrueFalse Statement-x Next Statement False-Block Statements
  • 9.
    W.A.P to checkwhether 2 nos are equal or not
  • 10.
    W.A.P whether givennum is even or odd
  • 11.
    W.A.P to checkwhether person is eligible for voting or not
  • 12.
    Program to checknumber is divisible by 5 and 11 int main() { int num; /* Reads number from user */ printf("Enter any number: "); scanf("%d", &num); if((num % 5 == 0) && (num % 11 == 0)) { printf("Number is divisible by 5 and 11"); } else { printf("Number is not divisible by 5 and 11"); } return 0; }
  • 13.
    Program to findmaximum of 2 nos intmain() { int num1, num2; printf("Enter two numbers:"); scanf("%d%d", &num1, &num2); if(num1 > num2) { // True part means num1 >num2 printf("%d is maximum", num1); } else { // False part means num1 <num2 printf("%d is maximum", num2); } return0; }
  • 14.
    NESTED IF -ELSE if(test condition 1) { if(test condition 2) { statement-1; } else { statement-2; } } else { statement-3; } Statement-x;
  • 15.
    FLOW CHART OFNESTED IF - ELSE Test condition 1? Entry True False Statement-3 Test condition 2? Statement-1 Statement-x Next Statement Statement-2 TrueFalse
  • 16.
    W.A.P to checkwhether two numbers are >,<,= or != int var1, var2; printf("Input the value of var1:"); scanf("%d", &var1); printf("Input the value of var2:"); scanf("%d",&var2); if (var1 !=var2) { printf("var1 is not equal to var2"); //Below – if-else is nested inside another if block if (var1 >var2) { printf("var1 is greater than var2"); } else { printf("var2 is greater than var1"); } } else { printf("var1 is equal to var2"); }
  • 17.
    W.A.P to findmaximum of 3 nos int main() { int num1, num2, num3, maximum; printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2) { if(num1 > num3) { maximum = num1; } else { maximum = num3; } } else { if(num2 > num3) { maximum = num2; } else { maximum = num3; } } printf("Maximum among all three numbers = %d", maximum); return 0; }
  • 18.
    W.A.P to readthree values from keyboard and print out the largest of them without using if statement. void main() { int x,y,z; clrscr(); printf("Enter Three Numbers:--n"); scanf("%d %d %d",&x,&y,&z); ((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :-- %d",y):printf("Largest is z :-- %d",z); } Output:-- Enter Three Numbers:-- 3 4 5 Largest is z :-- 5
  • 19.
    ELSE – IFLADDER if (test condition 1) { statement –1 ; } elseif (test condition 2) { statement –2 ; } elseif (test condition 3) { statement – 3 ; } ------------- elseif (test condition n) { statement – n ; } else { default-statement; } Statement – x;
  • 20.
    FLOWCHART OF ELSE-IFLADDER Condition-2 Condition-n Statement-1 Statement-2 Statement-n Default-statement Statement-x True True True False Condition-1 False False
  • 21.
    Program to checkwhether 2 nos are =,<,>,!=main(){ int var1,var2; printf("Input the value ofvar1:"); scanf("%d", &var1); printf("Input the value ofvar2:"); scanf("%d",&var2); if (var1 ==var2) { printf("var1 is equal tovar2"); } else if (var1 >var2) { printf("var1 is greater thanvar2"); } else if (var2 > var1) { printf("var2 is greater thanvar1"); } else { printf("var1 is not equal tovar2"); }}
  • 22.
    Write a Cprogram to check whether given character is a digit, lowercase alphabet or an uppercase alphabet. void main() { charch; clrscr(); printf("Enter the character:"); scanf("%c",&ch); if((ch>='0')&&(ch<='9')) printf("the number is %cdigit",ch); else if((ch>='a')&&(ch<='z')) printf("the character is %clowercase",ch); else if((ch>='A')&&(ch<='Z')) printf("the character is %cuppercase",ch); else printf("thecharacter is %cothertype"); getch(); } Output: Enter the character:7 the number is 7 digit Enter thecharacter:R the character is Ruppercase
  • 23.
    SWITCH STATEMENT Switch (expression ) //expression is any integerexpression orcharacters { case value-1 : //valuesare constantsorconstantexpressionsevaluableto an integral constant block-1; break; case value-2 : default: block-2; break; //default is optional..executed when nocasevalue matches default-block; break; } Statement-x;
  • 24.
    Implement a Calculatorusing Switch Case void main() { int a, b, c, d, e, f, ch; printf("Enter the first no. :"); scanf("%d", &a); printf("Enter the second no. :"); scanf("%d", &b); printf("Enterthechoiceas n 1.Addn 2.Sub n 3.Multiplyn 4.Dividen"); scanf("%d", &ch); switch(ch) { case1: c=a+b; printf("%d",c); break; case2: d=a-b; printf("%d",d); break; case 3: e=a*b; printf("%d", e); break; case 4: f=a/b; printf("%d", f); break; default: printf("Wrong Input"); break; } getch(); }
  • 25.
    Program to findnumber of days in a month using switch case int main() { int month; printf("Enter month number(1-12): "); scanf("%d", &month); switch(month) { case1: case3: case5: case7: case8: case10: case 12: printf("31days"); break; case 4: case 6: case 9: case 11: printf("30 days"); break; case 2: printf("28/29 days"); break; default: printf("Invalid input! Please enter month number between 1-12"); } return 0; }
  • 26.
    GOTO STATEMENT If youwant to jumpsome wherewithoutchecking any condition thengoto is used. statements; gotolabel1; Statements; lable1 : statements; statements; label1 : statements; goto label1; Statements;
  • 27.
    GoTo Example intmain() { intage; Vote: printf("you areeligible forvoting"); NoVote: printf("you are not eligibletovote"); printf("Enter yourage:"); scanf("%d", &age); if(age>=18) gotoVote; else gotoNoVote; return 0; } Output: Enter your age: 23 you are eligible for voting Enter your age: 15 you are not eligible to vote