Sie sind auf Seite 1von 26

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : T.Sudhakar
Designation : Lecturer in Computer Engg.
Branch : Computer Engg.
Institute : S.S.Govt. Polytechnic, Zaheerabad
Year/Semester : III Semester
Subject : UNIX & C
Code : CM-304
Major Topic : Programming Constructs
Sub-Topic : Conditional Statements
Duration : 100 Min
Teaching Aids : PPT
CM304.45TO46 1
Recap

• What is the syntax of if…else ?

• What is the syntax of nested if ?

• What is the use of nested if ?

CM304.45TO46 2
Objectives

On completion of this period you would be able to


learn
• Conditional operator
• Else if ladder

CM304.45TO46 3
Conditional Operator or The ? : Operator

• Useful to make two-way decisions.


• Combination of ? and :
• It takes three operands.

CM304.45TO46 4
Conditional Operator or The ? : Operator

The general form is


Conditional Expression ? Expr1 : Expr2
The Conditional Expression is evaluated first.
If its value is true (1), value of Expr1 is returned
Else the value of Expr2 is returned

CM304.45TO46 5
Conditional Operator
For example, the segment
if(x<0)
flag=0;
else
flag=1;
Can be written as
flag = (x<0) ? 0 : 1;

CM304.45TO46 6
Conditional Operator or The ? : Operator

Consider the evaluation of the following


function
y = 1.5 x + 3 for x <= 2
y = 2x + 5 for x > 2

Using conditional operator


y = (x<=2) ? (1.5x + 3) : (2x + 5);

CM304.45TO46 7
Consider weekly salary of a salesman who sells
products.
If x is no. of products he sold in a week.
His weekly salary is given by
4x + 100 for x < 40
Salary = 300 for x = 40
4.5x + 150 for x > 40

Using Conditional operator

Salary = (x != 40) ? ((x < 40) ? 4x + 100 : 4.5x + 150) : 300

CM304.45TO46 8
Program to find larger between 2 numbers

DISPLAY
PROGRAM
#include<stdio.h> Enter a, b values:
main() 20 35
{
Larger is 35
int a, b, larger;
printf(“Enter a, b values:\n”);
scanf(“%d%d”,&a, &b);
a b larger
larger = (a>b) ? a : b;
printf(“Larger is %d”, larger); 20 35 35
}
Memory Contents
CM304.45TO46 9
Multiple Branching Statements

• They are used when there are more than two


choices and the control has to select one among
these choices.
• They are
1. Else..if Ladder
2. Switch statement

CM304.45TO46 10
Else..if Ladder
if(condition1)
statement-1;
else if(condition2)
statement-2;
else if(condition3)
statement-3;
...
...
else if(condition n)
statement-n;
else
default-statement;
statement-x;
CM304.45TO46 11
Else..if Ladder

F F F condition4
F
condition1 condition2 condition3

T T T T
statement
Statement-1 Statement-2 Statement-3 Statement-4

Default
statement
Statement-x

CM304.45TO46 12
Else..if Ladder Contd..

• In this construct, the conditions are evaluated


downwards from top (of the ladder)
• As soon as true condition is found, statements
associated with it are executed and the control
is transferred to statement-x (skipping rest of
ladder)
• If n conditions are false, final else containing
the default-statement will be executed.

CM304.45TO46 13
Example

To determine grading of the student depending on the marks


he has got. The rules for the grading

Average Marks Grade


70 to 100 Distinction
60 to 69 First Division
50 to 59 Second Division
35 to 49 Third Division
0 to 34 Fail

CM304.45TO46 14
#include<stdio.h> DISPLAY
main()
Enter average marks:
{
int marks; 75
printf(“Enter average marks:\n”);
Distinction
scanf(“%d”,&marks);
if(marks >= 70)
printf(“Distinction”);
else if(marks >= 60)
printf(“First Division”);
else if(marks >= 50)
printf(“Second Division”); marks
else if(marks >=35)
printf(“Third Division”);
75
else printf(“fail”);
Memory Contents
}

CM304.45TO46 15
#include<stdio.h> DISPLAY
main()
Enter average marks:
{
int marks; 45
printf(“Enter average marks:\n”);
Third Division
scanf(“%d”,&marks);
if(marks >= 70)
printf(“Distinction”);
else if(marks >= 60)
printf(“First Division”);
else if(marks >= 50)
printf(“Second Division”); marks
else if(marks >=35)
printf(“Third Division”);
45
else printf(“fail”);
Memory Contents
}

CM304.45TO46 16
#include<stdio.h> DISPLAY
main()
Enter average marks:
{
int marks; 22
printf(“Enter average marks:\n”);
fail
scanf(“%d”,&marks);
if(marks >= 70)
printf(“Distinction”);
else if(marks >= 60)
printf(“First Division”);
else if(marks >= 50)
printf(“Second Division”); marks
else if(marks >=35)
printf(“Third Division”);
22
else printf(“fail”);
Memory Contents
}

CM304.45TO46 17
Example

An Electric Power Distribution company charges its


domestic consumers as follows

Consumption Units Rate of charge


0 - 200 Rs. 0.50 per unit
201 – 400 Rs 100 + Rs. 0.65
per unit excess 200
401 - 600 Rs.230 + Rs. 0.80
per unit excess 400
601 and above Rs.390 + Rs 1.00
per unit excess 600
CM304.45TO46 18
#include<stdio.h> DISPLAY
main()
{ Enter units consumed:
int units;
100
float charges;
printf(“Enter units consumed:\n”); The charges are Rs. 50.0
scanf(“%d”,&units);
if(units > 600)
charges = 390 + (units – 600);
else if(units > 400)
charges = 230 + 0.80 * (units – 400);
else if(units > 200)
charges = 100 + 0.65 * (units – 200);
units charges
else
charges = 0.50 * units;
100 50.0
printf(“The charges are Rs. %f”, charges);
}
Memory Contents

CM304.45TO46 19
#include<stdio.h> DISPLAY
main()
{
Enter units consumed:
int units; 410
float charges;
printf(“Enter units consumed:\n”); The charges are Rs. 238.0
scanf(“%d”,&units);
if(units > 600)
charges = 390 + (units – 600);
else if(units > 400)
charges = 230 + 0.80 * (units – 400);
else if(units > 200)
units charges
charges = 100 + 0.65 * (units – 200);
else
410 238.0
charges = 0.50 * units;
printf(“The charges are Rs. %f”, charges);
Memory Contents
}

CM304.45TO46 20
#include<stdio.h> DISPLAY
main()
{
Enter units consumed:
int units; 210
float charges;
printf(“Enter units consumed:\n”); The charges are Rs. 106.5
scanf(“%d”,&units);
if(units > 600)
charges = 390 + (units – 600);
else if(units > 400)
charges = 230 + 0.80 * (units – 400);
else if(units > 200)
units charges
charges = 100 + 0.65 * (units – 200);
else
210 106.5
charges = 0.50 * units;
printf(“The charges are Rs. %f”, charges);
Memory Contents
}

CM304.45TO46 21
Summary
In this class, we have discussed about…

• Conditional Operator
• Else..if Ladder

CM304.45TO46 22
Quiz

1. Write the following Else if ladder code using nested if

--
--
if(code == 1)
colour = “RED”;
else if(code == 2)
colour = “GREEN”;
else if(code == 3)
colour = “WHITE”);
else
colour = “YELLOW”;
--
-- CM304.45TO46 23
if(code != 1)
{
if(code != 2)
{
if(code != 3)
colour = “YELLOW”;
else
colour = “WHITE”;
}
else
colour = “GREEN”;
}
else
colour = “RED”;
CM304.45TO46 24
2. if (number > 100 || number < 0)
printf(“out of range”);
else
sum = sum + number;

Ans:- if (number > 100 )


printf(“out of range”);
else if (number < 0)
printf(:out of range”);
else
sum = sum + number;

CM304.45TO46 25
Frequently asked Questions

1. Explain Conditional operator.

3. Write a c Program to find the larger between two


given numbers using conditional Operator?

5. Explain Else..if Ladder.

CM304.45TO46 26

Das könnte Ihnen auch gefallen