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

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

  • Upload
    aysel

  • View
    14

  • Download
    0

Embed Size (px)

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 Presentation

Citation preview

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

• In the last lesson we discussed about:

• Boolean data type• Shorthand's for arithmetic operations• Constants

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

• We will learn about:

• Using Boolean with conditions• While loop VS For loop• Drawing on the canvas using GLabel• Nested for loops

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

• 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!

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

General form: While (condition) { statements;}

Example:

int x=15;

while (x>1) {

x/=2;

println(x);

}

• 0 is not displayed

731

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

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!

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

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 + “.”);

}

}

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

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

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

if (score>=90) {

println(“A”);

} else if (score>=80) {

println(“B”);

} else if (score>=70) {

println(“C”);

} else {

println (“Bad times!”);

}

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

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; }

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

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

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

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

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

Example:

for (int i=6; i>0; i=-2) {

println(i);

}

• 0 is not displayed

642

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

• Before beginning to use ACM library you need to download it from:

• http://www-cs-faculty.stanford.edu/~eroberts/jtf/acm.jar

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

How to setup the acm.jar library

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

Create a new Project. In this case we call it

“MyGraphics”

Click Next after entering the new

project name

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

Add External Jar file by browsing to the

location of ACM.JAR

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

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);}

}