Experiment#7: An Introduction To Basic Operators in C++ Objective
Experiment#7: An Introduction To Basic Operators in C++ Objective
ME DEPARTMENT
Experiment#7
An Introduction to Basic operators in C++
Objective
An introduction with
1. Operators, types of operators and basic input and output from user.
Software Tools
1. Dev C++
Theory
C++ Program
A computer program is a sequence of instructions that tell the computer what to do
Operators
Operators are special symbols used for specific purposes. C++ provides six types of operators.
Arithmetical operators, Relational operators, Logical operators, Unary operators, Assignment
operators, Conditional operators, Comma operator
Arithmetical operators
Arithmetical operators +, -, *, /, and % are used to performs an arithmetic (numeric) operation.
You can use the operators +, -, *, and / with both integral and floating-point data types. Modulus
or remainder % operator is used only with the integral data type.
Operators that have two operands are called binary operators.
Relational operators
The relational operators are used to test the relation between two values. All relational operators
are binary operators and therefore require two operands. A relational expression returns zero
when the relation is false and a non-zero when it is true. The following table shows the relational
operators.
ME DEPARTMENT
== Equal to
!= Not equal to
Logical operators
The logical operators are used to combine one or more relational expression. The logical
operators are
Operators Meaning
|| OR
&& AND
! NOT
Unary operators
C++ provides two unary operators for which only one variable is required.
For Example
a = - 50;
a = + 50;
Here plus sign (+) and minus sign (-) are unary because they are not used between two variables.
Assignment operator
The assignment operator '=' is used for assigning a variable to a value. This operator takes the
expression on its right-hand-side and places it into the variable on its left-hand-side. For example:
m = 5;
The operator takes the expression on the right, 5, and stores it in the variable on the left, m.
x = y = z = 32;
This code stores the value 32 in each of the three variables x, y, and z.
ME DEPARTMENT
in addition to standard assignment operator shown above, C++ also support compound
assignment operators.
Conditional operator
The conditional operator ?: is called ternary operator as it requires three operands. The format of
the conditional operator is:
Conditional_ expression ? expression1 : expression2;
If the value of conditional expression is true then the expression1 is evaluated, otherwise
expression2 is evaluated.
int a = 5, b = 6;
big = (a > b) ? a : b;
The condition evaluates to false, therefore biggest the value from b and it becomes 6.
ME DEPARTMENT
Would first assign the value of a to i, and then assign value of b to variable i. So, at the end,
variable i would contain the value 2.
the size of operator determines the amount of memory required for an object at compile time
rather than at run time.
Basic Input/Output
C++ uses a convenient abstraction called streams to perform input and output operations in
sequential media such as the screen or the keyboard. The standard C++ library includes the header
file iostream, where the standard input and output stream objects are declared.
cout console output
cin console input
Standard Output (cout)
By default, the standard output of a program is the screen, and cout is used in conjunction with
the insertion operator, which is written as << (two "less than" signs).
To print more than one thing on the same line, the output operator (<<) can be used multiple
times. For example:
std::endl
If we want to print things to more than one line, we can do that by using endl. When used with
cout, endl inserts a newline character (causing the cursor to go to the start of the next line).
output
ME DEPARTMENT
Hi!
My name is Alex.
Standard Input (cin)
The standard input device is usually the keyboard. input in C++ is done by operator of extraction
(>>) on the cin stream. The operator must be followed by the variable that will store the data that
is going to be extracted from the stream. For example:
int age;
cin >> age;
You can also use cin to request more than one datum input from the user:
cin >> a >> b;
is equivalent to
cin >> a;
cin >> b;
However, cin extraction stops reading as soon as if finds any blank space character, so in this case
we will be able to get just one word for each extraction.
if we want to get a sentence from the user, this extraction operation would not be useful. In order
to get entire lines, we can use the function getline, which is the more recommendable way to get
user input with cin:
int main ()
{
string name;
cout << "Enter your name";
getline (cin, name);
cout << "Hello " << name << "!\n";
return 0;
}
Lab Tasks
1. Write a program that find the greatest number by using conditional operator.
2. Write a program that will calculate body mass index (BMI) of a person by getting weight
and height from user.
Computing Fundamentals 1st Semester-ME HITEC UNI Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF Mechanical ENGINEERING
ME DEPARTMENT
3. Write a program that calculates the total marks and percentage of a student in five
different subjects.