Java Script 101 Lesson 3 Variables and Arithmetic

Java. Script 101 Lesson 3: Variables and Arithmetic

Lesson Topics n n n Use the arithmetic operators +, -, *, / to solve problems Use the assignment operator(=) to give a numeric value to a variable How operator precedence controls the order in which an expression is calculated Use the alert method to display information Use the Math object in calculations

Using Arithmetic Operators n n n Arithmetic operations in Java. Script are carried out with arithmetic symbols Same as math class (except multiplication is * rather than x) These symbols are called arithmetic operators

Arithmetic Operators(cont. ) n The addition operator + A + B yields the sum of A plus B n The subtraction operator – A - B yields the difference A minus B n The multiplication operator * A * B yields the product A multiplied by B n The division operator / A / B yields the dividend of A divided by B

Expressions n n n Expression is computer science term formula Expression in Java. Script can combine variables, numbers, and arithmetic operators Example: var num 1 = 6; var num 2 = 3; num 1 * 2 is an expression that yields 12 num 1/num 2 is an expression that yields 2

Assignment Operator and Expressions n Expressions used with the assignment operator give a value to a variable var length = 5; var width = 6; var area = length * width; (area now has a value of 30)

Expressions can combine arithmetic operators n Expressions can also have more than one operator: var length = 5; var width = 6; var perimeter; perimeter = length*2 + width * 2; n Perimeter now has a value of 22

Multiple operators The computer can only execute one operation at a time n When an expression has more than one operator, they have to be carried out in order determined by rule of mathematics known as “operator precedence” n

Operator precedence The operator precedence rules determine the order of evaluation n Next slide summarizes operator precedence n Operations are carried out in order from top to bottom n

Operator Precedence Table Type of Operator Example of Operators Parentheses (Overrides others) () Multiplication, Division *, / Addition, Subtraction +, - Assignment =

The alert Method n n n The alert method is used to display a small pop up window See example on p. 3 -5 Syntax: alert(“message”);

In the lab n n This lab uses arithmetic operators and alert statement Open Notepad and create a new HTML document named lesson 0301. html Enter the code on p. 3 -6 exactly as you see it Add code to calculate perimeter and display its value with alert statement (p. 3 -8)

Student Modifications n n Change the first document. write statement to an alert statement Add the following variables: base, height and triangle. Area Use assignment operator to store values in base and height Calculate and display the area of a triangle (formula is ½*base*height)

Student Modifications cont. n n n Add variables radius, circle. Area, circle. Circumference Use assignment operator to assign value to radius Calculate and display the area and circumference of a circle using the Math. PI is a defined constant that is part of Math object Math. PI stores the value of PI

Lesson Summary n n Arithmetic operators Operator precedence Using the alert method Using the Math object
- Slides: 15