programming_fundamentals_with_processing:ch03-places-to-put-things
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
programming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 17:49] – created mithat | programming_fundamentals_with_processing:ch03-places-to-put-things [2017/07/26 18:06] (current) – mithat | ||
---|---|---|---|
Line 16: | Line 16: | ||
$a = b + 20$ | $a = b + 20$ | ||
- | If I asked you what the value of $a$ is, you would probably reply " | + | If I asked you what the value of $a$ is, you would probably reply " |
Let's now look at a Processing program that does what the mathematics example above does. | Let's now look at a Processing program that does what the mathematics example above does. | ||
- | **program '' | + | <file java variables1.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int a; | int a; | ||
Line 30: | Line 28: | ||
a = b + 20; | a = b + 20; | ||
} | } | ||
- | </code> | + | </file> |
The second part of the example probably makes sense to you. It's really just the mathematical expressions with semicolons tacked onto the ends. (Remember that the end of a statement in Processing must be marked with a semicolon.) But you might be confused about the lines that begin with '' | The second part of the example probably makes sense to you. It's really just the mathematical expressions with semicolons tacked onto the ends. (Remember that the end of a statement in Processing must be marked with a semicolon.) But you might be confused about the lines that begin with '' | ||
Line 37: | Line 36: | ||
int b; | int b; | ||
</ | </ | ||
- | do exactly that--they are **variable declarations**. They tell the Processing compiler that you plan to use two variables, one named '' | + | do exactly that---they are **variable declarations**. They tell the Processing compiler that you plan to use two variables, one named '' |
)) | )) | ||
- | Processing' | + | Processing' |
- | )) So, in the above example, you could have written: | + | |
<code java> | <code java> | ||
Line 50: | Line 48: | ||
a = b + 20; | a = b + 20; | ||
</ | </ | ||
+ | |||
as well. | as well. | ||
Line 73: | Line 72: | ||
One way we can learn the value of a variable is to output it to the console as in the code below: | One way we can learn the value of a variable is to output it to the console as in the code below: | ||
- | **program '' | + | <file java variables2.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int a; | int a; | ||
Line 86: | Line 83: | ||
println(a); | println(a); | ||
} | } | ||
- | </code> | + | </file> |
or, if you like, | or, if you like, | ||
- | **program '' | + | <file java variables2a.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int a; | int a; | ||
Line 104: | Line 99: | ||
println(a); | println(a); | ||
} | } | ||
- | </code> | + | </file> |
You can also use variables in places where Processing expects literal constants. Here's an example: | You can also use variables in places where Processing expects literal constants. Here's an example: | ||
- | **program '' | + | <file java variables3.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int a; | int a; | ||
Line 119: | Line 112: | ||
rect(0, 0, a, b); | rect(0, 0, a, b); | ||
} | } | ||
- | </code> | + | </file> |
This will draw a rectangle whose upper left corner is at the origin of the canvas, whose width is the value of '' | This will draw a rectangle whose upper left corner is at the origin of the canvas, whose width is the value of '' | ||
Line 130: | Line 123: | ||
$x = \frac{{ - b \pm \sqrt {b^2 - 4ac} }}{{2a}}$ | $x = \frac{{ - b \pm \sqrt {b^2 - 4ac} }}{{2a}}$ | ||
- | However, most programming languages let you give variables long names--subject to certain rules. **TODO verify:** The rules in Processing say that an identifier must start with an upper or lowercase letter and then can be made up of any combination of upper or lowercase letters, numbers, and the underscore character. Note that spaces in identifiers are not allowed. | + | However, most programming languages let you give variables long names---subject to certain rules. **TODO verify:** The rules in Processing say that an identifier must start with an upper or lowercase letter and then can be made up of any combination of upper or lowercase letters, numbers, and the underscore character. Note that spaces in identifiers are not allowed. |
- | There is also a HolyList® of identifiers that Processing has reserved for its own use--so called **reserved words**. You cannot use reserved words for your own identifiers. For example, '' | + | There is also a HolyList® of identifiers that Processing has reserved for its own use---so called **reserved words**. You cannot use reserved words for your own identifiers. For example, '' |
Finally, Processing is a **case sensitive** language, meaning that the identifier '' | Finally, Processing is a **case sensitive** language, meaning that the identifier '' | ||
Line 140: | Line 133: | ||
Knowing these rules, we could rewrite '' | Knowing these rules, we could rewrite '' | ||
- | **program '' | + | <file java variables3a.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int var_one; | int var_one; | ||
Line 152: | Line 143: | ||
rect(0, 0, var_two, var_one); | rect(0, 0, var_two, var_one); | ||
} | } | ||
- | </code> | + | </file> |
or, | or, | ||
- | **program '' | + | <file java variables3b.pde> |
- | + | ||
- | <code java> | + | |
void setup () { | void setup () { | ||
int rec_width; | int rec_width; | ||
Line 167: | Line 156: | ||
rect(0, 0, rec_width, rec_height); | rect(0, 0, rec_width, rec_height); | ||
} | } | ||
- | </code> | + | </file> |
- | The only difference in the the three versions of the program is the names we have given to the variables; the programs are functionally identical. However, of the three versions a lot of programmers would probably prefer '' | + | |
- | Closely related | + | The only difference in the the three versions of the program is the names we have given to the variables; the programs are functionally identical. However, of the three versions a lot of programmers would probably prefer '' |
- | **program '' | + | Closely related to this are **coding conventions**---mutually agreed upon standard practices for writing code. Coding conventions are not rules; rather they are standard practices that have emerged over time and/or have been agreed upon ahead of time by a team of programmers. A common coding convention for variable names in Processing is to use CamelCase (i.e., capitalizing the first letter of adjacent words) with the first letter of the first word in lower case. Rewriting the example we have been working with so far using the CamelCase convention would yield: |
- | <code java> | + | <file java variables3c.pde> |
void setup () { | void setup () { | ||
int recWidth; | int recWidth; | ||
Line 184: | Line 172: | ||
rect(0, 0, recWidth, recHeight); | rect(0, 0, recWidth, recHeight); | ||
} | } | ||
- | </code> | + | </file> |
- | An alternative convention (used in program '' | + | |
+ | An alternative convention (used in program '' | ||
===== Data types ===== | ===== Data types ===== | ||
Line 195: | Line 184: | ||
int recHeight; | int recHeight; | ||
</ | </ | ||
- | are variable declarations--they announce to the Processing compiler that you plan to use variables with names '' | + | are variable declarations---they announce to the Processing compiler that you plan to use variables with names '' |
Most programming languages, including Processing, are **typed languages**. A typed language is one that differentiates between different types of data: an integer number is one type of data, a character such as ' | Most programming languages, including Processing, are **typed languages**. A typed language is one that differentiates between different types of data: an integer number is one type of data, a character such as ' | ||
Line 231: | Line 220: | ||
=== int === | === int === | ||
- | The '' | + | The '' |
<code java> | <code java> | ||
Line 276: | Line 265: | ||
=== double === | === double === | ||
- | The '' | + | The '' |
=== char === | === char === | ||
- | The '' | + | The '' |
<code java> | <code java> | ||
Line 324: | Line 313: | ||
- | ---- | + | <WRAP center round box 80%> |
=== Geek break: Why doesn' | === Geek break: Why doesn' | ||
Line 335: | Line 323: | ||
These might seem like small issues when you are just starting programming, | These might seem like small issues when you are just starting programming, | ||
+ | </ | ||
- | |||
- | ---- | ||
===== Syntactic sugar ===== | ===== Syntactic sugar ===== | ||
Line 350: | Line 337: | ||
int foo = 66; | int foo = 66; | ||
</ | </ | ||
- | The above statement declares an integer variable named '' | + | The above statement declares an integer variable named '' |
- | If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables--at least for some categories. In other words, the " | + | If you try to access the value of a variable without initializing it or otherwise giving it a value, the Processing IDE will complain at you. This is because it is widely considered bad programming practice to rely on the default values given to variables by a language. The reason for this is that many languages do not specify //any// default values for variables---at least for some categories. In other words, the " |
)) | )) | ||
Line 362: | Line 349: | ||
char firstInitial, | char firstInitial, | ||
</ | </ | ||
- | declare two variables--'' | + | declare two variables---'' |
You can even combine variable initialization and multiple declaration in the same statement: | You can even combine variable initialization and multiple declaration in the same statement: | ||
Line 371: | Line 358: | ||
==== Potential pitfalls ==== | ==== Potential pitfalls ==== | ||
- | Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs--it increases **writability**. When done right, it also makes programs easier to read--it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read--because the reader has to know all the intricacies of all the different possible ways of doing things. | + | Syntactic sugar can be a tricky thing. On the one hand, syntactic sugar makes it easier to write your programs---it increases **writability**. When done right, it also makes programs easier to read--it increases **readability**. However, when a language has too much syntactic sugar, then the number of ways to express the same idea increases to such a level that it ends up making it //harder// to read--because the reader has to know all the intricacies of all the different possible ways of doing things. |
programming_fundamentals_with_processing/ch03-places-to-put-things.1501091386.txt.gz · Last modified: 2017/07/26 17:49 by mithat