0% found this document useful (0 votes)
29 views16 pages

Mathematics & Computer Programming: Toqeer Mahmood

The document discusses the setw manipulator in C++, which sets the width of output on the output device. It also discusses relational operators like ==, !=, >, etc. and shows examples of conditional and if-else statements to execute code based on conditions.

Uploaded by

FILZAH HASHMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views16 pages

Mathematics & Computer Programming: Toqeer Mahmood

The document discusses the setw manipulator in C++, which sets the width of output on the output device. It also discusses relational operators like ==, !=, >, etc. and shows examples of conditional and if-else statements to execute code based on conditions.

Uploaded by

FILZAH HASHMI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Mathematics & Computer

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}

Condition: represents the test condition. If the condition is true then


Exp1 will be executed and if the condition is false then Exp2 will execute.

Exp1 & Exp2: represent two expressions. It may be arithmetic


expressions or constant values. Input/output statements can also be used.
Conditional operator ( ? : )
Sample Program
// conditional operator
#include <iostream.h>
#include<conio.h>
int main ()
{
int a, b, c;
a=2;
b=7; Output
c = (a>b) ? a : b;
cout << c; C=7
return 0;
}
Conditional Statement
“If”
Statement
“Switch” Statement
“If” Statement
The “if statement” is used to execute a set
of statements after testing a condition.
“if statement” have two part i.e. TRUE
and FALSE
If the condition is true then the TRUE
part executes and if the condition is false
then the FALSE part executes.
“If” Statement
 Syntax: (Simple if statement)

if (condition)
statement;

if (condition)
{ statement 1;
statement 2;
………
statement n;}

where condition specifies a condition or a relational


expression.
“If” Statement – Flow Chart

FALSE Condition TRUE

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

FALSE Condition TRUE

Set of statement/s Set of statement/s

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==‘+’)

You might also like