Programming With C++ - Lecture5
Programming With C++ - Lecture5
The if Statements
Syntax
If (condition) statement;
Condition is any integral expression
Statement is any executable statement.
The statement will be executed if the integral
expression is having any non zero value (true)
If the value from the integral expression is zero
(false) the statement will not be executed
4
The if Statements
int main()
{int m, n;
cout << "Enter integer m = ";
cin>>m;
cout << "Enter integer n = ";
cin>>n;
if (m%n) cout << "The remainder is not
zero"<<endl;
}
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter integer m = ";
cin>>m;
cout << "Enter integer n = ";
cin>>n;
if (m%n) cout << "The remainder is not zero"<<endl;
else cout << "The remainder is zero"<<endl;
}
7
Comparison Operators
Six comparison operators
X < y //x is less than y
X > y //x is greater than y
X <= y //x is less than or equal to y
X >= y //x is greater than or equal to y
X == y //x is equal to y
X != y //x is not equal to y
These comparison operators can be used as condition
in the decision making statements.
For instance an integral statement with comparison of
two values.
Assignment and equal to operator
8
Comparison Operators
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m<n) cout << "m is the smaller integer"<<endl;
else cout << "n is the smaller integer"<<endl;
}
Comparison Operators
Three integers
int main()
{int l, m, n;
cout << "Enter three positive integer = ";
cin>>l>>m>>n;
int min=l;
if (m<min) min=m;
if (n<min) min=n;
cout << min<< "is the smallest integer"<<endl;
}
10
Statement block
Remember internal blocks and scope
Statement block are used to execute several
statements with one decision.
if (condition){ statement 1; statement 2; statement 3;}
11
Statement block
int main()
{// this program arranges two integer in ascending order
int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m>n) {int arrange=m; m=n; n=arrange;} //Swap
cout <<m<<" and "<< n<<endl;
}
12
Compound conditions
With compound conditions we can combine two
condition together for decisions making,
The OR (||), AND (&&) and NOT (!) operators are
used for this purpose.
Truth Table
For example
x<y && x!=y
x<y || a==b
Short circuiting
13
Compound conditions
#include <iostream>
using namespace std;
int main()
{int m, n;
cout << "Enter two positive integer = ";
cin>>m>>n;
if (m<n && m!=n) cout << "m is the smaller integer"<<endl;
if (n<m && m!=n) cout << "n is the smaller integer"<<endl;
}
14
Compound conditions
int main()
{int l, m, n;
cout << "Enter three positive integer = ";
cin>>l>>m>>n;
if (l<=m && l<=n) cout<<l<<"is the smallest
integer";
if (m<=l && m<=n) cout<<m<<"is the smallest
integer";
if (n<=l && n<=m) cout<<n<<"is the smallest
integer";
}
15
if (condition1)
if (condition2) statement 1;
else statement 2;
16
if (condition1) statement 1;
else if (condition2) statement 2;
else if (condition3) statement 3;
18
Practice
Create a program to ask shape of room from round and
rectangular/square. Than ask the user for dimensions. Calculate
the areas and calculate the carpet cost for 100 Rs/ft2. Output total
cost.
The End
Reference book:
Programming with C++, SCHAUM’s outlines, John Hubbard,
3rd/2nd Edition, McGraw Hill, New Delhi