1 / 7

Assignment Statements, Expressions and Variables

Assignment Statements, Expressions and Variables. B. Ramamurthy Still in chapter 5. Assignment statement. Syntax LHS = RHS; variable = expression; Semantics: Evaluate the expression on the RHS and assign it as the value for the variable on the LHS Example f loat wages; float salary;

Télécharger la présentation

Assignment Statements, Expressions and Variables

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, Expressions and Variables B. Ramamurthy Still in chapter 5

  2. Assignment statement • Syntax LHS = RHS; variable = expression; • Semantics: Evaluate the expression on the RHS and assign it as the value for the variable on the LHS • Example float wages; float salary; float bonus; salary = wages + bonus;

  3. Arithmetic operators • + : addition • - : subtration • * : multiplication • / : division • Operator precedence: */ and then + - • Can use () to override precedence

  4. Examples int x = 5; int y = 6; int z = 10; int a; a = x * y; a = z/ x; a = z – y; a = y – z / x;

  5. Revisit while • More while statements • Lets write a while statement to compute sum to first n numbers : 0, 1, 2,3….N int sum = 0; intnum = 0; while (num <= N) { sum = sum + num; num = num + 1; } bg.drawString(“ sum to “ + N + “integers is” + sum);

  6. while example Draw n circles. int count = 0; while (count < n) { bg.drawOval(count *50, count*50, 50 , 50); count ++; }

  7. Two dice example • inti = 1; • while (i <= 2) • { • bg.setColor(Color.BLUE); • bg.fillRect(50*i,50*i,50,50); • bg.setColor(Color.WHITE); • int d = Greenfoot.getRandomNumber(6) +1; • bg.drawString(" "+d,40*i+20*i,70*i); • i++; • }

More Related