0% found this document useful (0 votes)
164 views

II&CT Lecture 21 - 22 Programming Fundamentals in C++ (Conditinal Statements)

This document summarizes a lecture on conditional statements in C++ programming. It introduces conditional statements and how they can change the order of execution in a program. It defines relational expressions and operators that return true or false. It describes the "if" statement and how it executes code if a condition is true. It also covers the "if-else" statement, which allows for two blocks of code where one will execute depending on the condition. Examples are provided to demonstrate using conditional statements to check values, divisibility, and calculate electricity bills.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

II&CT Lecture 21 - 22 Programming Fundamentals in C++ (Conditinal Statements)

This document summarizes a lecture on conditional statements in C++ programming. It introduces conditional statements and how they can change the order of execution in a program. It defines relational expressions and operators that return true or false. It describes the "if" statement and how it executes code if a condition is true. It also covers the "if-else" statement, which allows for two blocks of code where one will execute depending on the condition. Examples are provided to demonstrate using conditional statements to check values, divisibility, and calculate electricity bills.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

INTRODUCTION TO ICT.

(Programming Fundamentals with C++)


Lecture # 21-22
By:
M.Nadeem Akhtar.
Department of CS & IT.

URL: https://sites.google.com/site/nadeemcsuoliict/home/lectures

1
OUTLINES
 Conditional statements
 Relational Expressions
 Relational operators
 The "if” statement
 The “if-else” statements
 The “nested if ”Statements
 The “nested if-else” structure
 The Conditional Operator
 Logical Operators
 Exercise
Conditional Statements:
 The statements of computer program are executed one after the
other in the order in which they are written.This is known as
sequential execution of the program.
 The order of execution of the statements in a program can be
changed.This is done with the help of conditional statements.
 conditional statements are also called selection statements.

Mr.Nadeem Akhtar 3
Relational Expressions
 A relational expression consists of constants, variables or arithmetic
expressions that are combined by a relational operator.
 A relational expression is written to find a relation between two
expressions.
 It returns only one value, which is either true or false.
 For example, 10>6 is a relational expression between two
constants and it returns a “true” value.

Mr.Nadeem Akhtar 4
Relational operators ( used to specify a relation
between two expressions or values.
Oper. Meaning / Description

> Greater than: used to test if one expression is greater than other
expression. i.e. if “e1” and “e2” are two expressions then:
e1>e2
If ‘e1’ is greater than ‘e2’ then this expression returns TRUE value.
If ‘e1’ is less than ‘e2’ then this expression returns FALSE value.
>= Greater than or equals to: It is used to test if one expression is greater
than or equal to other expression.
i.e. if “e1” and “e2” are two expressions then:
e1>=e2
If ‘e1’ is greater than or equal to‘e2’ then this expression returns TRUE value.
If ‘e1’ is less than ‘e2’ then this expression returns FALSE value.
< Less than: It is used to test if one expression is less than other expression.
i.e. if “e1” and “e2” are two expressions then:
e1<e2
If ‘e1’ is less than ‘e2’ then this expression returns TRUE value.
If ‘e1’ is greater than ‘e2’ then this expression returns FALSE value.
Mr.Nadeem Akhtar 5
Relational operators (Continue--)
Oper. Meaning / Description
<= Less than or equals to :It is used to test if one expression is less than or
equal to other expression.
i.e. if “e1” and “e2” are two expressions then:
e1<=e2
If ‘e1’ is less than or equal to‘e2’ then this expression returns TRUE value.
If ‘e1’ is greater than ‘e2’ then this expression returns FALSE value.
== Equals to :It is used to test if one expression is equal to other expression.
i.e. if “e1” and “e2” are two expressions then:
e1==e2
If ‘e1’ is equal to‘e2’ then this expression returns TRUE value.
If ‘e1’ is not equal ‘e2’ then this expression returns FALSE value.
!= Not Equal to: It is used to test if one expression is not equal to other
expression.
i.e. if “e1” and “e2” are two expressions then:
e1!=e2
If ‘e1’ is not equal to(less than or greater than)‘e2’ then this expression
returns TRUE value.
If ‘e1’ is equal ‘e2’ then this expression returns FALSE value.
Mr.Nadeem Akhtar 6
Example:
 If x=10, y=20 and z=5 then find out the out put of the following
relational expressions.

Relational Expression Output returned


X>y False
y<z False
y!=x True
X==z False
X<=y True
Z>=y False

Mr.Nadeem Akhtar 7
The”if”statement:
 The “if statement” is used to execute (or ignore) a set of statements after testing
condition.
 The “if statement” evaluates a condition. if the given condition is true, the statement (or a
set of statements) following the “if statement” is executed.
 If the given condition is false, the statement (or a set of statements) following the “if
statement” condition is ignored and the control transfer to the next statement.
 The syntax of the “if statement” is:
if(condition)
stetement-1;
stetement-2;
where condition: specifies a condition or relational expression.
 The syntax of the “if statement” for executing a set of statements is:
if (condition)
{ statement-1;
statement-2;
statement-3;
statement-m;
}
Statement-n;
In above syntax , the set of statements from 1 to m are enclosed in curly braces are called
compound statements.

Mr.Nadeem Akhtar 8
The”if”statement (Continue)
 If the condition given in the “if statement” is true, the compound statements given in braces are executed .
 If the condition is false the control shifts to the statement-n and the set of statement that follows the “if
statement” is ignored.
Program 02-01:
The following program executes a single statement if the given condition is true.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
Int a,b;
char q;
a=100;
b=50;
If(a>b)
cout<<“Islamabad”<<endl;
cout<<“ok”<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Program out put:


Islamabad
ok

Mr.Nadeem Akhtar 9
Program 02-02
 The following program executes a set of statements when the given condition is true:

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char q;
int n;
cin>>n;
if(n>10)
{
cout<<“Islamabad”<<endl;
cout<<“ok”<<endl;
cout<<"press any character to exit program =";
cin>>q;
}
return 0;
}

Mr.Nadeem Akhtar 10
Program 02-03
 Write a program to input a number. If the number is divisible by 3 then print the message on the screen that “The
number is divisible by 3”. Use block if statement.

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char q;
int n;
cout<<“enter a number?”;
cin>>n;
if(n%3==0)
{
cout<<“The number”<<n<<“ is divisible by 3\n\a”;
cout<<"press any character to exit program =";
cin>>q;
}

return 0;
}

Mr.Nadeem Akhtar 11
Program 02-04
 Write a program to input two integer values and find out whether the first
or the second number is greater .
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char q;
int a,b;
cout<<“enter first value?”;
cin>>a;
cout<<“enter second value?”;
cin>>b;
if(a>b)
cout<<“first value is greater”<<endl;
if(b>a)
cout<<”Second value is greater”<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 12
Program 02-05
 Write a program to calculate the electricity bill.The rates of electricity per unit are as follows:
1- If the unit consumed are equal or less than 300, then the cost is Rs.3/unit.
2- If the unit consumed are more than 300, then the cost is Rs.3.5/unit and a surcharge of 5% of
bill is added.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float cr,pr,units,bill;
char q;
cout<<“enter the current reading ?”;
cin>>cr;
cout<<“enter the previous reading ?”;
cin>>pr;
units=cr-pr;
if(units>300)
bill=units*3.5+units*0.05/100.0;

if(units<=300)
bill=units*3.0;
cout<<”Electricity bill =“<<bill;
cout<<"press any character to exit program =";
cin>>q;

return 0;
}

Mr.Nadeem Akhtar 13
The “if-else” statements:
 This is another form of the “if statement". it is used for making two way
decisions. In this statement, one condition and two blocks of statements
are given. Either one of the two blocks of the statements is executed after
evaluating a condition.
 the “if-else ” statement tests the given relational condition. if the condition
is true then the first block of statement is executed. If the condition is false
, the first block of statement is ignored and the second block following the
else is executed .
 The syntax of “if-else” statement is :
Syntax-1: if (condition)
statement-1;
else
statement-2;

Mr.Nadeem Akhtar 14
The “if-else” statements (continue--)

Syntax-2: in case of multiple or block of statement, the syntax is written as:


if (condition)
{ statement-1;
statement-2; First Block
statement-m;
}
else
{
statement-1;
statement-2; Second Block
statement-n;
}

Mr.Nadeem Akhtar 15
Program 02-07
 Write a program to input a number from the keyboard .Use if-
else statement to find out whether the number is less than or
greater than 100.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int n;
char q;
cout<<"enter an integer value ?";
cin>>n;
if(n>100)
cout<<"number is greater than 100"<<endl;
else
cout<<"number is less than 100"<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 16
Program 02-08
 Write a program to input two values from the keyboard and then to print the
larger number on the computer screen .
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b;
char q;
cout<<“enter first number ?”<<endl;
cin>>a;
cout<<“enter second number ?”<<endl;
cin>>b;

If(a>b)
cout<<“First number is greater ”;
else
cout<<” Second number is greater;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 17
Program 02-09
Write a program to calculate the net pay of an employee. Input the
basic pay and calculate the net pay as follows:
◦ House rent is 45% of the basic pay.
◦ Medical allowance is 2% of the basic if basic is greater than Rs. 5000/-.It is 5 %
of the basic pay if the pay is less than Rs. 5000/.
◦ Conveyance allowance is Rs. 96/-if basic pay is less than Rs. 5000/-. It is Rs.193/-
if the pay is more than Rs. 5000/-.
◦ Net pay is calculated by adding basic pay, medical allowance, conveyance
allowance and house rent.

Mr.Nadeem Akhtar 18
Program 02-09
Syntax/Code of above program is as follows:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float basic,ma,ca,net,hr;
char q;
cout<<“enter Basic Pay ?”;
cin>>basic;
If(basic>5000)
{
ma=basic*2/100.0;
ca=193.0;
hr=basic*45.0/100.0;
}
else
{
ma=basic*5.0/100.0;
ca=96.0;
hr=basic*45.0/100.0;
}
net=basic+ma+ca+hr;
cout<<” Net pay =“<<net<<endl;
cout<<"press any character to exit program =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 19
Program 02-10
 Write a program to input a number from the keyboard and then print the message “It is an odd
number” if it is an odd number. Else print message” it is an Even Number”. (A number is even if
it is divisible by 2).
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int n;
char q;
cout<<"enter an integer value ?";
cin>>n;
if(n%2==1)
cout<<"It is an odd number"<<endl;
else
cout<<" It is an Even number"<<endl;
cout<<"press any character to exit =";
cin>>q;

return 0;
}

Mr.Nadeem Akhtar 20
Program 02-11
 Write a program to input two integers and then to find out
whether these numbers are equal or different.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b;
char q;
cout<<“Enter first value ?";
cin>>a;
cout<<“Enter Second value ?";
cin>>b;
if(a==b)
cout<<“Both values are equal"<<endl;
else
cout<<" Values are different"<<endl;
cout<<"press any character to exit =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 21
The “nested if ”Statements:
 When an “if statement” is used within another “if statement” it is called the
“nested if statement”.The “nested if statement” is used for multi-way decision
making.
 The syntax of “nested if statement ”is:

{ if(condition-1)
{ if(condition-2)
{ statement-2;
}
statement-3; } }
 In above syntax , if condition-1 is true then the control shifts to the next “if
statement” and the condition-2 is tested.
 If the condition-2 is true then statement-2 is executed.The control will
then pass to statement-3 and this statement will be executed.
 If condition-2 is false, then statement-2 is skipped and the control shifts to
statement-3 and it will be executed.
 The “if statement” for testing condition-2 is within another ‘if statement.
This “if statement” is called the “nested if statement”.

Mr.Nadeem Akhtar 22
Flow Chart : The nested –if statement

FALSE TRUE
Cond-1

FALSE TRUE
Cond-2

Execute Execute
stat-3 stat-2

Next
statement

Mr.Nadeem Akhtar 23
Program 02-12
 Write a program to input three integer values. Compare the three values to find out if they are equal. Use “nested if statement” and
print the message “All values are equal” if they all are equal. Otherwise print the message “These values are different”.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
char q;
cout<<“Enter first value ?";
cin>>a;
cout<<“Enter Second value ?";
cin>>b;
cout<<“Enter third value ?";
cin>>c;
if(a==b)
{ if(a==c)
cout<<“All values are equal"<<endl;
}
else
cout<<" These values are different"<<endl;
cout<<"press any character to exit =";
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 24
Program 02-13
 Write a program to input three integer values from the keyboard. Find out the greatest number
and print it on the screen.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
char q;
cout<<“Enter first no. ?";
cin>>a;
cout<<“Enter Second no. ?";
cin>>b;
cout<<“Enter third no. ?";
cin>>c;
if(a>b)
if(a>c)
cout<<“1 St number is greater"<<endl;
else
cout<<" Third number is greater"<<endl;
else
if(b>c)
cout<<“2 nd number is greater“<<endl;
else
cout<<“ 3 rd number is greater”<<endl;
cout<<“Press any character to exit the program =”<<endl;
cin>>q;
return 0;
}
Mr.Nadeem Akhtar 25
The “nested if-else” structure
 When an “if-else ”structure is placed in other “if-else” structure, it is called
“nested if-else” structure. It is used for multiple selection.
 Its general syntax is:
if(condition-1)
statement-1;
else if (condition-2)
statement-2;
else if (condition-3)
statement-3;
-----------------------
--------------------------
else if (condition-m)
statement-m;
else
statement-n;

Mr.Nadeem Akhtar 26
Flow Chart : The “nested –if else” statement

TRUE
Cond
Block

FALSE TRUE

TRUE

Cond Block

FALSE

statements after if-else


strructure

Mr.Nadeem Akhtar 27
Program 02-15
 Write a program to perform simple arithmetic operation by using “nested if-else” structure.

#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int a,b;
char q,op;
cout<<"Enter 1st integer, operator & 2nd integer\n";
cout<<"Press Enter Key";
cin>>a>>op>>b;
if(op=='+')
cout<<"Addition ="<<(a+b)<<endl;
else if(op=='-')
cout<<"Subtraction ="<<(a-b)<<endl;
else if(op=='*')
cout<<"Multiplication ="<<(a*b)<<endl;
else if(op=='/')
cout<<"Division ="<<(a/b)<<endl;
else if(op=='%')
cout<<"Remainder ="<<(a%b)<<endl;
else
cout<<" Invalid Input"<<endl;
cout<<"Press any character to exit the program ="<<endl;
cin>>q;
return 0; }
Mr.Nadeem Akhtar 28
The Conditional Operator
 The conditional operator is used as an alternative of a simple if-else statement.
 The conditional operator consists of “?” (Question Mark) and a “:” (colon).Its syntax is:
{condition}?{Exp1}:{Exp2}
Where
condition = Represents test condition. If this condition is true
then the value of “exp1” is returned after evaluation.
otherwise the value of “exp2” is returned.
exp1 & exp2 =represents two expressions. It may be arithmetic
expression or constant values. Input/output
statements can be used.
 For example, if a=10 and b=5 and res is an integer type variable, then an assignment
statement is written as:
res=(a>b)?:a:b
The above statement is equivalent to:
if(a>b)
res=a;
else
res=b;

Mr.Nadeem Akhtar 29
Program 02-19
 Write a program to input two values. Use conditional operator to find out which
value is greater. Print a message on screen to indicate the greater value.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,max;
char q;
cout<<“Enter first value";
cin>>a;
cout<<“Enter Second value";
cin>>b;
max=(a>b)?a:b;
cout<<“Greater value is =“<<max<<endl;
cout<<“Press any character to exit the program =”<<endl;
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 30
Logical Operators:

 The logical operators are used to combine relational expressions


or relational conditions.
 The output of logical expressions is also in logical form.Its value is
either True or False.
 In C++ following logical operators are used:
(i) &&  AND operator
(ii) II  OR operator
(iii) !  NOT operator.

Mr.Nadeem Akhtar 31
The && (AND) Operator

 The && (AND) operator is used to combine two or more


relational expressions .
 If all relational expressions are true then the out put returned by
the compound expression is True.
 If any one of relational expressions is false then the out put
returned by the compound expression is false.
 For example, if x=10, y=15, z=5 then compound expression
(x<y) && (z==5) returns true because both the expressions are
true. Similarly, the compound expressions (x>y) && (z==5) and
(x<y) && (z>x) will return False values.

Mr.Nadeem Akhtar 32
Program 02-21
 Write a program to find out the largest no. from three given numbers.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b,c;
char q;
cout<<“Enter first value";
cin>>a;
cout<<“Enter Second value";
cin>>b;
cout<<“Enter third value";
cin>>c;
if((a>b) && (a>c))
icout<<“1 St value is greater"<<endl;
else
if((b>a) && (b>c))
cout<<“ Second value is greater"<<endl;
else
cout<<“ 3 rd value is greater”<<endl;
cout<<“Press any character to exit the program =”<<endl;
cin>>q;
return 0;
} Mr.Nadeem Akhtar 33
The II (OR) Operator
 The II (OR) operator is used to combine more than
one relational expressions .
 If any one of the given relational expressions is true the
out put will be True otherwise the output will be false.
 For example, if x=0, y=15, z=5 then compound
expression
(x>y) II (z==5) returns true because (z==5) is true.
Similarly, (x!=y) II (x==y) II (z>10) will return true
values because (x!=y) is true.
 If all the relational expressions in the compound
condition are false then out put will be false.

Mr.Nadeem Akhtar 34
Program 02-22
 Write a program in C++ to implement the “OR”
operator.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a;
char q,op;
cout<<“Enter an integer value";
cin>>a;
cout<<“Enter any character";
cin>>op;
if((a<0) || (op==‘y’) || (op==‘n’))
cout<<“OK, condition is true"<<endl;
else
cout<<“ All relational expressions are false”<<endl;
cout<<“Press any character to exit the program =”<<endl;
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 35
The ! (NOT) Operator
 The ! (NOT)operator is also known as the
Unary operator. It inverts the value returned by
the relational expression or the compound
expression.
 For example, if x=5 and y=10 then the logical
expression !(x>y) returns true. It is because
(x>y) returns false and the “!”(NOT) operator
inverts the result into true .

Mr.Nadeem Akhtar 36
Program -2-23

 Write a program to find out the greater value from two given values:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int a,b;
char q;
cout<<"Enter 1st an integer value";
cin>>a;
cout<<"Enter 2nd integer value";
cin>>b;
!(a<b)? cout<<"1st is larger"<<endl : cout<<"2nd is larger<<endl";
cout<<"Press any character to exit the program ="<<endl;
cin>>q;
return 0;
}

Mr.Nadeem Akhtar 37

You might also like