forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatorprecedence.html
More file actions
111 lines (99 loc) · 3.44 KB
/
operatorprecedence.html
File metadata and controls
111 lines (99 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<!DOCTYPE html>
<html>
<head>
<script src="../../processing.js"></script>
<link rel="stylesheet" href="../style.css"/></head>
<body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>OperatorPrecedence</h2>
<p>by REAS <http://reas.com>
If you don't explicitly state the order in which
an expression is evaluated, they are evaluated based
on the operator precedence. For example, in the statement
"4 + 28", the 2 will first be multiplied by 8 and then the result will
be added to 4. This is because the "*" has a higher precedence
than the "+". To avoid ambiguity in reading the program,
it is recommended that is statement is written as "4 + (28)".
The order of evaluation can be controlled through placement of
parenthesis in the code. A table of operator precedence follows below.</p>
<p><a href="http://processing.org/learning/basics/operatorprecedence.html"><b>Original Processing.org Example:</b> OperatorPrecedence</a><br>
<script type="application/processing">
// The highest precedence is at the top of the list and
// the lowest is at the bottom.
// Multiplicative: * / %
// Additive: + -
// Relational: < > <= >=
// Equality: == !=
// Logical AND: &&
// Logical OR: ||
// Assignment: = += -= *= /= %=
size(200, 200);
background(51);
noFill();
stroke(51);
stroke(204);
for(int i=0; i< width-20; i+= 4) {
// The 30 is added to 70 and then evaluated
// if it is greater than the current value of "i"
// For clarity, write as "if(i > (30 + 70)) {"
if(i > 30 + 70) {
line(i, 0, i, 50);
}
}
stroke(255);
// The 2 is multiplied by the 8 and the result is added to the 5
// For clarity, write as "rect(5 + (2 * 8), 0, 90, 20);"
rect(4 + 2 * 8, 52, 90, 48);
rect((4 + 2) * 8, 100, 90, 49);
stroke(153);
for(int i=0; i< width; i+= 2) {
// The relational statements are evaluated
// first, and then the logical AND statements and
// finally the logical OR. For clarity, write as:
// "if(((i > 10) && (i < 50)) || ((i > 80) && (i < 160))) {"
if(i > 20 && i < 50 || i > 100 && i < width-20) {
line(i, 151, i, height-1);
}
}
</script><canvas width="200" height="200"></canvas></p>
<div style="height:0px;width:0px;overflow:hidden;"></div>
<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
// The highest precedence is at the top of the list and
// the lowest is at the bottom.
// Multiplicative: * / %
// Additive: + -
// Relational: < > <= >=
// Equality: == !=
// Logical AND: &&
// Logical OR: ||
// Assignment: = += -= *= /= %=
size(200, 200);
background(51);
noFill();
stroke(51);
stroke(204);
for(int i=0; i< width-20; i+= 4) {
// The 30 is added to 70 and then evaluated
// if it is greater than the current value of "i"
// For clarity, write as "if(i > (30 + 70)) {"
if(i > 30 + 70) {
line(i, 0, i, 50);
}
}
stroke(255);
// The 2 is multiplied by the 8 and the result is added to the 5
// For clarity, write as "rect(5 + (2 * 8), 0, 90, 20);"
rect(4 + 2 * 8, 52, 90, 48);
rect((4 + 2) * 8, 100, 90, 49);
stroke(153);
for(int i=0; i< width; i+= 2) {
// The relational statements are evaluated
// first, and then the logical AND statements and
// finally the logical OR. For clarity, write as:
// "if(((i > 10) && (i < 50)) || ((i > 80) && (i < 160))) {"
if(i > 20 && i < 50 || i > 100 && i < width-20) {
line(i, 151, i, height-1);
}
}</pre>
</body>
</html>