180 likes | 336 Views
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 ?.
E N D
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 ? • 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!
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
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!
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 + “.”); } }
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
Cascading If if (score>=90) { println(“A”); } else if (score>=80) { println(“B”); } else if (score>=70) { println(“C”); } else { println (“Bad times!”); }
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; }
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
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
For loop in reverse Example: for (inti=6; i>0; i=-2) { println(i); } • 0 is not displayed 6 4 2
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
Create a new Project. In this case we call it “MyGraphics” Click Next after entering the new project name
Add External Jar file by browsing to the location of ACM.JAR
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); } }