<< Chapter < Page Chapter >> Page >

Arithmetic operators

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.

Sequence

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.

Selection

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

In this analogy, you would check outside to confirm that it is not raining. If the condition is true (meaning that it isn't raining), you wouldplay tennis, go for a walk, and then relax on the beach. If it is raining, (meaning that the test condition is false), you would make some popcorn and relax in front of theTV.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask