<< Chapter < Page Chapter >> Page >

Eventually, either our stillHungry variable will be set to false, or the cookie box will become empty. At that point, the test will return false. Wewill terminate the loop and will stop eating cookies even though we might still be hungry for more cookies.

The while and for keywords

Most modern programming languages use the keywords while and for to implement loop structures such as this and you will see those keywords in useonce you get to Java, C++, C#, etc.

However, Scratch does not use those keywords to implement loops. Regardless, when I present pseudocode examples to explain Scratchprograms, I will continue to use while and for to get you accustomed to that terminology.

Preview

In this module, I will present and explain the following two Scratch programs:

  • ForLoop01 - This program illustrates the Scratch equivalent of a for loop (or something similar to a for loop) in other languages. In Scratch, it is called a repeat loop.
  • ForLoop02 - This program also illustrates the Scratch equivalent of a for loop in other languages. However, this program takes a different approach to specifying the number of times that the code in theloop is executed.

In addition, I will provide the specifications for a student-programming project for you to demonstrate your understanding of what you learned from thetwo programs listed above. In addition, this programming project will require you to do some independent research into the manner in which Scratch sprites cancommunicate with one another.

Discussion and sample code

The program named ForLoop01

This program illustrates the Scratch equivalent of a for loop (or something similar to a for loop) in other programming languages. In Scratch, it is called a repeat .

Why do I keep saying "something similar to a for loop?" The reason is that a for loop in other programming languages has many facets. Therepeat loop in Scratch is much more restricted than a for loop in those other languages.

This program places a cat sprite having two costumes on the stage. When the user clicks the green flag, code is executed that is equivalent to thepseudocode shown in Image C .

Image c. pseudocode for the program named forloop01.

Initialize cat position, orientation, and costume for(count = 0;count<24,count = count + 1){Cat moves ten steps forward. Cat waits 0.1 secondCat switches to costume #2 Cat turns 15 degreesCat waits 0.1 second Cat switches back to costume #1}//end for loop
Image C. Pseudocode for the program named ForLoop01.

Why set count to 0 ?

Most modern programming languages typically start counting with 0 instead of 1, but that is not necessarily the case with Scratch. You will learn more aboutthat when you get into Java, C++, C#, etc.

Purpose of the pseudocode

My purpose in providing the pseudocode is to help you better understand the Scratch code that follows. I am also preparing you for the use of otherprogramming languages later. I recommend that you compare the pseudocode with the Scratch code, and compareboth of them with what you see when you run the program.

What's new in this Scratch program?

This program contains several new code blocks that haven't been used in previous modules:

  • The switch costume block
  • The repeat block
  • The wait block

The only one of these blocks that I would put in the programming fundamentals category is the repeat block, and even the syntax of that block is quite a bit different from other modern programming languages.However, the other blocks are some of the things that cause Scratch programming to be fun, so I will use and explain such blocks as we go along.My only caution is that you should not expect to find those commands in other programming languages, at least not using the same syntax.

Questions & Answers

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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