0% found this document useful (0 votes)
71 views7 pages

LM - Unit3 2

Uploaded by

raisinghdaksh2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views7 pages

LM - Unit3 2

Uploaded by

raisinghdaksh2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT-3: Decision Making statements :

SELECTION/DECISION CONTROL STATEMENTS


In a C program, a decision causes a one-time jump to a different part of the program, depending on the
value of an expression. Decisions in C can be made in several ways.
The most important is with the if...else statement, which chooses between two alternatives. This
statement can be used without the else, as a simple if statement.
Another decision control statement, switch, creates branches for multiple alternative sections
of code, depending on the value of a single variable.
The if Statement
It is used to execute an instruction or sequence/block of instructions only if a condition is satisfied. In
if statements, expression is evaluated first and then, depending on whether the value of the expression
(relation or condition) is “true” or “false”, it transfers the control to a particular statement or a group
of statements.
Different forms of implementation if-statement are:
• Simple if statement
• If-else statement
• Nested if-else statement
• If..Else if statement(If ladder)
Simple if statement
It is used to execute an instruction or block of instructions only if a condition is satisfied.
Syntax:
if (condition)
statement;
If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the
program continues on the next instruction after the conditional statement.
This is shown in the Figure 5.1 given below:
Flowchart:

Condition?

True false

statement

exit

If we want more than one statement to be executed, then we can specify a block of statements within
the curly brackets { }.
syntax :
if (condition)
{
block of statements;
}
Example
Write a program to calculate the net salary of an employee, if a tax of 15% is levied
on his gross-salary if it exceeds Rs. 10,000/- per month.
/*Program to calculate the net salary of an employee */
#include <stdio.h>
main( )
{
float gross_salary, net_salary;
printf(“Enter gross salary of an employee\n”);
scanf(“%f ”,&gross_salary );
if (gross_salary <10000)
net_salary= gross_salary;
if (gross_salary >= 10000)
net_salary = gross_salary- 0.15*gross_salary;
printf(“\nNet salary is Rs.%.2f\n”, net_salary);
}

OUTPUT
Enter gross salary of an employee
9000
Net salary is Rs.9000.00
Enter gross salary of any employee
10000
Net salary is Rs. 8500.00

If … else statement
If…else statement is used when a different sequence of instructions is to be executed depending on the
logical value (True / False) of the condition evaluated.
syntax:
if (condition)
Statement _1;
else
Statement_ 2;
statement_3;
Or
if (condition)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
Statements _3_Block;
If the condition is true, then the sequence of statements (Statements_1_Block) executes; otherwise the
Statements_2_Block following the else part of if-else statement will get executed. In both the cases, the
control is then transferred to Statements_3 to follow sequential execution of the program.
Flowchart :

Condition?

True false

Statement 1 block Statement 2 block

Statement 3 block
Example
Write a program to print whether the given number is even or odd.
/* Program to print whether the given number is even or odd*/
#include <stdio.h>
main ( )
{
int x;
printf(“Enter a number:\n”);
scanf("%d",&x);
if (x % 2 == 0)
printf(“\nGiven number is even\n”);
else
printf(“\nGiven number is odd\n”);
}
OUTPUT
Enter a number:
6
Given number is even
Enter a number
7
Given number is odd
Nested if…else statement
In nested if… else statement, an entire if…else construct is written within either the body of the if
statement or the body of an else statement.
syntax :
if (condition_1)
{
if (condition_2)
{
Statements_1_Block;
}
else
{
Statements_2_Block;
}
}
else
{
Statements_3_Block;
}
Statement_4_Block;
• Here, condition_1 is evaluated. If it is false then Statements_3_Block is executed and is
followed by the execution of Statements_4_Block
• If condition_1 is true, then condition_2 is evaluated.
• If condition_1 and condition _2 both are true Statements_1_Block is executed otherwise
condition_2 is false so Statements_2_Block is executed and then the control is transferred to
Statements_4_Block.
Flowchart:

Condition? false

true
Condition? false

Statement 1 block Statement 2 block Statement 3 block


true

Statement 4 block

Example
Write a program to calculate an Air ticket fare after discount, given the following
conditions:
• If passenger is below 14 years then there is 50% discount on fare
• If passenger is above 50 years then there is 20% discount on fare
• If passenger is above 14 and below 50 then there is 10% discount on fare.
/* Program to calculate an Air ticket fare after discount */
#include <stdio.h>
main( )
{
int age;
float fare;
printf(“\n Enter the age of passenger:\n”);
scanf(“%d”,&age);
printf(“\n Enter the Air ticket fare\n”);
scanf(“%f”,&fare);
if (age < 14)
fare = fare - 0.5 * fare;
else
if (age <= 50)
{
fare = fare - 0.1 * fare;
}
else
{
fare = fare - 0.2 * fare;
}
printf(“\n Air ticket fare to be charged after discount is %.2f”,fare);
}
OUTPUT
Enter the age of passenger
12
Enter the Air ticket fare
2000.00
Air ticket fare to be charged after discount is 1000.00

If ..Else.. if statement(IF ladder)


To show a multi-way decision based on several conditions, we use the else if statement. This works by
cascading of several comparisons. As soon as one of the conditions is true, the statement or block of
statements following them is executed and no further comparisons are performed.
syntax:
if (condition_1)
{
Statements_1_Block;
}
else if (condition_2)
{
Statements_2_Block;
}
------------
else if (condition_n)
{
Statements_n_Block;
}
else
Statements_x;

Here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to
true, then the statement associated with the given condition is executed and control is transferred to
Statements_x skipping the rest of the conditions following it. But if all conditions evaluate false, then
the statement following final else is executed followed by the execution of Statements_x.

Condition_1

false

Condition_2

True false

Condition_n

false
Statement_1_block True
Statement_x

True
Statement_1_block

Statement_1_block
Example
Write a program to award grades to students depending upon the criteria mentioned
below:
• Marks less than or equal to 50 are given “D” grade
• Marks above 50 but below 60 are given “C” grade
• Marks between 60 to 75 are given “B” grade
• Marks greater than 75 are given “A” grade.
/* Program to award grades */
#include <stdio.h>
main()
{
int result;
printf("Enter the total marks of a student:\n");
scanf("%d",&result);
if (result <= 50)
printf("Grade D\n");
else if (result <= 60)
printf("Grade C\n");
else if (result <= 75)
printf("Grade B\n");
else
printf("Grade A\n");
}

OUTPUT
Enter the total marks of a student:
80

The Switch Statement


Its objective is to check several possible constant values for an expression, something similar
to what we had studied in the earlier sections, with the linking of several if and else if statements. When
the actions to be taken depending on the value of control variable, are large in number, then the use of
control structure Nested if…else makes the program complex. There switch statement can be used. Its
form is the following:
switch (expression)
{
case expression 1:
block of instructions 1
break;
case expression 2:
block of instructions 2
break;
.
.
default:
default block of instructions
}
It works in the following way: switch evaluates expression and checks if it is equivalent to
expression1. If it is, it executes block of instructions 1 until it finds the break keyword, moment at finds
the control will go to the end of the switch. If expression was not equal to expression 1 it will check
whether expression is equivalent to expression 2. If it is, it will execute block of instructions 2 until it
finds the break keyword.
Finally, if the value of expression has not matched any of the previously specified constants
(you may specify as many case statements as values you want to check), the program will execute the
instructions included in the default: section, if it exists, as it is an optional statement.

Example 5.5
Write a program that performs the following, depending upon the choice selected by
the user.
i). calculate the square of number if choice is 1
ii). calculate the cube of number if choice is 2 and 4
iii). calculate the cube of the given number if choice is 3
iv). otherwise print the number as it is
main()
{
int choice,n;
printf(“\n Enter any number:\n “);
scanf(“%d”,&n);
printf(“Choice is as follows:\n\n”);
printf(“1. To find square of the number\n”);
printf(“2. To find square-root of the number\n”);
printf(“3. To find cube of a number\n”);
printf(“4. To find the square-root of the number\n\n”);
printf(“Enter your choice:\n”);
scanf(“%d”,&choice);
switch (choice)
{
case 1 : printf(“The square of the number is %d\n”,n*n);
break;
case 2 :
case 4 : printf(“The square-root of the given number is %f”,sqrt(n));
break;
case 3: printf(“ The cube of the given number is %d”,n*n*n);
default : printf(“The number you had given is %d”,n);
break;
}
}
OUTPUT
Enter any number:
4
Choice is as follows:
1. To find square of the number
2. To find square-root of the number\n");
3. To find cube of a number
4. To find the square-root of the number
Enter your choice:
2
The square-root of the given number is 2

You might also like