Decision Control Structure
• Selection Structure
• Conditional
If statement provide one option to choose
If statement with compound statements
If-else statement provides two options to choose from
Nested if-else statement if more then two selections are desired
If-else chains for multiple alternative
Switch statements better option for multiple choices
• Examples
Selection Control structure
Control structure control the flow of execution in a program or
function
In real life decision making process involve a comparative
assessment of some parameters or characteristics directly
or indirectly
Selection control structure is a control structure that chooses
among alternative program statements
Selection process in computer programming would involve
selection of certain statements or blocks of statements for
execution if one or more conditions are true and rejecting them
or choosing other statements for execution if condition are not
true
is an expression that is
if Statement either false(0) or true(1)
if (condition) F
condition
{
True-statement-block; T
}
True-Statement-block
next-statement;
int X,Y;
if(X%Y)
printf(“X is not divisible Next Statement
by Y”);
o Condition is a C expression, which evaluates to TRUE (non-zero)
or FALSE (zero)
o Statement-Block can be a single statement or compound statement
Example if Statements
Compound statements is a group
of statements bracketed by { and }
if (x <= 10) that are executed sequentially
y = x * x + 5;
if (x <= 10) {
y = x * x + 5; compound statement;
z = (2 * y) / 3; both executed if x <= 10
}
if (x <= 10)
only first statement is conditional
y = x * x + 5;
second statement is
z = (2 * y) / 3; always executed
More if Examples
if (0 <= age && age <= 11)
kids += 1;
if (month == 4 || month == 6 ||month == 9 || month == 11)
printf(“The month has 30 days.\n”);
if (x = 2)
y = 5; always true,
so y = 5 is always executed!
a common programming error (= instead of ==), not caught
by compiler because it’s syntactically correct.
if - else Statement
if (condition)
true block statement(s);
else T F
condition
false block statement(s);
True Block False Block
int X,Y; statement statement
if(X%Y)
printf(“X is not divisible
by Y”);
else
printf(“X is divisible
by Y”);
Note: If-else allows choice between two mutually exclusive
actions without re-testing condition.
If – else Statement example
if(i>j)
k = i++ + --j;
else
k = ++i + j--;
if(i>j && j>k)
{ i++;
j++;
}
else
{ k++;
j--;
}
Chaining if’s and else’s
if(test-1)
statement-1;
else if(test-2)
statement-2;
else if(test-3)
statement-3;
else if(test-4)
statement-4;
else
default-statement;
next_statement;
Example:
if (month == 4 || month == 6 || month == 9 ||
month == 11 )
printf(“Month has 30 days.\n”);
else if (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(“Month has 31 days.\n”);
else if (month == 2)
printf(“Month has 28 or 29 days.\n”);
else
printf(“Don’t know that month.\n”);
Program using if-else ladder
/* Checking a character whether it is capital letter or
small letter or special symbol*/
#include<stdio.h>
int main()
{
char ch;
printf("Enter a char:");
scanf("%c",&ch);
if(ch>='A' && ch<='Z')
printf("\n Capital letter");
else if(ch>='a' && ch<='z')
printf("\n Small letter");
else if(ch>='0' && ch<='9')
printf("\n Digit");
else
printf("\n Special Symbol");
return 0;
}
Nesting of if –else statements
if(test-1)
{ if(test-2)
{
statement(s)-1;
}
else
{
statement(s)-2;
}
}
else
{
statement(s)-3;
}
next_statement;
Nested if examples
if (x == 3)
if (y != 6) {
z = z + 1;
w = w + 2;
}
is same as...
if ((x == 3) && (y != 6))
{
z = z + 1;
w = w + 2;
}
Matching else with If
Else is always associated with closest unassociated if.
if (x != 10)
if (y > 3)
z = z / 2;
else
z = z * 2;
is the same as... is NOT the same as...
if (x != 10) if (x != 10) {
{ if (y > 3) if (y > 3)
z = z / 2; z = z / 2;
else }
z = z * 2; else
} z = z * 2;
Example: nesting of if-else
int main() /* program to find largest of the three numbers */
{ float a, b, c;
printf(“Enter three values\n”);
scanf(“%f %f %f”,&a, &b, &c);
printf(“Largest value is”);
if(a>b)
{ if(a>c)
printf(“%f”,a);
else
printf(“%f”,c);
}
else
{ if(c>b)
printf(“%f”,c);
else
printf(“%f”,b);
}
return 0;
}
14
If-else Points to remember
1. Basically If-else is a two - way decision statement.
2. An If-else statement provides a way of choosing whether or
not to execute a statement.
3. The else part of an if - else statement associates with the
nearest available if. This resolves the “dangling else”
problem.
15
Exercise: on if-else
1. Write a program to determine whether a number
is odd or even and print the appropriate
message.
2. Write a program to find the smallest of the
three numbers.
3. Write a program to determine whether the
inputted year is a leap year or not.
16
Switch
evaluate
expression
switch (expression)
{
case const1:
block-1; break;
= const1? block-1
T
F
case const2:
block-2; break;
= const2? block-2
default: T
block-3; F
}
block-3
next_statement
17
Example using switch
#include <stdio.h>
int main()
{ int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Example: Month’s have how many days
if (month == 4 || month == 6 || month == 9 ||
month == 11 )
printf(“Month has 30 days.\n”);
else if (month == 1 || month == 3 ||
month == 5 || month == 7 ||
month == 8 || month == 10 ||
month == 12)
printf(“Month has 31 days.\n”);
else if (month == 2)
printf(“Month has 28 or 29 days.\n”);
else
printf(“Don’t know that month.\n”);
Switch Example
/* same as month example for if-else */
switch (month) {
case 4:
case 6:
case 9:
case 11:
printf(“Month has 30 days.\n”);break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf(“Month has 31 days.\n”);break;
case 2:
printf(“Month has 28 or 29 days.\n”);break;
default:
printf(“Don’t know that month.\n”);
}
20
More About Switch
Case expressions must be constant.
case i: /* illegal if i is a variable */
If no break, then next case is also executed.
switch (a) {
case 1:
printf(“A”); If a is 1, prints “ABC”.
case 2: If a is 2, prints “BC”.
printf(“B”); Otherwise, prints “C”.
default:
printf(“C”);
}
21
switch points to remember
It is a multi way decision statement, so it is an alternative to
long if-else chain.
If break is not used, then case "falls through" to the next.
const-1 and const-2 are constants or constant expressions
also known as case labels.
Case labels must be unique and must end with colon.
block-1 and block-2 are statement lists; may contain zero
or more statements.
The default label is optional. It can be used anywhere
within switch.
The break statement transfers the control out of the switch.
22
goto Statement
To branch unconditionally from one location to
another location in the program.
Syntax: goto label;
label can be anywhere in the program either
before or after the goto statement.
Accordingly jump is called as forward jump or
backward jump.
23
Use of goto Statement
Consider the program segment.
label:
printf(“Enter a number:”);
scanf(“%d”,&num);
if(num==0)
goto label;
num = num/2;
Warning: always Avoid the use of
goto in your program !!!
24
Program using switch
#include<stdio.h>/* program for arithmetic operation */
int main()
{ float num1,num2;
char choice;
printf(“Enter two numbers”);
scanf(“%f %f”,&num1,&num2);
printf(“\nEnter your choice”);
printf(“\nEnter A for Addition”);
printf(“\nEnter B for Subtraction”);
printf(“\nEnter C for Division”);
printf(“\nEnter D for Multiplication”);
choice = getchar();
/* continue to next slide */
25
switch(choice){
case ‘A’:
res = num1+num2;
printf(“\n Addition of %f and %f” is %f”,num1,num2,res);
break;
case ‘B’:
res = num1-num2;
printf(“\n Sub of %f and %f” is %f”,num1,num2,res);
break;
case ‘C’: res = num1/num2;
printf(“\n Division of %f and %f” is %f”,num1,num2,res);
break;
case ‘D’:
res = num1*num2;
printf(“\n Mul of %f and %f” is %f”,num1,num2,res);
break;
default:
printf(“Not a valid Choice”);
}
return 0;
26
}
Note:
Remove all break statements from the program
and observe the output if
Choice = ‘A’ ??
Choice = ‘B’ ??
Choice = ‘C’ ??
Choice = ‘D’ ??
Assume num1 = 125.0 and num2 = 10.0
Programming Exercise
Write the previous program using if–else statement.
And observe which is convenient.
27
#include <stdio.h> Example
int main() {
int number=5;
switch (number)
{ case 1:
case 2:
case 3:
printf("One, Two, or Three.\n");
break;
case 4:
case 5:
case 6:
printf("Four, Five, or Six.\n");
break;
default:
printf("Greater than Six.\n");
}
}
Practice Home Task:
Write the program implementation for the decision diagrammed in the accompanying flow
diagram shown below using appropriate selection structure in C language.
Practice Home Task:
2. Write a program that read principal amount, rate of interest and no of days
for a loan and Computes the simple interest for the loan using following
formula.
Simple_Interest = Pincipal_Amount * Rate * Days / 365;
3. Write a program which reads humidity value in percentage(%) and display an
appropriate message as per the following table :
Humidity (%) Human Perception
30 or lower Dry
31-50 Comfortable
51-60 Annoying
61-80 Very annoying
above 80 Uncomfortable
Assume that in this problem only one condition is true at a time.
Question??
31