in the last lesson we discussed about: boolean data type shorthand's for arithmetic operations
DESCRIPTION
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 ?. - PowerPoint PPT PresentationTRANSCRIPT
• In the last lesson we discussed about:
• Boolean data type• Shorthand's for arithmetic operations• Constants
• We will learn about:
• Using Boolean with conditions• While loop VS For loop• Drawing on the canvas using GLabel• Nested for loops
• 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!
General form: While (condition) { statements;}
Example:
int x=15;
while (x>1) {
x/=2;
println(x);
}
• 0 is not displayed
731
public class Add extends ConsoleProgram {
private static final int SENTINEL = 0;
public void run() {
int total=0;
int val = 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!
public class Add extends ConsoleProgram {
private static final int SENTINAL = 0;
public void run() {
int total=0;
while (true) {
int val = readInt(“Enter value:”);
if (val ==SENTINEL) break;
total+=val;
}
println(“The total is:” + total + “.”);
}
}
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
if (score>=90) {
println(“A”);
} else if (score>=80) {
println(“B”);
} else if (score>=70) {
println(“C”);
} else {
println (“Bad times!”);
}
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; }
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
Example:
for (int i=0; i<5; i++) {
println(i);
}
• As computer scientists we count from 0• The variable ‘i’ dies after the closing brace
01234
Example:
for (int i=6; i>0; i=-2) {
println(i);
}
• 0 is not displayed
642
• Before beginning to use ACM library you need to download it from:
• http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar
How to setup the acm.jar library
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);}
}