Mathematics & Computer Programming: Toqeer Mahmood
Mathematics & Computer Programming: Toqeer Mahmood
Programming
Toqeer Mahmood
Dept. of Civil Engg.
UET, Taxila
Lecture
#
04
The “setw” Manipulator
“setw” stands for set width.
“setw” is used to set width of the output on the output
device.
“setw” is used to specify the width for an output. This
output is printed right justified in the specified width.
Syntax: setw(n);
where ‘n’ specifies the width of the output field in numbers
e.g. cout<<setw(10)<<“Punjab”<<setw(10)<<“Islamabad”;
“setw” manipulator required an header file that is required
to be included in the program
# include <iomanip.h>
9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
P u n j a b I s l a m a b a d
Relational Operators
In order to evaluate a comparison between two expressions
The result of a relational operation is a Boolean value i.e. True or False.
Here is a list of the relational and equality operators that can be used in
C++
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
examples: (7 == 5)Evaluates to false.
(5 > 4) Evaluates to true.
(3 != 2) Evaluates to true.
(6 >= 6)Evaluates to true.
(5 < 5) Evaluates to false.
Conditional operator ( ? : )
The conditional operator evaluates an expression and return a value if that
expression is true and a different one if the expression is evaluated as
false.
Syntax:
{condition} ? {exp1} : {exp2}
if (condition)
statement;
if (condition)
{ statement 1;
statement 2;
………
statement n;}
Set of statements
Statements after if
structure
“If – else” statement
This is another for of the “if statement”.
Used for two way decision depending of the
given condition.
If the test condition is true then the TRUE block
of statement/s executes otherwise FALSE part
executes.
“If – else” statement
Syntax: (condition) ? Exp 1: Exp 2
if (condition)
statement;
else
statement;
if (condition)
{ statement 1;
statement 2; }
else
{ statement 1;
statement 2; }
“If – else” statement
Statements after if
structure
“Nested If” statement
When we use an “if statement” within another “if statement” is known as “nested if”
statement
If (condition)
{
if (condition)
{
statement/s
}
statement/s
}
else
{
if (condition)
{
statement/s
}
statement/s
}
Lab work
Write a program to input a number and show
that the input number is EVEN or ODD
(using if statement and conditional operator )
Write a program to input two numbers and
show the greater number (using conditional
operator and if-else statement)
Write a program to input three integer
values. Compare the three values to find out
if they are equal or not equal (using nested if
statement)
Practice Assignment
Write a program to input three integer
values. Compare the numbers and show
the highest number or lowest number
Write a program to input two values then
input an arithmetic operator and perform
the related operation and show the result
of that arithmetic operation.
Example condition: if(ch==‘+’)