User Tools

Site Tools


programming_fundamentals_with_processing:ch02-getting-going.html

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming_fundamentals_with_processing:ch02-getting-going.html [2017/07/26 15:52] – [Source code] mithatprogramming_fundamentals_with_processing:ch02-getting-going.html [2017/07/26 17:44] (current) – [The console] mithat
Line 7: Line 7:
 ==== Source code ==== ==== Source code ====
  
-A computer program's **source code** is what you "write" when you "write" a program. It almost always consists of plain, simple text.((There are programming tools that are based on manipulating visual elements rather than source code, but these as a rule ultimately produce text output. +In programming, the stuff you "write" when you "write" a program is called **source code**. It almost always consists of plain, simple text.((There are programming tools that are based on manipulating visual elements rather than source code, but these as a rule ultimately produce text output. 
-)) The source code you write is stored in files called **source code files** --- sometimes shortened to simply **sources** or **code**. Thus, source code files are text files made up of sets of instructions written in a particular programming language that express what you'd like the program to do.+)) The source code you write is stored in files called **source code files**---sometimes shortened to simply **sources** or **code**. Thus, source code files are text files made up of sets of instructions written in a particular programming language that express what you'd like the program to do.
  
 ==== Entry points ==== ==== Entry points ====
  
-Some languages start executing a program at the first bit of program text that is in the program's source file. Other languages start a program's execution at some magical point in the code that someone needs to tell you aboutthat is, at a predefined **entry point**. Processing is actually both.+Some languages start executing a program at the first bit of program text that is in the program's source code file. Other languages start a program's execution at some magical point in the code that someone needs to tell you about---that is, they begin at a predefined **entry point**. Processing is actually both.
  
 === Processing without an entry point === === Processing without an entry point ===
  
-Below is one of the simplest Processing programs you can write. When executed, it will print an important message about cranberries.+Below is one of the simplest Processing programs you can write. When executed, it will print an important message about cranberries.((Yes, this text output goes against the visual intent of Processing! Don't worry, we'll get to visual stuff shortly.))
  
-**program ''%%cranberries_basic.pde%%'':** +<file  java cranberries_basic_mode.pde>
- +
-<code java>+
 println("Cranberries are high in antioxidants."); println("Cranberries are high in antioxidants.");
-</code> +</file>
-This is an example of a program written in what Processing calls its Basic Mode. In Basic Mode, the program's execution will begin at the first statement in the source code file. It will then continue statement-by-statement until it reaches the end of the file. Here's a program with two statements:+
  
-**program ''%%cranberries_and_more.pde%%'':**+This is an example of a program written in what Processing calls its Basic ModeIn Basic Mode, the program's execution will begin at the first statement in the source code file. It will then continue statement-by-statement until it reaches the end of the file.
  
-<code java>+Here's a program with two statements: 
 + 
 +<file java cranberries_and_more.pde>
 println("Cranberries are high in antioxidants."); println("Cranberries are high in antioxidants.");
 println("Some blueberries too."); println("Some blueberries too.");
-</code+</file> 
-Processing's Basic Mode is an extremely easy way to program. However, the rules of Basic Mode state that you are not allowed to define any functions in Basic Mode--which as we shall see later is quite a serious limitation. Therefore, for the remainder of this text we are going to pretend that Processing's Basic Mode doesn't exist and focus entirely on what Processing calls its Continuous Mode--described next.+ 
 +Processing's Basic Mode is an extremely easy way to program. However, the rules of Basic Mode state that you are not allowed to define any //functions// in Basic Mode---which as we shall see later is quite a serious limitation. Therefore, for the remainder of this text we are going to pretend that Processing's Basic Mode doesn't exist and focus entirely on what Processing calls its Continuous Mode. We describe that next.
  
 === Processing with an entry point === === Processing with an entry point ===
  
-Processing will automatically switch to Continuous Mode when you define at least one function in your program. (This is why you are not allowed to define functions in Basic Mode.) In Continuous Mode, Processing will look for a definition of a function called **''%%setup%%''** and use this function as the point where the programs execution begins. Below is the Continuous Mode equivalent of the Basic Mode example above.+Processing will automatically switch to Continuous Mode when you define at least one function in your program. (This is why you are not allowed to define functions in Basic Mode.) In Continuous Mode, Processing will look for a definition of a function called **''%%setup%%''**and it will use this function as the point where the programs execution begins. Below is the Continuous Mode equivalent of the Basic Mode example above.
  
-**program ''%%cranberries_continuous.pde%%'':** +<file java cranberries_continuous_mode.pde>
- +
-<code java>+
 void setup() { void setup() {
   println("Cranberries are high in antioxidants.");   println("Cranberries are high in antioxidants.");
 } }
-</code>+</file> 
 This program consists of a definition of the ''%%setup%%'' function that has only one statement in it. If you run this program, you will discover that it too prints out an important message about cranberries. This program consists of a definition of the ''%%setup%%'' function that has only one statement in it. If you run this program, you will discover that it too prints out an important message about cranberries.
  
Line 50: Line 49:
 <code java> <code java>
 void setup() { void setup() {
-   
 } }
 </code> </code>
 +
 The code that is written between the pair of curly braces is called the **body** of the ''%%setup%%'' function. The code that is written between the pair of curly braces is called the **body** of the ''%%setup%%'' function.
  
Line 60: Line 59:
 void setup() { void setup() {
 </code> </code>
-and/or after the matching +and/or after the matching ''}'', 
- +
-<code java> +
-} +
-</code>+
 the program will actually start at the first statement in the ''%%setup%%'' function's body, that is the first bit of code after the program will actually start at the first statement in the ''%%setup%%'' function's body, that is the first bit of code after
  
Line 70: Line 65:
 void setup() { void setup() {
 </code> </code>
 +
 ==== Program statements ==== ==== Program statements ====
  
Line 92: Line 88:
 </code> </code>
  
----- 
  
 +<WRAP center round box 80%>
 === Geek break: Why semicolons? === === Geek break: Why semicolons? ===
  
 If a statement is the programming equivalent of a sentence, why are semicolons used to mark the end of statements rather than periods? If a statement is the programming equivalent of a sentence, why are semicolons used to mark the end of statements rather than periods?
  
-The biggest reason for this is that the language on which Processing is based, Java, uses semicolons to mark the end of statements. But why does Java do this? Because the language on which a lot of Java syntax is based, C++, also uses semicolons. Why does C++ do this? Because ..., you get the idea. The language ALGOL, whose development began in the late 1950's, may have been the first language to use the semicolon as the statement terminator. While the semicolon is widely used for this purpose, you should note that this is by no means universal.+The biggest reason for this is that the language on which Processing is based, Java, uses semicolons to mark the end of statements. But why does Java do this? Because the language on which a lot of Java syntax is based, C++, also uses semicolons. Why does C++ do this? Because ... , you get the idea. The language ALGOL, whose development began in the late 1950's, may have been the first language to use the semicolon as the statement terminator. While the semicolon is widely used for this purpose, you should note that this is by no means universal.
  
-One of the things that makes the semicolon well-suited to marking the end of statements is that the semicolon has little semantic value otherwise. A period is commonly used with floating point numbers (e.g., 3.14159, 22.5, 98.6) and so marking the end of program statements with a period would mean that whatever tries to understand the code would have to do additional work to figure out whether the dot means "end of statement" or "decimal point."((In spite of this, the dot has recently been adopted by a number of languages for purposes other than marking decimal points--as we will soon see.+One of the things that makes the semicolon well-suited to marking the end of statements is that the semicolon has little semantic value otherwise. A period is commonly used with floating point numbers (e.g., 3.14159, 22.5, 98.6) and so marking the end of program statements with a period would mean that whatever tries to understand the code you're written would have to do additional work to figure out whether the dot means "end of statement" or "decimal point."((In spite of this, the dot has recently been adopted by a number of languages for purposes other than marking decimal points---as we will eventually see.
 )) ))
  
-You might now be asking why you need to mark the end of a statement all. Isn't it obvious that the statement ends at the end of the line? Some languages actually do this. There are advantages and disadvantages either way, which you might gain an appreciation for when we cover the subject of whitespace. The debate over which is indeed "better" can be counted as one of the many religious wars in programming.+You might now be asking why you need to mark the end of a statement all. Isn't it obvious that the statement ends at the end of the line? Some languages actually do this. There are advantages and disadvantages either way, which you might gain an appreciation for when we cover the subject of //whitespace//. The debate over which is indeed "better" can be counted as one of the many religious wars in programming.
  
 +</WRAP>
  
----- 
  
 ==== Comments ==== ==== Comments ====
Line 133: Line 129:
 Sending text messages to the console can be accomplished with two predefined Processing functions: ''%%print%%'' and ''%%println%%''. Here is an example of a Processing program that uses ''%%println%%'' to output a message to the console: Sending text messages to the console can be accomplished with two predefined Processing functions: ''%%print%%'' and ''%%println%%''. Here is an example of a Processing program that uses ''%%println%%'' to output a message to the console:
  
-**program ''%%cranberries.pde%%'':** +<file java cranberries.pde>
- +
-<code java>+
 void setup() { void setup() {
   println("Cranberries are high in antioxidants.");   println("Cranberries are high in antioxidants.");
 } }
-</code>+</file> 
 The above code consists of a definition of the ''%%setup%%'' function in which there is a **call** to or **invocation** of the ''%%println%%'' function. The program will produce the following console output when you run it: The above code consists of a definition of the ''%%setup%%'' function in which there is a **call** to or **invocation** of the ''%%println%%'' function. The program will produce the following console output when you run it:
  
Line 145: Line 140:
 Cranberries are high in antioxidants. Cranberries are high in antioxidants.
 </code> </code>
 +
 Notice the double quotation marks around the text. These are important. They tell ''%%println%%'' that everything between them is a string of text characters that should be treated as a unit. Notice the double quotation marks around the text. These are important. They tell ''%%println%%'' that everything between them is a string of text characters that should be treated as a unit.
  
Line 161: Line 157:
 } }
 </code> </code>
 +
 produces the following console output: produces the following console output:
  
Line 166: Line 163:
 Cranberries are high in antioxidants. Cranberries are high in antioxidants.
 </code> </code>
 +
 Using ''%%println%%'': Using ''%%println%%'':
  
Line 176: Line 174:
 } }
 </code> </code>
-will outputs:+ 
 +will output:
  
 <code> <code>
Line 184: Line 183:
 antioxidants. antioxidants.
 </code> </code>
 +
 === Outputting numbers and calculations === === Outputting numbers and calculations ===
  
Line 219: Line 219:
 </code> </code>
  
----- +<WRAP center round box 80%>
 === Geek break: What about input? === === Geek break: What about input? ===
  
Line 228: Line 227:
  
 Because text-based input is as involved as it is in Processing, we will avoid it in this text. We will, however, work with some of the interaction features that Processing makes very easy when we cover //event handling//. Because text-based input is as involved as it is in Processing, we will avoid it in this text. We will, however, work with some of the interaction features that Processing makes very easy when we cover //event handling//.
 +</WRAP>
  
  
----- 
  
 ==== The canvas ==== ==== The canvas ====
programming_fundamentals_with_processing/ch02-getting-going.html.1501084352.txt.gz · Last modified: 2017/07/26 15:52 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki