Module 4 - Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java PDF

Title Module 4 - Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java
Author Ravi Patel
Course Computer Engineering
Institution Humber College
Pages 6
File Size 401 KB
File Type PDF
Total Downloads 41
Total Views 141

Summary

Assignment and the Arithmetic Expressions...


Description

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

ASSIGNMENT AND THE ARITHMETIC EXPRESSIONS An expression represents a computation involving values, variables, and operators that, taking them together, evaluates to a value. To assign a value to a variable you must place the variable name on the left of the assignment operator. We have already seen that we can assign a literal to a variable, as in, int y = 1;                     // Assigning 1 to variable y of integer type. double radius = 1.0;            // Assign 1.0 ti variable radius of double type int x = 5 * (3 / 2);              // Assign the value of the expression to x x = y + 1;                    // Assign the addition of y plus 1 to x double area = radius * radius * 3.14159; // Compute Area A variable can also be used in both sides of the = operator. For example x = x + 1; The result of x + 1 is assigned to x. If x was 1 before the statement is executed, then x becomes 2 after the statement is executed. The variable on the left must be compatible with the data type of the value on the right. For example x = 1.0;  would be illegal because the at a type of x is int. Type casting is necessary to resolve this (will be explained later)

Assignment Arithmetic Expressions

is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)  https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

1/6

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

Evaluating an Expression - Order of Precedence Though Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression.

Augmented Assignment Operators The operators of +, -, *, /, and % can be combined with the assignment operator (=) to form augmented operators, sometimes called compound assignment operators. Many times a variable is used, modified and then reassigned to the same variable. For example when we want to add 1 to a count variableas follows: count = count + 1; Java provides a way to combine the addition of 1 with the assignment using an assignment operator. The statement as above can then written be written using the addition assignment operator as: count += 1; The augmented assignment operator is performed last after all the other operators in the expression are evaluated. For example: x /= 4 + 5.5 * 1.5; https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

2/6

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

is equivalent to x = x / (4 + 5.5 * 1.5); Here is the list of the assignment operators

Increment and Decrement Operators The ++ and the --- are two shorthand operators for incrementing and decrementing a variable by 1. We will often need to increment or decrement by 1 later when we discuss iterating through our program.  For example the following code increments i by 1 and decrements j by 1. int i = 3, j = 3; i++;    // i becomes 4 j--;    // j becomes 2 i++ is pronounced as "i plus plus" and j-- is pronounced as "i minus minus" When the operators are placed after i++ is known as a postfix increment or post-increment i-- is known as a postfix decrement or or post-decrement When the operators are placed before ++i is known as a prefix increment or pre-increment -- i is known as a prefix decrement or pre-decrement The affects of the pre and post increment, decrement have very different affects when used in statements as follows.

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

3/6

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

Example A: Code show the effect of a post increment and pre decrement.

Line 10, The post increment of x occurs after the assignment of x to y. Therefore y has the value of 5.         x then is incremented by 1 and becomes 6. Line 12, shows the pre decrement of y prior to the print statement completion and x The console output is

Example B

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

4/6

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

Line 10, i is assigned the value of 10. Line 11, i is preincremented by 1, and i now becomes 11,        then 10 is multiplied by the 11 giving 110 and assigned to newNum The console output is

Example C

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

5/6

2/6/2020

Assignment and the Arithmetic Expressions - PROG78002 Introduction to Programming with Java

Line 13, x is post decremented by 1 therefore x maintains is value of 1 in the statement.        y is pre incremented by 1, therefore the value of y is 6.        z is therefore assigned the value of 7, value of x of 1 and value of y of 6 After line 13, x becomes 0 as a result of the post decrement in line 13. The console output is

Sheridan College. Faculty of Continuing and Professional Studies.

https://slate.sheridancollege.ca/d2l/le/content/657541/viewContent/8995325/View

6/6...


Similar Free PDFs