Lab 08 C++ Operators
Lab 08 C++ Operators
OBJECTIVE:
THEORY:
OPERATOR
Operators are the symbols that cause a program to do something on the variables.
Following are the operators which are commonly used in C language.
Arithmetic operators:
There are five arithmetic operators in C++. Those are:
Operator Purpose
+ addition.
- Subtraction.
* Multiplication.
/ Division.
% remainder after integer division.
C++ provides the unary increment operator (++), and the unary increment operator (--) for the
increment or decrement of one (1) to/from the variable. If increment or decrement operator is
placed before the variables, they are referred to as the pre increment or pre decrement operator,
respectively. If increment or decrement operators are placed after the variable, they are referred
as the post increment or post decrement operators
Example
#include<iostream>
main( )
{
int a=1;
cout<<++a; // increment first then display.
cout<<a++; // first display then increment.
cout<<a;
cout<< --a; // decrement first then display.
cout<<a--; // first display then decrement.
cout<<a;
}//Observe the output
Relational Operator:
There are four relational operators they are
Operators Meaning
< Less than.
<= Less than or equal to.
> Greater than.
>= Greater than or equal.
Closely associated with the relational operators are the following two equality operators.
Operators Meaning
== Equal to.
!= Not Equal to.
These six operators are used to form logical expression, which represents condition that is either
true or false. Resulting expression will be of type integer, since true is represented by integer
value 1 and false is represented by the value 0.
Example
#include<iostream>
main()
{
int a=1,b=2;
cout<<(a<b);
cout<<(a>b);
cout<<(++a = = b);
cout<<a<=b;
cout<<a>=b;
}
Logical Operator:
C contains three logical operators, they are
Operators Meaning
&& And
|| OR
! Not
The logical operators act upon the operands that are themselves logical expressions. The net
effect is to combine the individual logical expressions into more complex conditions that are
either true or false. The result of logical and operation will be true only of all the expression are
true, where as the result of a logical or operator will be true if either expression will be true. In
other words, the result of logical or operation will be false when all expression will be false.
#include<iostream>
main()
{
int a=1,b=2;
cout<<( (a<b) && (a<=b));
cout<<( (a= =b) || (a<b));
cout<<( !(a= =b));
} //Observe the Output
Manipulators
Manipulators are operators used with the insertion operator to modify or manipulate the way in
which data is displayed. The function prototype for the manipulators is the iomanip.h header file.
Two of the most common manipulators are endl and setw().
Example
#include<iostream>
#include<iomanip.h>
main()
{
cout<<”UCET”<<setw(5)<<”UNIVERSITY”<<endl<<”IUB”;
}
Observe how manipulators perform their function. Also search some other manipulators from the
help & right down the function of at least three by taking suitable examples
Lab Assignment
Q#02. Write a program that inputs a number and displays whether it is even or odd by using
logical operator.