Scratch Programming - Rutgers CS

29
1 Scratch Programming Lesson 5: Multiple Choice Decisions Jt Scratch Lesson 5 Fall 2011 • slide 2 Problem ! Write a script to display a letter grade based on a numeric score a student received in a class where the typical scale is used. Jt Scratch Lesson 5 Fall 2011 • slide 3 Problem ! Write a script to display a letter grade based on a numeric score a student received in a class where the typical scale is used. ! For example: a 97 gets an A; a 72 gets a C Jt Scratch Lesson 5 Fall 2011 • slide 4 Problem ! Write a script to display a letter grade based on a numeric score a student received in a class where the typical scale is used. ! For example: a 97 gets an A; a 72 gets a C ! Lets try out a full multimedia version of the code: Demo: Grading Jt Scratch Lesson 5 Fall 2011 • slide 5 Essence of the Problem ! The script must decide what letter grade to assign Jt Scratch Lesson 5 Fall 2011 • slide 6 Essence of the Problem ! The script must decide what letter grade to assign

Transcript of Scratch Programming - Rutgers CS

1

Scratch Programming

Lesson 5: Multiple Choice Decisions

Jt Scratch Lesson 5 Fall 2011 • slide 2

Problem

! Write a script to display a letter grade based

on a numeric score a student received in a

class where the typical scale is used.

Jt Scratch Lesson 5 Fall 2011 • slide 3

Problem

! Write a script to display a letter grade based

on a numeric score a student received in a

class where the typical scale is used.

! For example: a 97 gets an A; a 72 gets a C

Jt Scratch Lesson 5 Fall 2011 • slide 4

Problem

! Write a script to display a letter grade based

on a numeric score a student received in a

class where the typical scale is used.

! For example: a 97 gets an A; a 72 gets a C

! Lets try out a full multimedia version of the

code: Demo: Grading

Jt Scratch Lesson 5 Fall 2011 • slide 5

Essence of the Problem

! The script must decide what letter grade to

assign

Jt Scratch Lesson 5 Fall 2011 • slide 6

Essence of the Problem

! The script must decide what letter grade to

assign

2

Jt Scratch Lesson 5 Fall 2011 • slide 7

Essence of the Problem

! The script must decide what letter grade to

assign

! Or put another way, it must decide whether

or not to give an A; decide whether or not to

give a B and so on

Jt Scratch Lesson 5 Fall 2011 • slide 8

Essence of the Problem

! The script must decide what letter grade to

assign

! Or put another way, it must decide whether

or not to give an A; decide whether or not to

give a B and so on

! What do you use in Scratch to decide

things?

Jt Scratch Lesson 5 Fall 2011 • slide 9

Essence of the Problem

! The script must decide what letter grade to

assign

! Or put another way, it must decide whether

or not to give an A; decide whether or not to

give a B and so on

! What do you use in Scratch to decide

things? An IF or IF/ELSE

Jt Scratch Lesson 5 Fall 2011 • slide 10

Essence of the Problem

! The script must decide what letter grade toassign

! Or put another way, it must decide whetheror not to give an A; decide whether or not togive a B and so on

! Use an IF or IF/ELSE

! By the way, we essentially have to make amultiple choice decision. We will comeback to this.

Jt Scratch Lesson 5 Fall 2011 • slide 11

The Simple Stuff

! Variable: Score

Jt Scratch Lesson 5 Fall 2011 • slide 12

The Simple Stuff

! Variable: Score

person’s numeric

score

3

Jt Scratch Lesson 5 Fall 2011 • slide 13

The Simple Stuff

! Variable: Score

! Start & Input:

Jt Scratch Lesson 5 Fall 2011 • slide 14

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

Jt Scratch Lesson 5 Fall 2011 • slide 15

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output:

Jt Scratch Lesson 5 Fall 2011 • slide 16

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output: Say Letter

Jt Scratch Lesson 5 Fall 2011 • slide 17

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output: Say Letter

Jt Scratch Lesson 5 Fall 2011 • slide 18

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output: Say Letter

and so on

4

Jt Scratch Lesson 5 Fall 2011 • slide 19

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output: Say Letter

and so on

appropriate letter is said

Jt Scratch Lesson 5 Fall 2011 • slide 20

The Simple Stuff

! Variable: Score

! Start & Input: Ask for Score

! Output: Say Letter

Jt Scratch Lesson 5 Fall 2011 • slide 21

The Decisions

! Decide if they get an A:

Jt Scratch Lesson 5 Fall 2011 • slide 22

The Decisions

! Decide if they get an A:

Jt Scratch Lesson 5 Fall 2011 • slide 23

The Decisions

! Decide if they get an A:

Jt Scratch Lesson 5 Fall 2011 • slide 24

The Decisions

! Decide if they get an A: appropriate condition?

5

Jt Scratch Lesson 5 Fall 2011 • slide 25

The Decisions

! Decide if they get an A:

Jt Scratch Lesson 5 Fall 2011 • slide 26

The Decisions

! What are the rest of the decisions like?

Jt Scratch Lesson 5 Fall 2011 • slide 27

The Decisions

! What are the rest of the decisions like?

Pretty much the same

Jt Scratch Lesson 5 Fall 2011 • slide 28

The Decisions

! Decide if they get an B:

Jt Scratch Lesson 5 Fall 2011 • slide 29

The Decisions

! Decide if they get an B:

Jt Scratch Lesson 5 Fall 2011 • slide 30

The Decisions

! Decide if they get an B:

6

Jt Scratch Lesson 5 Fall 2011 • slide 31

The Decisions

! Decide if they get an C:

Jt Scratch Lesson 5 Fall 2011 • slide 32

The Decisions

! Decide if they get an C:

Jt Scratch Lesson 5 Fall 2011 • slide 33

The Decisions

! Decide if they get an C:

Jt Scratch Lesson 5 Fall 2011 • slide 34

The Decisions

! Only two letters left.

Jt Scratch Lesson 5 Fall 2011 • slide 35

The Decisions

! Only two letters left. What structure can we

use to decide between two options?

Jt Scratch Lesson 5 Fall 2011 • slide 36

The Decisions

! Only two letters left. What structure can we

use to decide between two options?

7

Jt Scratch Lesson 5 Fall 2011 • slide 37

The Decisions

! Only two letters left. What structure can we

use to decide between two options?

Jt Scratch Lesson 5 Fall 2011 • slide 38

The Decisions

! Only two letters left.

Jt Scratch Lesson 5 Fall 2011 • slide 39

The Decisions

! Only two letters left. Condition?

Jt Scratch Lesson 5 Fall 2011 • slide 40

The Decisions

! Only two letters left.

Jt Scratch Lesson 5 Fall 2011 • slide 41

The Decisions

! Only two letters left.

Jt Scratch Lesson 5 Fall 2011 • slide 42

The Complete

Script

8

Jt Scratch Lesson 5 Fall 2011 • slide 43

The Complete

ScriptA?

B?

C?

D?

or F?

Four separate IFs

Jt Scratch Lesson 5 Fall 2011 • slide 44

The Complete

ScriptA?

B?

C?

D?

or F?

Four separate IFs

Demo: Grading 1

Jt Scratch Lesson 5 Fall 2011 • slide 45

Fixing the Script

! What is wrong?

Jt Scratch Lesson 5 Fall 2011 • slide 46

Fixing the Script

! What is wrong?

! Scripts always run all the way through.

Jt Scratch Lesson 5 Fall 2011 • slide 47

Fixing the Script

! What is wrong?

! Scripts always run all the way through. Just

because the first IF figured out an A should

be said, doesn’t mean the code stops.

Jt Scratch Lesson 5 Fall 2011 • slide 48

Fixing the Script

! What is wrong?

! Scripts always run all the way through. Just

because the first IF figured out an A should

be said, doesn’t mean the code stops. The

code still goes on to the 2nd IF and then the

3rd and so on ….

9

Jt Scratch Lesson 5 Fall 2011 • slide 49

Fixing the Script

! So lets consider the 2nd decision carefully:

Jt Scratch Lesson 5 Fall 2011 • slide 50

Fixing the Script

! So lets consider the 2nd decision carefully:

Jt Scratch Lesson 5 Fall 2011 • slide 51

Fixing the Script

! So lets consider the 2nd decision carefully:

See anything wrong here?

Jt Scratch Lesson 5 Fall 2011 • slide 52

Fixing the Script

! So lets consider the 2nd decision carefully:

Works for 84.

Jt Scratch Lesson 5 Fall 2011 • slide 53

Fixing the Script

! So lets consider the 2nd decision carefully:

Works for 84. Works for 86.

Jt Scratch Lesson 5 Fall 2011 • slide 54

Fixing the Script

! So lets consider the 2nd decision carefully:

What about 92?

10

Jt Scratch Lesson 5 Fall 2011 • slide 55

Fixing the Script

! So lets consider the 2nd decision carefully:

What about 92? Whoops!

Jt Scratch Lesson 5 Fall 2011 • slide 56

Fixing the Script

! We have not been precise enough.

Jt Scratch Lesson 5 Fall 2011 • slide 57

Fixing the Script

! We have not been precise enough.

The condition is not exactly right.

Jt Scratch Lesson 5 Fall 2011 • slide 58

Fixing the Script

! What must be true about the score?

Jt Scratch Lesson 5 Fall 2011 • slide 59

Fixing the Script

! What must be true about the score? Yes it must

be more than 80 ….

Jt Scratch Lesson 5 Fall 2011 • slide 60

Fixing the Script

! What must be true about the score? Yes it must

be more than 80 …. but also less than 90

11

Jt Scratch Lesson 5 Fall 2011 • slide 61

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

Jt Scratch Lesson 5 Fall 2011 • slide 62

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

What do you use to combine two conditions

together …

Jt Scratch Lesson 5 Fall 2011 • slide 63

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

What do you use to combine two conditions

together … and both must be true?Jt Scratch Lesson 5 Fall 2011 • slide 64

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

Use an AND

Jt Scratch Lesson 5 Fall 2011 • slide 65

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

Use an AND

Jt Scratch Lesson 5 Fall 2011 • slide 66

Fixing the Script

! Yes it must be more than 80 …. but also less

than 90. Both of these conditions must be true.

Use an AND

12

Jt Scratch Lesson 5 Fall 2011 • slide 67

Fixing the Script

! Lets adjust the other two conditions also

Jt Scratch Lesson 5 Fall 2011 • slide 68

Fixing the Script

! Lets adjust the other two conditions also

a C is above 70 but also less than 80

Jt Scratch Lesson 5 Fall 2011 • slide 69

Fixing the Script

! Lets adjust the other two conditions also

a D is above 60 but also less than 70

Jt Scratch Lesson 5 Fall 2011 • slide 70

Revised Script

Jt Scratch Lesson 5 Fall 2011 • slide 71

Revised ScriptA?

B?

C?

D?

or F?

Jt Scratch Lesson 5 Fall 2011 • slide 72

Revised Script

Demo:

Grading 2

A?

B?

C?

D?

or F?

13

Jt Scratch Lesson 5 Fall 2011 • slide 73

Fixing the Script Again

! Close but not quite.

Jt Scratch Lesson 5 Fall 2011 • slide 74

Fixing the Script Again

! Close but not quite. Now what is wrong?

Jt Scratch Lesson 5 Fall 2011 • slide 75

Fixing the Script Again

! Close but not quite. Now what is wrong?

Remember, just because an earlier IF is true

and causes a grade to be said - this doesn’t

mean the script stops.

Jt Scratch Lesson 5 Fall 2011 • slide 76

Fixing the Script Again

! Close but not quite. Now what is wrong?

Remember, just because an earlier IF is true

and causes a grade to be said - this doesn’t

mean the script stops. It eventually makes it to

the 4th decision (IF) where the F seems to be

said too.

Jt Scratch Lesson 5 Fall 2011 • slide 77

Fixing the Script Again

! Close but not quite. Now what is wrong?

Remember, just because an earlier IF is true

and causes a grade to be said - this doesn’t

mean the script stops. It eventually makes it to

the 4th decision (IF) where the F seems to be

said too. Why?

Jt Scratch Lesson 5 Fall 2011 • slide 78

Fixing the Script Again

! Even after the A is said, it still eventually

makes it to the 4th decision (IF) where the F

seems to be said too. Let’s look at this 4th IF.

14

Jt Scratch Lesson 5 Fall 2011 • slide 79

Fixing the Script Again

! Even after the A is said, it still eventually

makes it to the 4th decision (IF) where the F

seems to be said too. Let’s look at this 4th IF.

Jt Scratch Lesson 5 Fall 2011 • slide 80

Fixing the Script Again

! Its an IF/ELSE.

Jt Scratch Lesson 5 Fall 2011 • slide 81

Fixing the Script Again

! Its an IF/ELSE. Is it ever possible for an

IF/ELSE to do nothing?

Jt Scratch Lesson 5 Fall 2011 • slide 82

Fixing the Script Again

! Its an IF/ELSE. Is it ever possible for an

IF/ELSE to do nothing? NO!

Jt Scratch Lesson 5 Fall 2011 • slide 83

Fixing the Script Again

! Its an IF/ELSE. Is it ever possible for an

IF/ELSE to do nothing? NO! It always does

one of its two parts.

Jt Scratch Lesson 5 Fall 2011 • slide 84

Fixing the Script Again

! Is it ever possible for an IF/ELSE to do

nothing? NO! It always does one of its two

parts. So either a D or F will always print !

15

Jt Scratch Lesson 5 Fall 2011 • slide 85

Fixing the Script Again

! So either a D or F will always print ! Every time

we run this script, no matter what else it does - a

D or F will always be the last item to print.

Jt Scratch Lesson 5 Fall 2011 • slide 86

Fixing the Script Again

! How can we fix this?

Jt Scratch Lesson 5 Fall 2011 • slide 87

Fixing the Script Again

! How can we fix this? Lets not use and IF/ELSE.

Jt Scratch Lesson 5 Fall 2011 • slide 88

Fixing the Script Again

! How can we fix this? Lets not use and IF/ELSE.

We can break this into two separate regular IFs

(like we did for the earlier grades.)

Jt Scratch Lesson 5 Fall 2011 • slide 89

Fixing the Script Again

! Two separate regular IFs.

Jt Scratch Lesson 5 Fall 2011 • slide 90

Fixing the Script Again

! Two separate regular IFs. No ELSE used.

16

Jt Scratch Lesson 5 Fall 2011 • slide 91

Latest Script

Jt Scratch Lesson 5 Fall 2011 • slide 92

Latest Script

Five Separate IFs

1. A?

2. B?

3. C?

4. D?

5. F?

Jt Scratch Lesson 5 Fall 2011 • slide 93

Latest Script

Five Separate Ifs

Three of them had to

use ANDs

1. A?

2. B?

3. C?

4. D?

5. F?

Jt Scratch Lesson 5 Fall 2011 • slide 94

Latest Script

Five Separate Ifs

Three of them had to

use ANDs

Bit more complicated

than our first

attempt.

1. A?

2. B?

3. C?

4. D?

5. F?

Jt Scratch Lesson 5 Fall 2011 • slide 95

Latest Script

Demo:

Grading 3

1. A?

2. B?

3. C?

4. D?

5. F?

Jt Scratch Lesson 5 Fall 2011 • slide 96

Latest Script

Demo:

Grading 3

It finally works right.

1. A?

2. B?

3. C?

4. D?

5. F?

17

Jt Scratch Lesson 5 Fall 2011 • slide 97

Reviewing The Issue

! There was a fairly simple problem. Assign a

letter grade given a numeric score.

! Our original code seemed simple enough. But

the simple approach just didn’t work well.

! In the end, to get it to work right, we needed

five separate IF statements, many of which

needed to use AND conditions.

Jt Scratch Lesson 5 Fall 2011 • slide 98

Reviewing The Issue

! There was a fairly simple problem. Assign a

letter grade given a numeric score.

! Our original code seemed simple enough. But

the simple approach just didn’t work well.

! In the end, to get it to work right, we needed

five separate IF statements, many of which

needed to use AND conditions.

! Why?

Jt Scratch Lesson 5 Fall 2011 • slide 99

Insight

! The main issue is each IF is a separate statement

- each making its own decision. So, that means

more than one of them can come out TRUE.

Jt Scratch Lesson 5 Fall 2011 • slide 100

Insight

! The main issue is each IF is a separate statement

- each making its own decision. So, that means

more than one of them can come out TRUE.

! But, we don’t want more than one to be TRUE.

Jt Scratch Lesson 5 Fall 2011 • slide 101

Insight

! The main issue is each IF is a separate statement

- each making its own decision. So, that means

more than one of them can come out TRUE.

! But, we don’t want more than one to be TRUE.

! We have a five way decision. We want exactly

one of five possible grades to be assigned.

Jt Scratch Lesson 5 Fall 2011 • slide 102

Insight

! The main issue is each IF is a separate statement

- each making its own decision. So, that means

more than one of them can come out TRUE.

! But, we don’t want more than one to be TRUE.

! We have a five way decision. We want exactly

one of five possible grades to be assigned.

! Yet, IF/ELSEs by their nature, can only make a

two way decision.

18

Jt Scratch Lesson 5 Fall 2011 • slide 103

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

Jt Scratch Lesson 5 Fall 2011 • slide 104

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

Jt Scratch Lesson 5 Fall 2011 • slide 105

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

A?

or not?

Jt Scratch Lesson 5 Fall 2011 • slide 106

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

A?

or not?Indicate person gets an A

Jt Scratch Lesson 5 Fall 2011 • slide 107

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

A?

or not?Indicate person gets an A

Indicate which of the other

grades the person gets

Jt Scratch Lesson 5 Fall 2011 • slide 108

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

Indicate person gets an A

Indicate which of the other

grades the person gets

19

Jt Scratch Lesson 5 Fall 2011 • slide 109

Another Approach

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

So, how do

this?

Indicate person gets an A

Indicate which of the other

grades the person gets

Jt Scratch Lesson 5 Fall 2011 • slide 110

Side Lesson

! What statements can you place inside the

sections of an IF/ELSE statement?

Jt Scratch Lesson 5 Fall 2011 • slide 111

Side Lesson

! What statements can you place inside the

sections of an IF/ELSE statement?

Answer:

any type

of statement

can placed

here

Jt Scratch Lesson 5 Fall 2011 • slide 112

Side Lesson

! But an IF statement is a type of statement.

Answer:

any type

of statement

can placed

here

Jt Scratch Lesson 5 Fall 2011 • slide 113

Side Lesson

! But an IF statement is a type of statement. So an

IF can be placed inside another IF.

Answer:

any type

of statement

can placed

here

Jt Scratch Lesson 5 Fall 2011 • slide 114

Example: Problem

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift.

20

Jt Scratch Lesson 5 Fall 2011 • slide 115

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift.

Give Toy To Kids

Jt Scratch Lesson 5 Fall 2011 • slide 116

Example : Focus on Toy

! Not everyone gets the

toy.

Jt Scratch Lesson 5 Fall 2011 • slide 117

Example : Focus on Toy

! Not everyone gets the

toy. You only give it

to some people.

Jt Scratch Lesson 5 Fall 2011 • slide 118

Example : Focus on Toy

! Not everyone gets the

toy. You only give it

to some people. You

have to decide to give

it or not.

Jt Scratch Lesson 5 Fall 2011 • slide 119

Example : Focus on Toy

! Not everyone gets the

toy. You only give it

to some people. You

have to decide to give

it or not. What do

you use in Scratch to

make a decision?

Jt Scratch Lesson 5 Fall 2011 • slide 120

Example : Focus on Toy

! Not everyone gets the

toy. You only give it

to some people. You

have to decide to give

it or not. What do

you use in Scratch to

make a decision? Use

an IF.

21

Jt Scratch Lesson 5 Fall 2011 • slide 121

Example : Focus on Toy

! Use an IF. What is

our condition? What

has to be true about

the person for them

to get a toy?

Jt Scratch Lesson 5 Fall 2011 • slide 122

Example : Focus on Toy

! Use an IF. What is

our condition? What

has to be true about

the person for them

to get a toy?

Jt Scratch Lesson 5 Fall 2011 • slide 123

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift.

Give Toy To Kids

Back to our main theater code

Jt Scratch Lesson 5 Fall 2011 • slide 124

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift.

Give Toy To Kids

We have to use the code we

just created for the toy here

Jt Scratch Lesson 5 Fall 2011 • slide 125

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift. We have to use the code we

just created for the toy here

Jt Scratch Lesson 5 Fall 2011 • slide 126

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift. Here we have an IF

22

Jt Scratch Lesson 5 Fall 2011 • slide 127

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift. Here we have an IF

inside another IF

Jt Scratch Lesson 5 Fall 2011 • slide 128

Example : Code Segment

! Tuesday is a special

day at a movie

theater. Everyone

gets in for $6, gets a

free drink of their

choice, and kids

under 10 years old,

get a free gift. This is called a nested IF

Jt Scratch Lesson 5 Fall 2011 • slide 129

Back To Grading

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

So, how do

this?

Indicate person gets an A

Indicate which of the other

grades the person gets

Jt Scratch Lesson 5 Fall 2011 • slide 130

Back To Grading

! Lets look at the skeleton of an IF/ELSE that

makes a two-way decision but assigns the right

grade.

We can use

a nested IF

Indicate person gets an A

Indicate which of the other

grades the person gets

Jt Scratch Lesson 5 Fall 2011 • slide 131

Back To Grading

! Put code for other grades in the ELSE section of

our A? IF.

Jt Scratch Lesson 5 Fall 2011 • slide 132

Back To Grading

! Put code for other grades in the ELSE section of

our A? IF.

give other grade

Code for all

other grades:

Either a B

or the rest

23

Jt Scratch Lesson 5 Fall 2011 • slide 133

Back To Grading

! This is a nested IF.

give other grade

Jt Scratch Lesson 5 Fall 2011 • slide 134

Back To Grading

! This is a nested IF. You can nest more than

once.

give other grade

Code to

give C or

other grade

Jt Scratch Lesson 5 Fall 2011 • slide 135

Back To Grading

! This is a nested IF. You can nest more than

once.

give other grade

Nest it

Jt Scratch Lesson 5 Fall 2011 • slide 136

Back To Grading

! This is an IF inside an IF inside an IF.

give other grade

Jt Scratch Lesson 5 Fall 2011 • slide 137

Back To Grading

! One last time:

Give

D or Fgive other grade

Jt Scratch Lesson 5 Fall 2011 • slide 138

Back To Grading

! One last time:

Give

D or Fgive other grade

24

Jt Scratch Lesson 5 Fall 2011 • slide 139

Back To Grading

! One last time:

Jt Scratch Lesson 5 Fall 2011 • slide 140

Back To Grading

! One last time:

! Note: This is

one big IF

statement{

Jt Scratch Lesson 5 Fall 2011 • slide 141

Back To Grading

! One last time:

! Note: This isone big IFstatement

! Not fiveseparatestatements likebefore

{Jt Scratch Lesson 5 Fall 2011 • slide 142

Back To Grading

! One last time:

! Note: This isone big IFstatement

! Not fiveseparatestatements likebefore

{

Jt Scratch Lesson 5 Fall 2011 • slide 143

Back To Grading

! Note: This is onebig IF statement

! As such, it isimpossible formore than onegrade to beassigned.

Jt Scratch Lesson 5 Fall 2011 • slide 144

Back To Grading

! Note: This is one bigIF statement

! As such, it isimpossible for morethan one grade to beassigned.

! The one main IFeither Says A or

(else section) assignsone other grade

25

Jt Scratch Lesson 5 Fall 2011 • slide 145

Back To Grading

! Note: This is onebig IF statement

! No ANDs needed.

Jt Scratch Lesson 5 Fall 2011 • slide 146

Complete Code

Jt Scratch Lesson 5 Fall 2011 • slide 147

Complete Code

Demo:

Grading 4

Jt Scratch Lesson 5 Fall 2011 • slide 148

Main Point

! We already knew you use a regular IF if you

want to choose between doing something or

skipping it.

Jt Scratch Lesson 5 Fall 2011 • slide 149

Main Point

! We already knew you use a regular IF if you

want to choose between doing something or

skipping it.

! And, if you need to choose between two things

to do, and you definitely want to do one of

those two things, you use an IF/ELSE

Jt Scratch Lesson 5 Fall 2011 • slide 150

Main Point

! We already knew you use a regular IF if youwant to choose between doing something orskipping it.

! And, if you need to choose between two thingsto do, and you definitely want to do one ofthose two things, you use an IF/ELSE

! Now we know, if you have more than twochoices - a multiple choice decision - and youwant to do exactly one of the choices, then useNESTED IFs

26

Jt Scratch Lesson 5 Fall 2011 • slide 151

Main Point

For multiple choice decisions, use NESTED IFs

Jt Scratch Lesson 5 Fall 2011 • slide 152

One Last Fix

! Most people think of exactly 90 as an A, and

exactly an 80 as a B.

! What does our code do? Lets go see.

Jt Scratch Lesson 5 Fall 2011 • slide 153

One Last Fix

! Most people think of exactly 90 as an A, and

exactly an 80 as a B.

! What does our code do? Lets go see.

! Some of you already noticed this.

! How do we fix this?

Jt Scratch Lesson 5 Fall 2011 • slide 154

One Last Fix

! Most programming languages have more

relational operators, similar to what you

learned long ago in math class. Like:

IF Score >= 90

Jt Scratch Lesson 5 Fall 2011 • slide 155

One Last Fix

! Most programming languages have more

relational operators, similar to what you learned

long ago in math class. Like: Score >= 90

! Scratch does not. So we have to use the logical

operators.

Jt Scratch Lesson 5 Fall 2011 • slide 156

One Last Fix

! Most programming languages have more

relational operators, similar to what you learned

long ago in math class. Like: Score >= 90

! Scratch does not. So we have to use the logical

operators.

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

27

Jt Scratch Lesson 5 Fall 2011 • slide 157

One Last Fix

! Most programming languages have more

relational operators, similar to what you learned

long ago in math class. Like: Score >= 90

! Scratch does not. So we have to use the logical

operators.

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

! If either one of those two possibilities is true, we

should give an A.

Jt Scratch Lesson 5 Fall 2011 • slide 158

One Last Fix

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

! If either one of those two possibilities is true, we

should give an A.

! What logical (boolean) operator do you use when

just one of two possibilities has to be true?

Jt Scratch Lesson 5 Fall 2011 • slide 159

One Last Fix

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

! If either one of those two possibilities is true, we

should give an A.

! What logical (boolean) operator do you use when

just one of two possibilities has to be true? OR

Jt Scratch Lesson 5 Fall 2011 • slide 160

One Last Fix

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

! If either one of those two possibilities is true, we

should give an A.

! What logical (boolean) operator do you need just

one of two possibilities has to be true? OR

Jt Scratch Lesson 5 Fall 2011 • slide 161

One Last Fix

! For example, assign an A when the grade is over

90 or when the grade is exactly 90.

Jt Scratch Lesson 5 Fall 2011 • slide 162

Code to

Handle Exact

Scores

Notice ORs

28

Jt Scratch Lesson 5 Fall 2011 • slide 163

Code to

Handle Exact

Scores

Notice ORs

Jt Scratch Lesson 5 Fall 2011 • slide 164

Code to

Handle Exact

Scores

Notice ORs

Demo:

Grading 5

Jt Scratch Lesson 5 Fall 2011 • slide 165

Adding Multimedia

! Stage

Jt Scratch Lesson 5 Fall 2011 • slide 166

Adding Multimedia

! Stage

! Costumes

Jt Scratch Lesson 5 Fall 2011 • slide 167

Adding Multimedia

! Stage

! Costumes

! Sounds

Jt Scratch Lesson 5 Fall 2011 • slide 168

Adding Multimedia

! Change costume, play appropriate sound when

grade is assigned.

29

Jt Scratch Lesson 5 Fall 2011 • slide 169

Code With

Multimedia

Included

Jt Scratch Lesson 5 Fall 2011 • slide 170

Code With

Multimedia

Included

Demo:

Grading

Jt Scratch Lesson 5 Fall 2011 • slide 171

Availability of Slides

Go to

nbcs.rutgers.edu/~jt

to see the powerpoint

slides and/or podcasts

for this lecture