![]() music |
![]() | OSdata.com |
order of precedence
summary
This subchapter looks at order of precedence.
free computer programming text book projecttable of contents
|
![]() music |
![]() | OSdata.com |
This subchapter looks at order of precedence.
free computer programming text book projecttable of contents
|
This subchapter looks at order of precedence.
Every programming language needs to have some system for determining the order of evaluation of expressions.
x := 5 + 6 * 7;
or
x = 5 + 6 * 7
Is x 47 or 77?
(5 + 6) = 11; followed by 11 * 7 = 77
(6 * 7) = 42; 5 + 42 = 47
The normal rules of elementary algebra call for multiplication (and division) to have a higher priority than addition (or subtraction), which would make x = 42.
| elementary algebra |
|---|
| parenthesis |
| exponentiation |
| multiplication |
| division |
| addition |
| subtraction |
A strict left to right evaluation would make the answer x = 77.
A strict right to left evaluation would make the answer x = 42.
The use of a reverse Polish notation (RPN, such as is used in Forth) avoids the question.
The two most common approaches used in computer programming languages are either (1) some order of precendence) or (2) directional evaluation (left-to-right or right-to-left).
The use of parenthesis can be used to change the normal order of evaluation.
Parenthesis can also be used to make complicated expressions more clear to a human reader. This makes the program easier to udnerstand and easier to maintain over years or decades of use.
The order of precedence in the following charts are from highest to lowest (top to bottom). Items on the same level are of equal order of precendence.
| Primary-expression operators | |
|---|---|
| 15 | ( ) [ ] . -> |
| Unary operators | |
| 14 | * & - ! ~ ++ -- sizeof (type) |
| 13 | * / % |
| 12 | + - |
| 11 | >> << |
| 10 | < > <= >= |
| 9 | == != |
| 8 | & |
| 7 | ^ |
| 6 | | |
| 5 | && |
| 4 | || |
| 3 | ?: |
| Assignment operators | |
| 2 | = += -= *= /= %=
>> =< <= &= ^= |= |
| Sequence operator | |
| 1 | , (comma) |
| unary + unary - not |
| * / div mod and |
| + - or |
| = <> < <= > >= in |
| 1 | all subexpressions in parenthesis from innermost to outermost |
| 2 | all exponentiations from right to left |
| 3 | all multiplications and divisions from left to right |
| 4 | all additions and subtractions from left to right |
| 1 | parenthesis |
| 2 | ↑ (exponentiation) |
| 3 | × / ÷ (multiplication and division) |
| 4 | + - (addition and subtraction) |
| 5 | < ≤ = ≠ ≥ > (relational operators) |
| 6 | ¬ (not) |
| 7 | ∧ (and) |
| 8 | ∨ (or) |
| 9 | ⊃ (implies) |
| 10 | ≡ (is equivalent to) |
Note that the actual ALGOL symbol for is not greater than has the less than symbol over the equals symbol rather than ≤ and the ALGOL symbol for not less than has the greater than symbol over the equals symbol rather than ≥.
return to table of contents
free downloadable college text book
Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.
| previous page | next page |
free computer programming text book projectBuilding a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming. If you like the idea of this project, At the time I write this message I am a few days from becoming homeless. That will greatly interfere with my ability to create this project, which can help nearly 20 million U.S. college students and more than 150 million students world-wide. I am looking for 30 rich people or corporations willing to donate $10 a month to my church so that the church can provide a place indoors for me to continue work. If you want to donate, please see help project. Thanks much. Supporting the entire project: If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church) free downloadable college text book on computer programming. |
I do the news as an unpaid volunteer for KOCI 101.5 FM, Newport Beach/Costa Mesa (also available on the web)

This web site handcrafted on Macintosh
computers using Tom Benders Tex-Edit Plus
and served using FreeBSD
.
UNIX used as a generic term unless specifically used as a trademark (such as in the phrase UNIX certified). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.
Names and logos of various OSs are trademarks of their respective owners.
Copyright © 2010 Milo
Created: October 31, 2010
Last Updated: November 24, 2010
return to table of contents
free downloadable college text book
| previous page | next page |