1 / 10

Programming with App Inventor

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.

dwayne
Download Presentation

Programming with App Inventor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop

  2. LoopsSession SA-3

  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.

  4. The Loop BlockFor 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 namedvariable whose scope is restricted to inside the for range loop block. Unlike normal variable whose scope is anywhere in the program.

  5. The While Loop • The whileloop, as the name implies, keeps repeating while a given Boolean expression is true. A Boolean expression evaluates to either True or False. • 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. • Whileloops 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. BooleanExpression

  6. Infinite Loops • In some cases, a loop must continue repeating the entire time the program is running. When this is required an infiniteloop 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.

  7. Boolean ExpressionsLogic 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 or . • You can negate them, or them or andthem. • You can also compare 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.

  8. Project SA_2Conditional 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.

  9. Day 2 QuestionsConditional Branching, Boolean Expressions, Loops • What part of an If/Else control structure determines which sequence of blocks is executed? • The expression in the predicate part of an If/Else structure evaluates to a _____or _____value. • What part of the If/Else structure would execute if the predicate value was true? • If a certain block or number of blocks needed to be executed several times, what control structure would you use? • 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? • The expression in the predicate part of an If/Else structure evaluates to what type of value? • If the condition of a while loop was set to “true” how many times would this loop execute?

  10. Day 2 Answer KeyConditional Branching, Boolean Expressions, Loops • 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). • The expression in the predicate part of an If/Else structure evaluates to a _true_or _false_value. • What part of the If/Else structure would execute if the predicate value was true?consequent (internal) section (the ‘then-do’ block) • 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) • 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 • The expression in the predicate part of an If/Else structure evaluates to what type of value?A Boolean value (true or false) • If the condition of a while loop was set to “true” how many times would this loop execute?an infinite number of times

More Related