0% found this document useful (0 votes)
24 views17 pages

Chapter 2 Decision Making

The document discusses decision control instructions in C programs using if, if-else, and conditional operators. It provides examples of if and if-else statements, explaining how to evaluate conditions using relational operators and demonstrating their use in sample programs to calculate discounts and bonuses based on certain criteria.

Uploaded by

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

Chapter 2 Decision Making

The document discusses decision control instructions in C programs using if, if-else, and conditional operators. It provides examples of if and if-else statements, explaining how to evaluate conditions using relational operators and demonstrating their use in sample programs to calculate discounts and bonuses based on certain criteria.

Uploaded by

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

What is decision?

• Many a times, we want a set of instructions to be executed in one


situation, and an entirely different set of instructions to be executed
in another situation.
• This kind of situation is dealt in C programs using a decision
control instruction.
Decision control instruction can be implemented in C using:

• The if statement
• The if-else statement
• The conditional operators
• C uses the keyword if to implement the decision control
instruction. The general form of if statement looks like this:
• if ( this condition is true )
execute this statement ;
• The keyword if tells the compiler that what follows is a
decision control instruction. The condition following the
keyword if is always enclosed within a pair of parentheses.
• If the condition, whatever it is, is true, then the statement is
executed.
• If the condition is not true then the statement is not
executed; instead the program skips past it.
How to evaluate condition?

• As a general rule, we express a condition using C’s ‘relational’


operators.
• The relational operators allow us to compare two values to see
whether they are equal to each other, unequal, or whether one is
greater than the other.
How relational operators are evaluated in C?
Demonstration of If Statement
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Flow Chart Decision Statement
Example: If-Else

• Example : While purchasing certain items, a discount of 10% is


offered if the quantity purchased is more than 1000. If quantity and
price per item are input through the keyboard, write a program to
calculate the total expenses?
Flowchart for Problem
C Program
main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
Interaction with discussed C Program

• Enter quantity and rate 1200 15.50


• Total expenses = Rs. 16740.000000

• Enter quantity and rate 200 15.50


• Total expenses = Rs. 3100.000000
Multiple Statements Within If

• It may so happen that in a program we want more than one


statement to be executed if the expression following if is satisfied.
• If such multiple statements are to be executed then they must be
placed within a pair of braces
Example: The current year and the year in which the employee
joined the organization are entered through the keyboard. If
the number of years for which the employee has served the
organization is greater than 3 then a bonus of Rs. 2500/- is given to
the employee. If the years of service are not greater than 3, then the
program should do nothing.
Program
Flowchart
If-else Statement

• The if statement by itself will execute a single statement, or a group


of statements, when the expression following if evaluates to true.
• It does nothing when the expression evaluates to false.
• We can execute one group of statements if the expression evaluates
to true and another group of statements if the expression evaluates
to false.
• This is the purpose of the else statement.

You might also like