<< Chapter < Page | Chapter >> Page > |
Good programming design
It is a principle of good programming design that each function should perform only one task, and should perform it well. The task performed by thefunction named getHalf is to calculate and return half the value that it receives, and it does that task very well.
The division operation in Listing 2 introduced the use of arithmetic operators.
In computer programming jargon, we speak of operators and operands. Operators operate on operands.
As a real-world example, if you were to go to the hospital for knee surgery, the surgeon would be the operator and you would be the operand . The surgeon would operate on you.
Binary operators
The operators that we will use in the modules in this collection will usually be restricted to those that have two operands, a left operand and a right operand . (Operators with two operands are commonly called binary operators .)
In the function named getHalf in Listing 2 , the "/" character is the division operator. The left operand is incomingParameter and the right operand is 2 . The result is that the incoming parameteris divided by the right operand (2).
Binary arithmetic operators
The binary arithmetic operators supported by JavaScript are shown in Figure 3 .
Figure 3 . Binary arithmetic operators. |
---|
+ Addition: Adds the operands
- Subtraction: Subtracts the right operand from the left operand* Multiplication: Multiplies the operands
/ Division: Divides the left operand by the right operand% Modulus: Returns integer remainder of dividing the left operand
by the right operand |
We will use these operators extensively as we work through the physics exercises in future modules.
Of the four items listed under Structured programming earlier, the simplest one is sequence .
The concept of sequence in structured programming simply means that code statements can be executed in sequential order. Listing 2 provides a good example of the sequential execution of statements. The code in Listing 2 shows four sequential statements that begin with document.write.
The next item that we will discuss from the list under Structured programming is selection . While not as simple as sequence , selection is something that you probably do many times each day without even thinking about it. Therefore, once you understand it, it isn'tcomplicated.
General syntax for selection statement
The general syntax for a selection statement (often called an if-else statement) is shown in Figure 4 .
Figure 4 . General syntax for selection statement. |
---|
if(conditional expression is true){
execute code}else{//optional
execute alternative code}//end selection statement |
A selection statement performs a logical test that returns either true or false. Depending on the result, specific code is executed to control thebehavior of the script.
A real-world analogy
Figure 5 shows a real-world analogy of a selection statement that you might make on your day off.
Figure 5 . Real-world analogy of a selection statement. |
---|
if(it is not raining){
Play tennisGo for a walk
Relax on the beach}else{//optional
Make popcornWatch TV
}//end selection statement |
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?