
Introduction  Getting Started

Flow of Control
C++ Operators
Introduction
C++ is a very rich in builtin operators. In fact, C++ places more significance on operators that most other computer languages do. Operators are tokens that trigger some computation when applied to variables and other objects in an expression. The objects of the operation are referred as operands. C++’s rich set of operators comprises of arithmetic, relational, logical, conditional and certain other types of operators.
Let’s discuss these operators in detail.
Arithmetic Operators
To do arithmetic, C++ uses arithmetic operators. These are again subdivided into the following:
Unary Operators
Operators that act on one operand are referred to as Unary Operators. C++ has the following Unary Operators:
Unary Negation Operator
The unary negation operator ( – ) produces the negative of its operand. The operand is preceded by a negative sign ( – ). The operand to the unary negation operator must be an arithmetic type.
Example: If a=5 then –a means 5
Unary Plus Operator
The unary plus operator ( + ) produces the positive of its operand. The operand is preceded by a positive sign ( + ). The operand to the unary plus operator must be an arithmetic type.
Example: If a=–5 then +a means –5
Binary Operators
Operators that act upon two operands are referred to as Binary Operators. C++ has the following Binary Operators:
Addition Operator
The operator which is used to find the sum of the two operands. It is represented by the plus sign ( + ).
Example: If x + y ( x=5, y=10 ) results in 15
Subtraction Operator
The operator which is used to find the difference between the two operands. It is represented by the negative sign ( – ).
Example: If x – y ( x=15, y=10 ) results in 5
Multiplication Operator
The operator multiplies the values of its two operands. It is represented by the multiplication sign ( * ).
Example: If x * y ( x=15, y=10 ) results in 150
Division Operator
The operator divides its first operand by the second. It is represented by the division sign ( / ).
Example: If x / y ( x=50, y=10 ) results in 5
Modulus Operator
The operator finds the remainder by dividing the first by the second operand. It is represented by the modulus sign ( % ).
Example: If x % y ( x=50, y=10 ) results in zero
Relational Operators
The operator which determines the relation between two operands. The operators return a true value if the statement is correct and return a false value if the statement is incorrect. The operators are as follows:
Logical Operators
The operators that refer to the ways these relationship among values can be connected. C++ provides three logical operators to combine existing expressions.
Note: True value is equal to one and False value is equal to zero
Logical OR Operator
The Logical OR Operator (  ) evaluates to true ( 1 ) if either of its operands evaluates to true. The operator works as if the operands are getting added ( A+B ).
Example:
Logical AND Operator
The Logical AND Operator ( && ) evaluates to true ( 1 ) only if both of its operands are true. The operator works as if the operands are getting multiplied ( A*B ).
Example:
Logical NOT Operator
The Logical NOT Operator ( ! ) negates or reverses the truth value of the expression following it and vice versa.
Example:
The last few operators will be discussed in the next module.
The Weekly Program
Now let’s create a program to print the Unit test report card of a student.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float s1, s2, s3, s4, s5, marks, perc;
cout<<"Marks obtained in Science:\n";
cin>>s1;
cout<<"Marks obtained in Mathematics:\n";
cin>>s2;
cout<<"Marks obtained in English:\n";
cin>>s3;
cout<<"Marks obtained in Computer Science:\n";
cin>>s4;
cout<<"Marks obtained in Social Studies:\n";
cin>>s5;
marks=s1+s2+s3+s4+s5;
perc=(marks/5);
cout<<"\n\n\n";
cout<<"Unit Test Report Card\n\n";
cout<<"Science Marks: "<<s1<<"\n";
cout<<"Mathematics Marks: "<<s2<<"\n";
cout<<"English Marks: "<<s3<<"\n";
cout<<"Computer Science Marks: "<<s4<<"\n";
cout<<"Social Studies Marks: "<<s5<<"\n";
cout<<"Percentage of is: "<<perc<<"%";
getch();
}
Execution of the Program
The program is quite selfexplanatory. Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:
Now it’s time to do it yourself and feel free to mess around with the program to understand indepth what each statement does. Don’t forget to save your program.
That’s it for today, see you next week!!!