You are on page 1of 24

Decision Making

Statements
if
if-else
if-else-if
switch-case
Introduction to Decision Making Statements
Used to have a program execute different
statements depending on certain conditions. In a
sense, makes a program “smarter” by allowing
different choices to be made. In C, there are three
decision making statements.
if :execute a statement or not.
if-else :choose to execute one of two statements.
switch :choose to execute one of a number of
statements.
if Statement
The if statement allows branching (decision making) depending upon a
condition. Program code is executed or skipped. The basic syntax is
if( condition)
{
statement(s)
}
Here if condition is true then only statement(s) in { } will be executed. In other
words you can say that control will reach in { } only after checking condition
and finding it true.
/* if evaluated expression is not 0 */
if (expression)
{
/* then execute this block */
}
/* Continue other operations in sequence*/
/*Example program to test whether a given number is
greater than 10 */
#include <stdio.h>
int main(void)
{
int num = 20; /* initialize num by 20*/
if( num > 10)
{
printf(“The number is greater than 10\n”);
}
printf(“The number is:%d”,num);
return 0;
}
The output of this program will be:
The number is greater than 10
The number is:20
• Write a program to accept a score. If this
score is less than 75 then display “Fail”,
otherwise display “Pass”.
if –else statement
This statement is used if you have to choose one from
two given set of statement(s). The structure of if-else is
if(condition)
{
/* if condition is true this block will execute*/
statement(s) C Programming Language
}
else
{
/* condition is false this block will execute */
statement(s)
}
if-else also work similar to if statement.
The only difference between if and if-else
is that in if after finding condition true a
set of statement(s) is executed and in if-
else for true value of condition one set of
statement(s) is executed and for false
value of condition another set of
statement(s) is executed.
Below is the flowchart for if-else
statement.
You can say that to decide between two courses of
action if-else is used. For example to decide whether a
student has passed an exam with a pass mark of 45 you
can write c code:
/*
if (Marks >= 45)
{
printf("The Student is Pass\n");
}
else
{
printf("The Student is Fail\n");
}
If-else if-else
This statement is also known as ladder if
statement. Flowchart for if-else if-else
is given below.
This conditional statement is used in situations where
you wish to make a multi-way decision based on
certain conditions. The way of doing such type of
testing is by using several else-if statements.
This works by cascading more than one comparison. As
soon as a comparison gives a true result, the following
statement(s) or block is executed, and after that no
further comparisons are performed.
You have already seen one example of comparison
where you found whether a student is pass or not.
Suppose you have find the grade of a student on the
basis of marks of the student then you need to do
many comparison. In the following example we will
find the grades depending on the exam result.
if (Marks >= 75)
{
printf("Passed and Grade is A\n");
}
else if (Marks >= 60)
{
printf("Passed and Grade is B\n");
}
else if (Marks >= 45)
{
printf("Passed and Grade is C\n");
}
else
{
printf("Failed\n");
}
In this example, all comparisons are testing a single
variable called Marks. There may be cases where
you have to do multiple comparisons on more than
one variable. While doing programming the same
pattern can be used with more or fewer else if's.
Note that finally only else may be left out as last
block.
• Write a program which prompt user to enter
an integer number so as to print “Hello, I love
C programming” only when the number is less
than 10 and display the message “This is
greater than 10” when it is otherwise {Use if-
else statement}.
• Program to find greatest in three numbers.
• Program to find if the given number is even or
odd
switch Statement
The switch statement is a better way of writing a program which employs an if-else
ladder. It is C’s built-in multiple branch decision statement.
The syntax for the switch statement is as follows:
switch (integer expression) {
case constant1:
statement1;
break;
case constant2:
statement2;
break;
...
default:
statement;
}
The keyword break should be included at the end of each case statement. In
general, whenever a break statement is encountered in C, it interrupts the normal
flow of control. In the switch statement, it causes an exit from the switch shunt.
The default clause is optional. The right brace at the end marks the end of switch
statement.
switch Statement Example
Here is a simple example of a switch statement:
switch(n) {
case 12:
printf("value is 12\n");
break;
case 25:
printf("value is 25\n");
break;
case 99:
printf("value is 99\n");
break;
default:
printf("number is not part of the Xmas date\n");
}
switch Statement Example: Menus
A common application of the switch statement is to control
menu-driven software:
switch(choice) {
case 'S':
check_spelling();
break;
case 'C':
correct_errors();
break;
case 'D':
display_errors();
break;
default:
printf("Not a valid option\n"); }
/* Write the c program that prompt user to select a day from day 1 to 7 and
display the selected day*/
#include <stdio.h>
main()
{
int day;
printf(“Please enter a single digit for a day\n”);
printf(“(within the range of 1 to 7):\n”);
day = getchar();
switch (day){
case ‘1’:
printf(“Day 1 is Sunday.\n”);
break;
case ‘2’:
printf(“Day 2 is Monday.\n”);
break;
case ‘3’:
printf(“Day 3 is Tuesday.\n”);
break;
case ‘4’:
printf(“Day 4 is Wednesday.\n”);
break;
case ‘5’:
printf(“Day 5 is Thursday.\n”);
break;
case ‘6’:
printf(“Day 6 is Friday.\n”);
break;
case ‘7’:
printf(“Day 7 is Saturday.\n”);
break;
default:
printf(“The digit is not within the range of 1 to 7.\n”);
break;
}
return 0;
}
OUTPUT:
Please enter a single digit for a day
(within the range of 1 to 7):
1
Day 1 is Sunday.
• Write a program to accept a number. Use the case
statement to display “Tanzania” when number is 1,
“Kenya” when number is 2, and “Kenya”, when
number is 3. Display “That is not correct” if number
wasn’t any of the above.
• Using switch–case statements write a program that
compute the area of a circle, the area of a rectangle
and the area of a triangle. Let the program prompt
the user to enter dimensions, perform calculations
and print the result on each case.

You might also like