assignment statements using the same variable in lhs and rhs n.
Download
Skip this Video
Download Presentation
Assignment statements using the same variable in LHS and RHS

Loading in 2 Seconds...

play fullscreen
1 / 19

Assignment statements using the same variable in LHS and RHS - PowerPoint PPT Presentation


  • 142 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Assignment statements using the same variable in LHS and RHS' - elijah-mills


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
previously discussed
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 ;

previously discussed cont
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
assignment statements with the same variable on the lhs and rhs
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);

}

}

assignment statements with the same variable on the lhs and rhs cont
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

assignment statements with the same variable on the lhs and rhs cont1
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;

assignment statements with the same variable on the lhs and rhs cont2
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
example program interest computation
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)
example program interest computation cont
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
example program interest computation cont1
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

example program interest computation cont2
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);

}

}

example program interest computation cont3
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.
example program interest computation cont4
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
advice in writing programs
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
improved program to compute interest
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
improved program to compute interest cont
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

improved program to compute interest cont1
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);

}

}

improved program to compute interest cont2
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 !!!
improved program to compute interest cont3
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