Page 62 - Introduction to Programming with Java: A Problem Solving Approach
P. 62

                28 Chapter 2 Algorithms and Design
one with an uppercase letter to make the combination readable. This is called camelCase, because of the bump(s) in the middle. Again, it’s not necessary for pseudocode, but it’s a good habit to develop. Here are two more examples that show how to name variables with camelCase:
Description
sports team name weight in grams
A Good Variable Name
teamName
weightInGrams
  Variables can hold different types of data. Which type of data would the teamName variable probably hold—a number or a string? It would probably be used to hold a string (e.g., “Jayhawks” or “Pirates”). Which type of data would the weightInGrams variable probably hold—a number or a string? It would probably be used to hold a number (e.g., 12.5). It’s relatively easy for a human to determine the type of a named variable by just thinking about the name, but this kind of thinking is very difficult for a computer. So in a real Java program, we must tell the computer the type of each data item.
However, since pseudocode is designed strictly for humans and not computers, in pseudocode we don’t bother with type specification. Notice that Figure 2.2’s pseudocode representation of a Rectangle program does not contain any mention of data type. Pseudocode ignores data type so that focus can be kept on the algorithm’s essence—its instructions.
2.4 Operators and Assignment Statements
The previous section described variables by themselves. Now let’s consider relationships between variables
 by looking at operators and assignments.
Apago PDF Enhancer
Here is the third statement from Figure 2.2’s Rectangle algorithm:
set rectangleArea to length * width
As indicated earlier, the * symbol is the multiplication operator. The other common arithmetic operators are 􏰀 for addition, - for subtraction, and / for division. These should be familiar to everyone. The length and width variables are operands. In mathematics and again in programming, an operand is an entity (e.g., a variable or a value) that is operated on by an operator. The length and width variables are operands because they are operated on by the * operator.
When we say “set variableA to x,” we mean “put the value of x into variableA” or “assign the value of x to variableA.” So the set rectangleArea to length * width statement puts the product of length times width into the rectangleArea variable. A picture is worth a thousand words. See Figure 2.3—it visually describes what the statement does.
rectangleArea length width
40 (10*4(
Figure 2.3 Assignment (or “set”) operation represented by left-pointing arrow
Figure 2.3 includes a pair of parentheses not shown in the pseudocode statement. You could put these parentheses in the pseudocode if you wanted, but most people expect the multiplication operation to have a higher precedence (occur sooner) than the assignment operation, so we did not bother including parentheses in this particular pseudocode statement.














































































   60   61   62   63   64