An Engineer's Literature

Language spoken by engineers!

Lesson5: Operators, Expressions and Operator Precedence (Arithmetic Operations)

Arithmetic Operators

Addition (+)

-Takes the sum of two or more variables, two or more constants, or a combination of both.

Subtraction(-)

-Takes the difference of two or more variables, two or more constants, or a combination of both.

Multiplication(*)

-Takes the product of two or more variables, two or more constants, or a combination of both.

Division

-Takes the quotient of two or more variables, two or more constants, or a combination of both.

Modulo(%)

-Takes the sum of two or more variables, two or more constants, or a combination of both.

Here is an example of a program that uses Arithmetic Operators:

Picture15

It is also possible to assign the results to different variables and print these variables.  Here is how it goes:

Picture16

Operator Precedence

Operator precedence is an important thing in C++ and other programming language.  It is a must to know for each and every programmers how this works so here are the rules and an example program:

Rules:

  1. Operations *, /, and % are solved first.  It is also solved from left to right.
  2. + and – are next.  Like the first, it is also solved from left to right.
  3. If there are operations inside ( ), it is solved first then rule 1 and rule 2.  If multiple parenthesis are in an equation like (3+2)*(2/1), 3+2 is solved first, then 2/1, and after that the sum and quotient are multiplied.

Picture17

It is always ideal to add parenthesis in long equations to easily understand the given equation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.