<< Chapter < Page
  Teaching beginners to code     Page 7 / 9
Chapter >> Page >

Selection structures are created by dragging these blocks into the center pane in order to apply them to a particularsprite.

Image 11. selection control structures available in scratch.

Missing image.
Image 11. Selection control structures available in Scratch.

How do the two bottom blocks differ?

The bottom block in Image 11 is used to select between two specific actions. The middle block is used when there is only one action and you need to decidewhether to take that action or not. That is the case in this program. If the basketball is touching a beach ball, a specific action is required.If the basketball is not touching a beach ball, no specific action is required.

Specifying the condition on which the decision will be based

Note the empty darker depressed area in each of the blocks shown in Image 11 (the rectangle with the pointed ends) . In order to use either of these blocks, you must drop another block having the same shape into the depressedarea. The block that you drop into that area must specify the condition that will be used to make the decision.

Two groups of programming blocks have the correct shape

Unless I missed seeing some others, there are only two buttons at the top left of Image 4 that expose blocks having the required shape:

  • Sensing (light blue)
  • Operators (green)

In this case, we will select and use a block from the light blue Sensing group. (We will use programming blocks from the green Operators group in a future module.) All of the blocks belonging to the Sensing group are shown in Image 12 .

Image 12. programming blocks belonging to the sensing group.

Missing image.
Image 12. Programming blocks belonging to the Sensing group.

Will use the touching block

We will use the block labeled touching followed by a pull-down list and a question mark.

The items that appear in the pull-down list depend on the sprites that have been added to the program. Forthis program, that list consists of the following choices when the basketball has been selected for programming:

  • mouse-pointer
  • edge
  • LeftBeachball
  • RightBeachball

The top two choices are always there. The remaining choices depend on the sprites that have been added to the program at the point in time when youpull down the list and the sprite that has been selected for programming. (Note that the Basketball sprite does not appear in the above list because it was selected for programming when I examined the list. In otherwords, you can't determine that the basketball is touching the basketball.)

Go back and examine the script

Getting back to the bottom script in Image 8 , you can see that I dragged two copies of the if block shown in Image 11 into the center panel and connected the blocks as shown in Image 8 . Then I dragged two copies of the touching block from the Sensing group shown in Image 12 , and dropped each of those blocks into the corresponding locations in the if blocks in Image 8 .

Then I selected RightBeachball from the pull-down list for one of the touching blocks and selected LeftBeachball from the pull-down list for the other touching block.

Conditions have been established - need actions

At this point, I had the conditions for the two selection structures established, but I hadn't yet specified the actions to be taken when one or theother of the conditions is found to be true.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask