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

Eventually all of the actions will be completed and the condition will be tested again.

Need to avoid infinite loops

Generally speaking, unless something is done in one of the actions to cause the test to eventually return false, control will never exit the loop.

In this case, the program will be caught in what is commonly referred to as an infinite loop.

Other possible structures

In some programming languages, there are structures other than sequence, selection, and loop that structured-programming experts are willing toaccept for convenience including:

  • The switch-case structure.
  • The do-until structure.
  • The for loop
  • The for-each loop

While sometimes more convenient than the three main structures, these structures are not required for the solution of programming logic problems.

Preview

In this module, I will present and explain the simplest example of a selection structure that I was able to write in Scratch without using variablesand without using relational or logical operators. (I will explain operators, including relational and logical operators in future modules.)

Screen output for the program named IfSimple01

The program places a basketball and two beach balls on the Stage as shown in Image 4 .

Image 4. user interface for the program named ifsimple01.

Missing image.
Image 4. User interface for the program named IfSimple01.

Click the green flag or the basketball

When the user clicks the green flag in the upper right corner, the three balls are placed in a horizontal line with the basketball in the center.

Scratch code, which can be paraphrased as shown in Image 5 , is executed each time the user clicks the basketball with the mouse.

Image 5. a paraphrased version of the scratch code.

when Basketball is clicked{ move basketball forward by 90 stepsif(Basketball is touching RightBeachball){ turn Basketball by 180 degrees}//end ifif(Basketball is touching LeftBeachball){ turn Basketball by 180 degrees}//end if }
Image 5. A paraphrased version of the Scratch code.

In other words, if you repetitively click the basketball with the mouse, it will move back and forth from left to right bouncing off of the two beach balls.The basketball will keep bouncing back and forth between the two beach balls for as long as you continue clicking on the basketball.

Discussion and sample code

Let's walk through the steps required to develop this program. I will deal first with the code that defines the behavior of the program when the userclicks the green flag in the upper right corner of Image 4 .

The Cartesian coordinate system The position of sprites on the Stage in the Scratch user interface is based on a two-dimensional Cartesian coordinate system with the origin at the center of the Stage.

Three sprites

Note in the lower right corner of Image 4 that this program contains two beach balls and one basketball in addition to the Stage.

You add sprites to your program by clicking on the button with the icon of the file folder in the grayarea immediately below the white Stage area in Image 4 .

When you click on one of the sprites that you have added to the program, that sprite appears at the top of the center pane. Having done that, you canthen drag programming blocks into the center pane that define the behavior of that sprite.

Questions & Answers

what is mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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