1 / 19

Assignment statements using the same variable in LHS and RHS

Assignment statements using the same variable in LHS and RHS. Previously discussed. Assignment statement: Syntax of an assignment statement :. The assignment statement in the Java programming language instructs the computer to update the value stored in a variable.

Download Presentation

Assignment statements using the same variable in LHS and RHS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment statements using the same variable in LHS and RHS

  2. Previously discussed • Assignment statement: • Syntax of an assignment statement: • The assignment statement in the Java programming language instructs the computer to update the value stored in a variable VariableName = Expression ;

  3. Previously discussed (cont.) • Meaning of the assignment statement: • The expression on the RHS of the "=" operator is first computed • The computed value is then assigned to the receiving variable

  4. Assignment statements with the same variable on the LHS and RHS • Consider the following program: public class Assign01 { public static void main(String[] args) { double x = 1.0; // x = 1.0 System.out.print("Before: x = "); System.out.println(x); x = x + 4.0; // x is used in the LHS and RHS System.out.print("After: x = "); System.out.println(x); } }

  5. Assignment statements with the same variable on the LHS and RHS (cont.) • Explanation: • You need to realize that the symbol "=" does not represent an equality relationship • The symbol "=" denotes an assignment operation: • 1. The computer will first evaluate the LHS "x + 4.0": LHS = x + 4.0 (x contains 1.0) = 1.0 + 4.0 = 5.0

  6. Assignment statements with the same variable on the LHS and RHS (cont.) Therefore, after executing the statement "x = x + 4.0", the variable x contains the value 5.0 • 2. Then the result 5.0 is assigned to the receiving variable x: x = 5.0;

  7. Assignment statements with the same variable on the LHS and RHS (cont.) • Example Program: (Demo above code)   • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Assign01.java • How to run the program:           • Right click on link and save in a scratch directory • To compile:   javac Assign01.java • To run:          java Assign01

  8. Example program: interest computation • Problem statement: Write a Java program that read in the following information: The program prints the principle amount for the next 3 years (with the interest added to the principle) • A principle amount principle • A interest rate interest_rate (given in percents)

  9. Example program: interest computation (cont.) • Algorithm: • Let p0 = principle and i = interest_rate • The principle amount after 1 year is:   p1 = (1.0 + i/100.0)×p0 • The principle amount after 2 year is:   p2 = (1.0 + i/100.0)×p1 • The principle amount after 3 year is:   p3 = (1.0 + i/100.0)×p2

  10. Example program: interest computation (cont.) • A preliminary version of the Java program: import java.util.Scanner; public class Interest01 { public static void main(String[] args) { double principle, interest_rate; double p1, p2, p3; Scanner in = new Scanner(System.in); // Construct a Scanner object System.out.print("Enter principle = "); principle = in.nextDouble(); // Read in principle System.out.print("Enter interest rate = "); interest_rate = in.nextDouble(); // Read in interest rate

  11. Example program: interest computation (cont.) p1 = (1.0 + interest_rate/100.0) * principle; System.out.print("Principle after 1 year = "); System.out.println(p1); p2 = (1.0 + interest_rate/100.0) * p1; System.out.print("Principle after 2 year = "); System.out.println(p2); p3 = (1.0 + interest_rate/100.0) * p2; System.out.print("Principle after 3 year = "); System.out.println(p3); } }

  12. Example program: interest computation (cont.) • Comments: • This program works, but it uses an unnecessarily large number of variables • We can use the variable principle to record the year-to-year principle amount.

  13. Example program: interest computation (cont.) • Example: • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Interest01.java • How to run the program • Right click on link and save in a scratch directory • To compile:   javac Interest01.java • To run:          java Interest01

  14. Advice in writing programs • Programming principle: • Each variable in the program stores one piece of information • A good programming practice is to assign a meaning to each variable in the program • Giving a meaning to each variable will help you understand the steps of the algorithm

  15. Improved program to compute interest • We will re-write the preliminary version of the program using the following meaning of the variables: • principle = the current amount of principle • interest_rate = interest rate paid

  16. Improved program to compute interest (cont.) • Java program: import java.util.Scanner; public class Interest01 { public static void main(String[] args) { double principle, interest_rate; Scanner in = new Scanner(System.in); // Construct a Scanner object System.out.print("Enter principle = "); principle = in.nextDouble(); // Read in principle System.out.print("Enter interest rate = "); interest_rate = in.nextDouble(); // Read in interest rate

  17. Improved program to compute interest (cont.) principle = (1.0 + interest_rate/100.0) * principle; System.out.print("Principle after 1 year = "); System.out.println(principle); principle = (1.0 + interest_rate/100.0) * principle; System.out.print("Principle after 2 year = "); System.out.println(principle); principle = (1.0 + interest_rate/100.0) * principle; System.out.print("Principle after 3 year = "); System.out.println(principle); } }

  18. Improved program to compute interest (cont.) • Explanation: • By storing the computed value "(1.0 + interest_rate/100.0) * principle" back into the variable principle, this variable principle will contains the correct value to be used in the next computation !!!

  19. Improved program to compute interest (cont.) • Example: • Prog file: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/Progs/Interest02.java • How to run the program: • Right click on link and save in a scratch directory • To compile:   javac Interest02.java • To run:          java Interest02

More Related