You are on page 1of 24

Lecture No.

4
Bilal Ashfaq Ahmed
In The Previous Lectures
 Basic structure of C program
 Variables and Data types
 Operators
 ‘cout’ and ‘cin’ for output and input
 Manipulators
Relational Operators
Selection/Conditional Statements
Decision Making Statements
IF Selection Structure
IF-ELSE Selection Structure

SWITCH Selection Structure


If Statement in C
The if statement has a simple structure.
if ( condition / expression )
Statement (or group of statements);

For Example: Suppose the passing grade on an exam is 60. The pseudo code statement

If student's grade is greater than or equal to 60


Print "Passed"
IF Statement in C
The preceding pseudo code If statement can be written
in C++ as

if ( grade >= 60 )
cout << "Passed";
IF Statement in C
Single Statement

if (condition)
Statement ;

Multiple Statement

if ( condition )
{
Statement1 ;
Statement2 ;
:
}
Example
Suppose, we have ages of two students (say for the
time being we have got these ages in variables). These
variables are age1 and age2. Now we say that if the
age1 is greater than age2, then display the statement
‘Student 1 is older than student 2’.
The coding for this program will be as below
#
#
void main()
{
int age 1,age2;
clrscr();
cout<<“Enter the age of first student”<<endl;
cin>>age1;
cout<<“Enter the age of Second student”<<endl;
cin>>age2;
if (age1 > age2)
cout << “Student 1 is older than student 2”;
getch();
}
Flow Charts
Description Symbol
Start / End

Processing

Input / Output

Decision/Condition

Connector

Flow lines

Stored Procedure

Comments …..

Off page / On page connectivity


IF Flow Chart

Boolean
Exp

Statement(s)

Rest of the
program
Examples Using two IF
Statement
/* This program checks the age of Amer and Amara and displays the
appropriate the message. The program is using two if statements.*/
# include <iostream.h>
#include<conio.h>
void main ( )
{
int AmerAge, AmaraAge;
clrscr();
//prompt the user to enter Amer’s age
cout << “Please enter Amer’s age “ ;
cin >> AmerAge;
//prompt the user to enter Amara’s age
cout << “Please enter Amara’s age “ ;
cin >> AmaraAge;
//perform the test
if (AmerAge > AmaraAge )
{
cout << “ Amer is older than Amara”;
}
if (AmerAge < AmaraAge )
{
cout << “ Amer is younger than Amara”;
}
}
Logical Operators
AND &&
OR ||
NOT !

If a is greater than b AND c is greater than d

IN C
if(a > b && d > c)
if (age>18 || height>5)
IF ELSE Statement
if ( condition)
{
statement(s);
}
else
{
statement(s);
}
IF ELSE Flow chart
IF Example
Code

if (AmirAge > AmaraAge)


{
cout<< “Amir is older than Amara” ;
}

if (AmirAge < AmaraAge)


{
cout<< “Amir is younger than Amara” ;
}
IF ELSE Example
Code

if AmirAge > AmaraAge)


{
cout<< “Amir is older than Amara” ;
}
else
{
cout<<“Amir is younger than Amara” ;
}
Example
If
The student age is greater than 19 or his height is greater than or
equal to six feet then put him on the basket ball team
Else
Put him on the football team

if (Age > 19 && Height >= 6)


cout<<“Student is selected Basketball Team ”<<endl;
else
cout<<“Student is selected Football Team ”<<endl;
TASK
/* Program that accepts a number from the user and prints EVEN if the number
is even (divisible by 2) else prints ODD. */

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<" Enter Number : “<<endl;
cin>>num;

if(num % 2 == 0)
cout<<“EVEN Number”<<endl;
else
cout<<“ODD Number”<<endl;

getch();
}
Conditional Operator
Symbol (?:)
Closely related to if–else statement
Only ternary operator in C++
Syntax:
(condition)? Statement A : Statement B;
Example
cout<<(num%2 == 0) ? “EVEN Number” : “ODD Number” ;
Nested IF Structure
if structure is used within an other if structure
Syntax
if ( condition-1 ) //outer if statement
{
if ( condition-2 ) //inner if statement
{
Statement(s) ;
}
}

Dangling else problem


Nested IF_ELSE Statements
if-else structure place within another if-else structure
Also known as if-else- if structure
Used for multiple selecton

if ( student Grade >= 90 ) // 90 and above gets "A"


cout << "A";
else if ( student Grade >= 80 ) // 80-89 gets "B“
cout << "B";
else if ( student Grade >= 70 ) // 70-79 gets "C"
cout << "C";
else if ( student Grade >= 60 ) // 60-69 gets "D“
cout << "D";
else // less than 60 gets "F"
cout << "F";
Task:
A shopkeeper announces a package for customers
that he will give 10 % discount on all bills and if a
bill amount is greater than 5000 then a discount of
15 %. Write a C program which takes amount of the
bill from user and calculates the payable amount by
applying the above discount criteria and display it
on the screen.

You might also like