Top Banner
Control Statements in Java
34

Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Aug 19, 2018

Download

Documents

lebao
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Statements in Java

Page 2: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Announcements

● Assignment 1 (Karel) due at 3:15PM today.● You can use a late period and submit on Wednesday of next

week by 3:15PM.● It's okay to use a late period on the Karel assignment –

this is your first time programming!

● Email assignment due Sunday.● Looking forward to meeting you!

● Assignment 2 (Welcome to Java!) goes out, is due on Monday, January 26 at 3:15PM.● Play around with graphics, control structures, and methods!● Some of these program require the use of methods. We'll

cover methods today and at the start of Wednesday's lecture.

Page 3: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Announcements

● Continuing a longstanding tradition, Eric Roberts will be showing a video of the “I Have a Dream” speech on Monday in Gates B12 at 2:15PM.

● Highly recommended, especially if you haven't seen it before.

Page 4: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Outline for Today

● The if Statement Revisited● Now with variables!

● The for Loop Revisited● Now with graphical goodies!

● Methods and Parameters● Customizing the behavior of your methods.

Page 5: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Structures

Page 6: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Structures

● When using Karel, we used these three control structures:● if statements.● for loops.● while loops.

● These exist in standard Java as well!● Let's see what they look like.

Page 7: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Structures

iffor

while

Page 8: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Structures

iffor

while

Page 9: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

if statements

if (condition) {… statements to run if condition holds …} else {… statements to run if condition doesn't hold …}

Page 10: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Based on slides by Mehran Sahami

Boolean Expressions

● A boolean expression is a test for a condition (it is either true or false).

● Value comparisons:

== “equals” (note: not single =)

!= “not equals”

> “greater than”

< “less than”

>= “greater than or equal to”

<= “less than or equal to”

Page 11: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Logical Operators

● We use logical operators combine or modify boolean values.● Logical NOT: !p

if (!isWeekday()) {

relaxAndUnwind();

}

● Logical AND: p && q if (youreHappy() && youKnowIt()) {

clapYourHands();

}

● Logical OR: p || q (inclusive OR)

if (hasPuppy() || hasKitty()) {

beHappy();

}

● Order of precedence given above.

Page 12: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Or else

if (condition) {… statements to run if condition holds …} else {… statements to run if condition doesn't hold …}

Page 13: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Cascading if

if (score >= 90) { println(" AWWWW YEAHHHHH "); } else if (score >= 80) { println(" <(^_^)> "); } else if (score >= 70) { println(" : - | "); } else if (score >= 60) { println(" ಠ_ ಠ "); } else { println(" ( ° °╯ □ )╯︵ ┻━┻ "); }

Based on slides by Mehran Sahami

Page 14: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Statements

iffor

while

Page 15: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Control Statements

iffor

while

Page 16: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

The Syntax

● As with Karel, to repeat a set of commands N times, use the following code:

for (int i = 0; i < N; i++) {

// … statements to execute …

}

● We'll talk about how exactly this works next time. For now, let's focus on what we can do with it!

Page 17: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Accessing the Counter

● Inside a for loop, the variable i keeps track of the index of the current loop, starting at 0.● First time through the loop: i = 0● Second time through the loop: i = 1● Third time through the loop: i = 2

● Let's see an example of this.

Page 18: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Accessing the Counter

● Suppose we want to print out the first fifteen multiples of 50 (0, 50, 100, …).

● We can accomplish this using a for loop.

for (int i = 0; i < 15; i++) {

println(i * 50);

}

● Do you see why?

Page 19: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Accessing the Counter

● Suppose we want to draw a row of boxes, like these:

● Suppose each box is 50 pixels wide and 50

pixels tall.● Look where their corners are... seem familiar?

Page 20: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Accessing the Counter

● Suppose we want to draw a row of boxes, like these:

● Suppose each box is 50 pixels wide and 50

pixels tall.● Look where their corners are... seem familiar?

(0, 0) (50, 0) (100, 0) (150, 0) (200, 0) (250, 0)

Page 21: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Double For Loops

● You can put for loops inside of for loops! This is sometimes called a double for loop.

● Syntax:

for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

// … statements to execute …

}

}

● This will run through all possible combinations of i and j where i is less than M and j is less than N.

Page 22: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Double For Loops

● Double for loops arise frequently when working with graphics.

● Suppose we want to draw this grid of boxes, each of which is 50 × 50:

● Notice anything about the corner positions?

Page 23: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Drawing a Checkerboard, Java Style

Page 24: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.
Page 25: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

0

1

2

3

4

5

6

7

0 1 2 3 4 5 6 7

Page 26: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

0 2 4 6

2 4 6

2 4 6 8

4 6 8

4 6 8 10

6 8 10

6 8 10 12

8 10 12

8

10

12

14

0

1

2

3

4

5

6

7

0 1 2 3 4 5 6 7

1

1

13

3

3

3

3

5

5

5

5

5

5

7

7

7

7

7

7

7

7

9

9

9

9

9

9

11

11

11

11 13

Page 27: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Methods Revisited

Page 28: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.
Page 29: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

0 (0o)

1 (72o)

(144o) 2

(216o) 3

4 (288o)

Page 30: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

0 (0o)

1 (51.43o)2 (102.9o)

(154.3o) 3

(205.7o) 4

(257.1o) 56 (308.6o)

Each point k is connected to point k + 2, after wrapping around.

Page 31: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Point k is at k

numSides×360o

0 (0o)

1 (51.43o)2 (102.9o)

(154.3o) 3

(205.7o) 4

(257.1o) 56 (308.6o)

Each point k is connected to point k + 2, after wrapping around.

Page 32: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

r

(x, y)

(x + r cos θ, y + r sin θ)

+y

Each point k is connected to point k + 2, after wrapping around.

1 (51.43o)

Point k is at k

numSides×360o

Page 33: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

r

(x, y)

(x + r cos θ, y – r sin θ)

Each point k is connected to point k + 2, after wrapping around.

+y

1 (51.43o)

Point k is at k

numSides×360o

Page 34: Control Statements in Java - Stanford University · Assignment 2 (Welcome to Java!) goes out, ... Play around with graphics, control structures, ... starting at 0.

Passing Parameters

● A method can accept parameters when it is called.

● Syntax:

private void name(parameters) {

/* … method body … */

}

● The values of the parameters inside the method are set when the method is called.

● The values of the parameters can vary between calls.