Conditional Programming Logic

The next set of control code blocks provided by Scratchis shown in Figure 9.6. These code blocks allow you to apply conditional programming logic to your scripts.

Figure 9.6. These five code blocks let you conditionally execute collections of code blocks.


Using these code blocks, you can analyze data within your applications and make decisions based on this analysis, resulting in the conditional execution of collections of code blocks. The key concept to understand when working with these types of code blocks is that conditional logic involves an evaluation as to whether a condition is true or not. If the condition being analyzed is true, then the code blocks embedded within the control block are executed. However, if the condition being analyzed proves false, the embedded code blocks are not executed.

The following script demonstrates how to use the first code block shown in Figure 9.6 to set up a loop in which execution is controlled by a conditional test. Each time the loop repeats, it checks the value assigned to a variable named Counter to see if it is equal to 0. If it is, the loop executes, plays an audio file, pauses for two seconds, and then checks to see if it should execute again.

This next example demonstrates how to conditionally execute the playback of an audio file. When executed, this script examines the value assigned to a variable named Counter to see if it is equal to 0, and if it is, the audio file is played.

Sometimes you may want to execute either of two sets of code blocks based on the results of a tested condition. This can be accomplished using the third code block shown in Figure 9.6.

Here, a conditional test is performed that checks to see if the direction that a sprite is facing is 90 degrees. If it is, the direction that the sprite is pointing is reversed. If you run the script repeatedly, the direction that the sprite is pointing is continuously reversed.

This next example demonstrates how to use a control block that pauses script execution and waits for a specified condition to become true.

Here, a script has been set up that, once run, checks on the value assigned to Counter to see if it is greater than 5. If it is, an audio file is played. If Counter is not greater than 5, then the script pauses its execution, waiting until the value of Counter exceeds 5 before finishing its execution.

Finally, the last example demonstrates how to work with the last of the control blocks shown in Figure 9.6. Here, a loop is set up to execute repeatedly until the value assigned to Counter is set equal to 3, at which time the loops will stop running. Each time the loop runs, it moves, or bounces, its associated sprite up and down on the stage.

Nesting Conditional Control Code Blocks

As powerful as the control blocks are that facilitate conditional execution, they are limited to analyzing a single condition at a time. To develop more complex programming logic, you can embed one control block within another, as demonstrated in the following example:

Here, one control block has been embedded within another control block to further analyze the value assigned to Counter. If necessary, you can embed control blocks many levels deep. However, the deeper you go, the more difficult your scripts will be to understand and maintain.

Preventing Endless Loops

Loops are extremely powerful tools, providing the capability to perform repetitive tasks with ease. However, if you are not careful when setting them up, you can accidentally set up an endless loop. An endless loop is a loop that, because of a logical error on the programmer’s part, never ends. For example, you might want to set up a loop that plays an audio file five times. But suppose when setting up the loop you made a mistake that prevented the loop from ever terminating, as shown here.

Here, the intention was to set up a loop that would execute five times. The loop has been set up to execute for as long as the value assigned to Counter is less than 5. Counter is assigned an initial value of 1, and its value is supposed to be incremented by 1 each time the loop executes. However, instead of incrementing the value of Counter by 1 at the end of the loop, the value of Counter is decremented by a value of 1. As a result, the loop never terminates, forever repeating the playback of the audio file. To prevent endless loops from occurring, you need to take extra care when setting up loops and test your scripts thoroughly when developing your applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.238