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

Perform the test and exit on false

The first thing that happens following entry is that a condition is tested for true or false.

If the test returns false, control simply exits the structure without taking any action at all.

Perform some actions and repeat the test on true

If the test returns true:

  • One or more actions are performed in sequence.
  • The condition is tested again and the process is repeated.

During each iteration , if the test returns false, control exits the structure. If the test returns true, the entire process is repeated.

Each action element may be another structure

Each of the action elements may be implemented by another sequence, selection, or loop structure.

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 called 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.


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.)

Programming interface for the program named IfSimple01

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

Image 4. scratch 2.0 programming interface for the program named ifsimple01.

Missing image.
Image 4. Scratch 2.0 programming 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 of the stage, 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 180degrees }//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 beachballs.The basketball will keep bouncing back and forth between the two beachballs 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 the stage in Image 4 .

Questions & Answers

How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?