0% found this document useful (0 votes)
18 views24 pages

Lecture1 1 3

Uploaded by

ethical581
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)
18 views24 pages

Lecture1 1 3

Uploaded by

ethical581
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/ 24

University Institute of Computing

BCA/BCA(ARVR)/B.Sc.(CS)
OBJECT ORIENTED PROGRAMMING
23CAH-111/23SCH-151/23CAH-151

Topic: manipulators, operators, control statements DISCOVER . LEARN . EMPOWER


Contents
• Manipulators
• Operators
• Control Statement

2
Manipulators
• Manipulators are helping functions that can modify the input/output
stream. It does not mean that we change the value of a variable, it
only modifies the I/O stream using insertion (<<) and extraction (>>)
operators.
• For example, if we want to print the hexadecimal value of 100 then
we can print it as
cout<<setbase(16)<<100

3
Types of Manipulators
• There are various types of manipulators:
• Manipulators without arguments: The most important manipulators defined by the
IOStream library are provided below.
• endl: It is defined in ostream. It is used to enter a new line and after entering a new line it
flushes (i.e. it forces all the output written on the screen or in the file) the output stream.
• ws: It is defined in istream and is used to ignore the whitespaces in the string sequence.
• ends: It is also defined in ostream and it inserts a null character into the output stream. It
typically works with std::ostrstream, when the associated output buffer needs to be null-
terminated to be processed as a C string.
• flush: It is also defined in ostream and it flushes the output stream, i.e. it forces all the
output written on the screen or in the file. Without flush, the output would be the same,
but may not appear in real-time. Examples:

4
Example
#include <iostream> #include <istream> #include <sstream>#include <string> using namespace std;
int main()
{ istringstream str(“ Programmer");
string line;
// Ignore all the whitespace in string
// str before the first word.
getline(str >> std::ws, line);
// you can also write str>>ws // After printing the output it will automatically // write a new line in the
output stream.
cout << line << endl; // without flush, the output will be the same.
cout << "only a test" << flush;// Use of ends Manipulator
cout << "\na"; // NULL character will be added in the Output
cout << "b" << ends;
cout << "c" << endl;
return 0;
} 5
Example output

Programmer
only a test
abc

6
Types of Manipulators
• Manipulators with Arguments: Some of the manipulators are used
with the argument like setw (20), setfill (‘*’), and many more. For
Example, you can use following manipulators to set minimum width
and fill the empty space with any character you want: std::cout <<
std::setw (6) << std::setfill (’*’);
• Some important manipulators in <iomanip> are:
• setw (val): It is used to set the field width in output operations.
• setfill (c): It is used to fill the character ‘c’ on output stream.
• setprecision (val): It sets val as the new value for the precision of floating-point values.
• setbase(val): It is used to set the numeric base value for numeric values.
• setiosflags(flag): It is used to set the format flags specified by parameter mask.
• resetiosflags(m): It is used to reset the format flags specified by parameter mask.

7
Operators
• The symbols that are used in C++ programs to form an expression are
known as operators. C++ has a rich set of operators including all C
language’s operators and also some new operators.
• There are three categories of operators in C++. These are: • Unary
Operators • Binary Operators • Ternary Operators

8
Category of Operators
• The operators that operate a single operand to form an expression are
known as unary operators. The operators like + + (increment)
operator, -- (decrement) operator etc. are the part of unary operators.
• The operators that operate two operands are known as binary
operators. The operators like +, -, *, /, % etc. are binary operators. •
E.g. a+b, a-b, a*b, a/b etc.
• The operator that operates minimum or maximum three operands is
known as ternary operator. There is only one ternary operator
available in C++. The operator ?: is the only available ternary operator
i.e. used as a substitute of if-else statement. • E.g. a>b ? a:b

9
Type of operators
• Arithmetic operators: The operators that helps the programmer in mathematical
calculations are known as arithmetic operators. Arithmetic operators include (+) for
addition, (-) for subtraction, (/) for division, (*) for multiplication etc. • E.g. 2+5 = 7
• Logical operators: he operators that help the programmer to connect (combine) two or
more expressions, are known as logical operators. Logical operators include: 1. && logical
AND 2. | | logical OR 3. | logical NOT
• Comparison operators: The operators that are used to compare variables to check if they
are similar or not. It will return values in true or false. These are also known as relational
operators. Comparison operators are: 1. > Greater than 2. < Less than 3. = Equal to
• Assignment operators: The operator i.e. used to assign values to identifiers, is known as
assignment operator. There is only one assignment operator in C++. The assignment
operator (=) is used to assign values to identifiers. • E.g. a = 2 [ assign value 2 to a ]

10
Type of operators
• Bitwise operators: The operators which operate a bit level and allows the
programmer to manipulate individual bits. These are basically used for testing
or shifting bits. • E.g. x << 3 // Shift three bit position to left.
• Scope resolution operator (::): Like C, C++ is also a block-structured language. A
variable declared in a block is said to be local to that block. In C, the global
version of a variable cannot be accessed from within the inner block. C++ solves
this problem by introducing the new operator :: called the scope resolution
operator. This operator allows access to the global version of a variable.
• Line feed operator & Field width operator: These operators are used to format
data display. The most commonly used manipulators are endl and setw. The
endl manipulator has the same effect as using the newline character “n”. The
setw manipulator specifies a field width for printing the value of variables.
11
Program to demonstrate the
use of scope resolution operator
#include<iostream.h> #include<conio.h>
int x = 10; // global x
int main()
{ int x = 20; // x redeclared, local to main
{ int k = x; int x = 30; // x is declared again in inner block
clrscr();
cout << “n We are in inner block n”;
cout << “k = “ << k << “n”;
cout << “x = “ << x <<“n”;
cout << “:: x = “ << :: x <<“n”; }
cout << “n We are in outer block n”;
cout << “x = “ << x <<“n”;
cout << “:: x = “ << :: x <<“n”;
return 0; }
Output • We are in inner block k = 20 x = 30 :: x = 10 • We are in outer block x = 20 :: x = 10
12
Program to demonstrate use of
endl and setw operators
#include<iostream.h> #include<conio.h> #include<iomanip.h> // for setw
int main()
{ int basic = 750, allowance = 75;
clrscr();
cout<< “Enter Basic Salary” << setw(10) <<basic<<endl; cout<<“Enter
Allowance” << setw(10) <<allowance<<endl;
return 0;
}
Output • Enter Basic Salary 750 Enter Allowance 75
13
Control Statement
• Programs are written using three basic structures
– Sequence
• a sequence is a series of statements that execute one after another
– Repetition(loop or iteration)
• repetition (looping) is used to repeat statements while certain
conditions are met.
– Selection(branching)
• selection (branch) is used to execute different statements depending on
certain conditions
• Called control structures or logic structures
14
Conditional Statement
Conditional Statements
• if
• if else
• nested if (if – else if – else if – else)
• statement blocks ({…})
• (goto)
• switch (case, default, break)

15
If Conditional Statement
Syntax:
if(condition)statement
Eg.
if (Marks>=40)
cout<<“Result : Pass”;
• If the condition is true, statement is
executed.
• If the condition is false, statement is not
executed.
16
If-else Conditional Statement
Syntax:
if(condition)statement1 else statemen2
Eg.
if (Marks>=40)
cout<<“Result : Pass”;
else
cout<<“Result : Fail”;
• If the condition is true, statement1 is
executed.
• If the condition is false, statement2 is executed.
17
Looping Statement
Looping Statements
• For loop
• While loop
• Do While loop

18
For Loop Statement
• for loop – firstly initializes, then, condition check, execute body,
update.
Syntax:

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}
19
While Loop Statement
• while loop – First checks the condition, then executes the body.
• Syntax:
initialization expression;
while (test_expression)
{
// statements

update_expression;
}

20
Do While Loop Statement
• do-while loop – firstly, execute the body then condition check
• Syntax:
initialization expression;
do
{
// statements

update_expression;
} while (test_expression);

21
Assessment Questions
• Write a C++ program to Demonstrate for loop
• Write a C++ program to Demonstrate while loop
• Write a C++ program to Demonstrate do-while loop

22
Discussion forum.

A deeper dive into Control Statement in C++

https://www.geeksforgeeks.org/decision-making-c-cpp/

23
THANK YOU

You might also like