back home Next view contents in a pop up window
Operators
Arithmetic Operators
Rest assure that you will have to do mathematical operations in C++. The rules here are similar to the ones that apply to algebra and your typical grocery calculator but in some ways different as well.
|
+ Used for addition - Used for subtraction * Used for Multiplication / Used for division % Used for Modulus division |
Lets see how these are used in C++
|
#include<iostream.h> int main () { int result; result=5+2; cout << result ; return 0; } |
#include <iostream.h> int main() { int result, num1, num2; cout << " Enter two numbers please."; cin >> num1 >> num2; result=num1-num2; cout << " The result is: " << result;
return 0; }
|
| output | ouput |
|
7 Press any key to continue.
|
Enter two numbers please. 5 3 The result is: 2 Press any key to continue.
|
The program on the left is straight foward. 5 and 2 are added, assinged to the variable result result=5+2; and the result is displayed.
The program on the right instructs us to enter two numbers and it will subtract them after we enter them. I Entered the numbers 5 and 3.
How would these mathematical expressions should be written in such a way that it would be understood by C++?
Algebreic expresion C++ expression
5+5/2 (5+5)/2 or 2/5+5;
xy-zp (x*y)-(z*p)
y=mx+b y=(m*x)+b or y=m*x+b
(35-10)/(10/2) (35-10)/(10/2)
35-(10/10)/1 35-10/10/1
10+10/2+2.5 10+10/2+2.5 or 10+(10/2)+2.5
10* (1+7)*3 10* (1+7)*3
Equality Operators
These are extremly important. These are used to compare values. They will help you set conditions.
|
< Less than > Greater than == Equal too <= Less or equal to >= larger or equal to |
Assignment operators
|
= Assings a value to a variable. NOT TO BE CONFUSED WITH THE EQUALITY SING. += Adds the variable on the left to the right side of an expression. *= Multiplies the variable on the left to the right side of an expression. /= Divides the variable on the left to the right side of an expression. -= Subtracks the variable on the left to the right side of an expression. %= |
Examples and explanetions on the assignment operators.
a) The = operator.
x=5 and x==5 mean different things. And here is a small program that shows just that.
| #include <iostream.h> int main () { int num1, num2, result; num1=20, num2=20; result = num1+num2; cout << result; return 0; } |
#include <iostream.h> int main () { int num1, num2, result; num1=20, num2==20; result = num1+num2; cout << result; return 0; } |
| output | output |
| 3 Press any key to continue |
-858993459 Press any key to continue |
The programs are almost the same besides line 4. In the program on the left num2 is assigned the value of 20 (num2=20) on the program on the right num2 is equal to 20 (num2==20). The output differs in a great way as you see.
b) The += /= -= *= operators
Here are two programs that do the same thing. The program on the right uses the += operator.
| #include <iostream.h> int main () { int num1, result; result=5,num1=10; result=result+ num1; cout << result << endl; return 0; } |
#include <iostream.h> int main () { int num1, result; result=5,num1=10; result += num1; cout << result << endl; return 0; } |
| output | output |
| 15 Press any key to continue |
15 Press any key to continue |
Both programs do the same thing. The program on the left we have two variables num1 and result. Both variable were assinged a value. Then we added the value of num1 to the variable result (result += num1;) line 5
The program on the right does the same thing but it replaces line 5 (result=result+ num1;) with result += num1; The same applies with all other *= -= /= &nbssp; %= operators.
Boolean Operators--> and (&&) or (||) not (!)
There might be an instance when dealing with if or while or for statements that you want to set more than one conditions with in the same code line. Examples in pseudocode:
a) The && (and) operator.
while temp larger than 80 and smaller than 100, then print "At least the asphalt is not melting".
How can we write this? Simple
while (temp<100 && temp>80)
cout << " At least the asphalt is not melting";
b) The || (or) operator.
Here is a part of a pseucode from a program that asks the user to input the number 1 if he is a high school graduate or the number 2 if he has recieved an Associates degree.
For users input 1 Or 2, print " I advise you to get a bachelors. You will be earning more money".
What is the syntax for this?
for (input==1 || input==2)
cout << " I advise you to get a bachelors. You will be earning more money" << endl;
c) The ! (not) operator.
For users input not equal to 100, then print " You have not recieved a perfect grade"
The syntax for this
for (grade != 100)
cout << You have not recieved a perfect grade";
back home Next view contents in a pop up window