darrell bethea may 19 2011
play

Darrell Bethea May 19, 2011 1 Program 2 due Monday Program 3 - PowerPoint PPT Presentation

Darrell Bethea May 19, 2011 1 Program 2 due Monday Program 3 assigned today Midterm in one week 2 3 int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i; } else {


  1. Darrell Bethea May 19, 2011 1

  2.  Program 2 due Monday  Program 3 assigned today  Midterm in one week 2

  3. 3

  4. int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i; } else { oddSum = oddSum + i; } } 4

  5. int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i;} else { oddSum = oddSum + i; } } 5

  6. int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i; } else { oddSum = oddSum + i; } } 6

  7.  Indentation ◦ Makes code easier to read ◦ Helps with finding syntax and logic errors ◦ Indent code that goes between { and }  Be consistent! 7

  8.  Brackets are required when your if/else or loop contains > 1 line of code!  Brackets are highly recommended even when your if/else or loop contains 1 line of code  Please use brackets around all your if/else or loop statements from now on! 8

  9.  Variables declared in outer scopes are visible to code inside inner scopes public static void main(String[] args) { int total = 15; int n = 5; inner if (n <= 10) { outer total = total + n; } System.out.println(total); } 9

  10.  Variables declared in inner scopes are NOT visible to outer code public static void main(String[] args) { int n = 5; inner if (n <= 10) { outer int total = 15 + n; } System.out.println(total); // ERROR!!! } 10

  11. if (inputString.equals(“”)) canvas.setColor(Color.BLACK); else if (inputString.equals(“BLUE”)) canvas.setColor(Color.BLUE); else if (inputString.equals(“GREEN”)) canvas.setColor(Color.GREEN); else if (inputString.equals(“RED”)) canvas.setColor(Color.RED); else canvas.setColor(Color.WHITE); 11

  12. if (inputString.equals(“MOUTH”)) { mouthStartAngle = 0; } else { } Also not needed when you are setting a variable to the same value it already has: ( mouthStartAngle = 180; ) 12

  13. if (inputString.equals(“MOUTH”)) { mouthStartAngle = 0; } if (inputString.equals(“EYES”)) { eyeColor = JOptionPane.showInputDialog(“Enter an eye color.”); } Compiler tests both statements even if 1 st one is true 13

  14. if (inputString.equals(“MOUTH”)) { mouthStartAngle = 0; } else if (inputString.equals(“EYES”)) { eyeColor = JOptionPane.showInputDialog(“Enter an eye color.”); } Compiler only tests else if statement if the 1 st if statement is false 14

  15. Tomorrow  Read Section 5.1  Classes ◦ You will need them to complete Program 3 18

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend