1 / 18

In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations

Review. In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations Constants. Today. We will learn about: Using Boolean with conditions While loop VS For loop Drawing on the canvas using GLabel Nested for loops. Remember the while loop ?.

aysel
Télécharger la présentation

In the last lesson we discussed about: Boolean data type Shorthand's for arithmetic operations

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. Review • In the last lesson we discussed about: • Boolean data type • Shorthand's for arithmetic operations • Constants

  2. Today • We will learn about: • Using Boolean with conditions • While loop VS For loop • Drawing on the canvas using GLabel • Nested for loops

  3. Remember the while loop ? • It is a indefinite loop. • There are times when you don’t know how many times you’re going to do something.So you want to use a do while loop. • It is not infinite!

  4. While loop General form: While (condition) { statements;} Example: int x=15; while (x>1) { x/=2; println(x); } • 0 is not displayed 7 3 1

  5. Loop and a half public class Add extends ConsoleProgram { private static final int SENTINEL = 0; public void run() { int total=0; intval = readInt(“Enter value:”); while (val != SENTINEL) { total+=val; // short hand val = readInt(“Enter value:”); } println(“The total is:” + total + “.”); } } Not a good idea to duplicate code!

  6. Loop and a half public class Add extends ConsoleProgram { private static final int SENTINAL = 0; public void run() { int total=0; while (true) { intval = readInt(“Enter value:”); if (val ==SENTINEL) break; total+=val; } println(“The total is:” + total + “.”); } }

  7. for versus while

  8. If statement General form: if (condition) { statements } if ( ( num % 2 ) ==0 ) { println(“num is even”); } else { println(“number is odd”); println(“and so are you!”); } Its always a good idea to use braces even if there is only one statement

  9. Cascading If if (score>=90) { println(“A”); } else if (score>=80) { println(“B”); } else if (score>=70) { println(“C”); } else { println (“Bad times!”); }

  10. Switch statement int day = readInt(“Enter Day of the week as an integer:”); switch (day) { case 0: println(“Sunday”); break; case 6: println(“Saturday”); break; default: println(“Weekday”); break; }

  11. For loop General form: for ( init; condition; step) { statements; } Init: Done once at the start of the loop (initialization) Condition: checked before every iteration through loop Statements get executed if condition is true

  12. For loop Example: for (inti=0; i<5; i++) { println(i); } • As computer scientists we count from 0 • The variable ‘i’ dies after the closing brace 0 1 2 3 4

  13. For loop in reverse Example: for (inti=6; i>0; i=-2) { println(i); } • 0 is not displayed 6 4 2

  14. Downloading ACM.JAR • Before beginning to use ACM library you need to download it from: • http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar

  15. How to setup the acm.jar library

  16. Create a new Project. In this case we call it “MyGraphics” Click Next after entering the new project name

  17. Add External Jar file by browsing to the location of ACM.JAR

  18. Using acm.program library to display text on Canvas using Glabel class import acm.program.*; import acm.graphics.*; import java.awt.*; public class acm extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello world!",100,75); label.setColor(Color.RED); label.setFont(“Arial-24”); add(label); } }

More Related