L3 - Increment and Decrement Operations, Conditional and Logical Operations
L3 - Increment and Decrement Operations, Conditional and Logical Operations
Assistant Professor,
Department of Civil Engineering,
BUET
2
Program to Find Average Speed
/* Program that finds the average speed of three
vehicles */
#include <iostream>
using namespace std;
int main()
{
double num1, num2, num3, average;
cout<<"Program to find the average speed of
three vehicles"<<endl;
cout<<"The first number ="<<endl;
cin>>num1;
cout<<"The second number ="<<endl;
cin>>num2; 3
Program to Find Average Speed
cout<<"The third number ="<<endl;
cin>>num3;
average = (num1 + num2 + num3)/3;
cout<< "Trial program"<<endl;
cout<<"Average =" <<average<<endl; //Prints the
value stored in variable average
return 0;
}
4
Increment and Decrement Operators
5
Increment and Decrement Operators
m = 45, n = 44
int n = 22;
float x = 3.14159;
11
Type Casting
int a=10,b=3;
#include <iostream> double c=double(a)/double(b); //type casting
cout<<c<<endl;
double e=a,f=b; //type promotion
using namespace std; c=e/f;
cout<<c<<endl;
int g=c; //high to low te ashlam but casting lage nai :)
cout<<g;
int main()
double v = 1234.56789;
int n = int(v);
cout << "v = " << v << ", n = " << n << endl;
14
Numeric Overflow
• On most computers the long int type allows
4,294,967,296 different values
17
Round-off Error
• Round-off error is another kind of error that often
occurs when computers do arithmetic on rational
numbers.
18
The if Statement
• The if statement allows conditional execution.
20
The if Statement
21
The if Statement
#include <iostream>
using namespace std;
int main()
{
int n,d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n%d) cout << n << " is not divisible by " << d <<
endl;
}
• Note: This program is inadequate because it
provides no affirmative information when n is
divisible by d. 22
The if … else Statement
• The if … else statement causes one of two
alternative statements to execute depending upon
whether the condition is true, its syntax is
₋ if (condition) statement1;
₋ else statement2;
• condition is an integral expression and statement1
and statement2 are executable statements.
24
The if … else Statement
#include <iostream>
using namespace std;
int main()
{
int n,d;
cout << "Enter two positive integers: ";
cin >> n >> d;
if (n%d) cout << n << " is not divisible by " << d <<
endl;
else cout << n << " is divisible by " << d << endl;
}
₋ x == y // x is equal to y
₋ x != y // x is not equal to y
27
Comparison/Relational Operators
#include <iostream>
using namespace std;
int main()
{
int m,n;
cout << "Enter two integers: ";
cin >> m >> n;
if (m < n) cout << m << " is the minimum." << endl;
else cout << n << " is the minimum." << endl;
}
28
Comparison/Relational Operators
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if (n = 22) cout << n << " = 22" << endl; // LOGICAL
ERROR!
else cout << n << " != 22" << endl;
} 29
Compound Conditions
• Conditions such as n % d and x >= y can be
combined to form compound conditions.
p q p && q p q p || q p !p
T T T T T T T F
T F F T F T F T
F T F F T T
F F F F F F 31
Compound Conditions
#include <iostream>
using namespace std;
int main()
{
char ans;
cout << "Are you enrolled (y/n): ";
cin >> ans;
If (ans == 'Y' || ans == 'y') cout << "You are
enrolled.\n";
else cout << "You are not enrolled.\n";
}
32
Short-Circuiting
• Compound conditions that use && and || will not
evaluate the second operand of the condition unless
necessary, this is called short-circuiting.
37
The else … if Construct
38
The else … if Construct
#include <iostream>
using namespace std;
int main()
{
int score;
cout << "Enter your test score: ";
cin >> score;
if (score > 100) cout <<"Error: that score is out of range.";
else if (score >= 90) cout << "Your grade is an A." << endl;
else if (score >= 80) cout << "Your grade is a B." << endl;
else if (score >= 70) cout << "Your grade is a C." << endl;
else if (score >= 60) cout << "Your grade is a D." << endl;
else if (score >= 0) cout << "Your grade is an F." << endl;
else cout << "Error: that score is out of range.";
} 39
The switch Statement
• The switch statement can be used instead of the else
if construct to implement a sequence of parallel
alternatives. Its syntax is:
switch (expression)
{ case constant1: statementList1;
case constant2: statementList2;
case constant3: statementList3;
:
:
case constantN: statementListN;
default: statementList0;
} 40
The switch Statement
• This evaluates the expression and then looks for its
value among the case constants.
43
The break Statement
• It is normal to put a break statement at the end of
each case clause in a switch statement.
44
Conditional Expression Operator
• C++ provides a special operator that often can be
used in place of the if ... else statement.
min = ( x<y ? x : y );
46
Conditional Expression Operator
#include <iostream>
using namespace std;
int main() if ( condition ) {
statement1;
}
{ else {
statement2;
}
int m, n;
cout << "Enter two integers: ";
cin >> m >> n;
cout << ( m<n ? m : n ) << " is the minimum." <<
endl;
} 47
Thank You!
48