Transcript 3 - Loops

Programming with App Inventor
Computing Institute for K-12 Teachers
Summer 2012 Workshop
Loops
Session SA-3
Loops
 At the heart of their functionality, loops enable blocks of code to be
executed more than once.
 Loops represent a simple concept that can be used to create extremely
dynamic and flexible programs.
 There are several different constructs in programming for implementing
loops.
 Some loops simply count the number of times a block of code is executed
and the loop will be exited once a pre-determine number of reiterations
have been reached (do ‘this same thing’ 10 times). If needed a variable
can be used as an index for the loop. The index is a counter for the
current count of the execution of the loop.
 Loops can be made to continue until a specific condition is obtained.
 Loops can be made to repeat execution for each item in a list.
 Loops can be made to loop indefinitely.
The Loop Block
For Range loop
 In the App Inventor, the simplest type of loop is the for range loop block. It closely
resembles the “for” loop found in other programming languages.
 This loop executes 10 times. The variable named ‘i’ will be incremented by the step
value with each iteration of the loop. In this case, it is increment by 1. The variable
‘i’ will equal 1 the first time through the loop, 2 the second time and so on. The
Label1.Text is set to the value of ‘i’ each time the loop is executed. With each time
through the loop, ‘i’ is compared to the end number (10). Once ‘i’ is greater than 10
the loop will exit. The for range loop is dependant on an implied Boolean expression
comparing the named variable i to the start and end values set for the loop.
 The for range loop requires a start value, an end value, a step value, and a named
variable whose scope is restricted to inside the for range loop block. Unlike normal
variable whose scope is anywhere in the program.
The While Loop
 The while loop, as the name implies, keeps repeating while a given
Boolean expression is true. A Boolean expression evaluates to either True
or False.
Boolean
Expression
 It is identical to the while loop found in other programming languages.
The example above loops 10 times (until var1 is great than 10). With
each iteration, var1 is increment by 1.
 While loops are perfect when combined with user input.
 This allows the user to interact with the program indefinitely; loops such
as these are found in almost every program in some form.
Infinite Loops
 In some cases, a loop must continue repeating the entire
time the program is running. When this is required an
infinite loop is often used.
 Like the while loop, the infinite loop is commonly used in
conjunction with user input.
 One must be careful when working with loops, infinite
loops can cause ‘software to hang’ or cause the system to
become very sluggish.
Boolean Expressions
Logic Operators
 The App Inventor provides for several implementations of Boolean
expression shown below. The are found under the Built-In tab in the
Block Editor in the Math and Logic drawers.
 A Boolean variable can be either
 You can negate
 You can also compare

or
them, or
.
them or and
them.
them
The comparison operator can be either =, <, >, not =, <= , >= .
 The operands in these blocks can be a variable with a value of either True
or False or the can be a Boolean Expressions.
 But no matter how you combine them, Boolean expressions evaluate to
either True or False.
Project SA_2
Conditional Branching, Boolean Expressions, Loops
 Create an app where a label starts at 0 and is increased by
1 every time you click a button. Have the ball only move
when the labeled number is either even or odd.
Day 2 Questions
Conditional Branching, Boolean Expressions, Loops
1.
What part of an If/Else control structure determines which sequence of blocks is executed?
2.
The expression in the predicate part of an If/Else structure evaluates to a _____
value.
3.
What part of the If/Else structure would execute if the predicate value was true?
4.
If a certain block or number of blocks needed to be executed several times, what control
structure would you use?
5.
If a loop executes 5 times and there was a block inside that incremented the variable varCount
by 3 each time, assuming the variable has an initial value of 0, what is the ending value of
varCount?
6.
The expression in the predicate part of an If/Else structure evaluates to what type of value?
7.
If the condition of a while loop was set to “true” how many times would this loop execute?
or _____
Day 2 Answer Key
Conditional Branching, Boolean Expressions, Loops
1.
What part of an If/Else control structure determines which sequence of blocks is executed?
The predicate (the Boolean expression attached to the test socket of the if blocks).
2.
The expression in the predicate part of an If/Else structure evaluates to a _true_
value.
3.
What part of the If/Else structure would execute if the predicate value was true?
consequent (internal) section (the ‘then-do’ block)
4.
If a certain block or number of blocks needed to be executed several times, what control
structure would you use?
a loop block (for range loop, while loop)
5.
If a loop executes 5 times and there was a block inside that incremented the variable varCount
by 3 each time, assuming the variable has an initial value of 0, what is the ending value of
varCount?
15
6.
The expression in the predicate part of an If/Else structure evaluates to what type of value?
A Boolean value (true or false)
7.
If the condition of a while loop was set to “true” how many times would this loop execute?
an infinite number of times
or _false_