Tuesday, March 16, 2010

Introduction to Programming: Starting Out with Alice: Chapter 4: Decision and Repetition Structures

Starting Out with Alice: A Visual Introduction to Programming

1. Boolean Values

Boolean Variables

A Boolean variable can only hold a true or a false value.

Boolean Functions

We can test a statement and see if it is one or the other and assign it the value. Separating that code into a function would be a Boolean function

2. The if/else Decision Structure

The if/else decision structure will do one set of instructions if a condition is true and a different set if false. This is the opposite of a sequence structure which does everything in order. The flowchart would be a diamond with an arrow to right if true and arrow to left if false.

In Alice, the ifElse tile is at the bottom of the Method Editor. When placed in the editor it will open slots to fill in for the steps for true or false.

Having Multiple Conditionally Executed Instructions

If you need to do more than one step for a condition, put those steps in sequential order within their section.

Creating a Single-Alternative Decision Structure

If you only need to do steps if a condition is true, then in Alice programming you leave the else empty. Most other languages have an if without the else.

Nested if/else Instructions

If you need to test something after it has met or not met the first test, another if/else instruction can be added. This called nested if/else instructions.

3. Relational Comparisons and Logical Operators

Relational operators are use to determine if one value is larger, smaller, or equal to another one.

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

These result in a Boolean value which can be used with the if/else to determine which path to take.

To program them in Alice, drag the function tile into place in the if/else tile, select the values you want and finish building the test and instructions.

Logical Operators

It is possible to add multiple tests together; eliminating the need for nested if/else statements. These become Boolean logical operators.

Not a if a is true it is now false and if false it is now true
Both a and b true if a and b is true, false if either one is
Either a or b true if either a or b is true' false only if both are false

4. The loop Instructions

A loop causes a set of instructions to repeat a certain number of times.

Computing the Number of Times to Repeat

It is possible to get a value, like a distance, and loop that number of times. Loops will only use integer numbers.

Infinite Loops

It is possible in the Alice environment to do infinite loops, in other words, ones that will never end till the user stops the program. This is not the best practice in most programs however.

The Complicated Version of The Loop Instruction

A loop instruction in the Alice programming environment has an 'inner workings' button. This button will break the loop down into more complicated steps that are really done.

Flowcharting a loop Instruction

Normally the flowchart would start with the box that initializes the index counter. It would then do a diamond decision with true the loop steps that the loop back to the start of the diamond and false where it drops out.

5. The while Instruction

The while loop is a loop that will repeat as long as a Boolean value is true. Instead of using a counter, it waits for a value to change.

The while Instruction is a Pretest Loop

Because it tests the Boolean value at the start of a loop, and possibly not do it, a while loop is a pretest loop.

Creating a while Instruction

In the method editor at the bottom is the while tile. Drag it on to the editor modify it to set the values for testing.

An Introduction to Programming Using Alice

No comments:

Post a Comment