EET 2259 Unit 7 Case Structures Sequence Structures

  • Slides: 25
Download presentation
EET 2259 Unit 7 Case Structures; Sequence Structures ¡ Read Bishop, Sections 5. 4

EET 2259 Unit 7 Case Structures; Sequence Structures ¡ Read Bishop, Sections 5. 4 and 5. 5. Lab #7 and Homework #7 due next week. ¡ Quiz #3 next week. ¡

Review: Structures ¡ ¡ Structures control the flow of a program’s execution. We’ve looked

Review: Structures ¡ ¡ Structures control the flow of a program’s execution. We’ve looked at two kinds: l l ¡ For Loops While Loops This week we’ll look at two more: l l Case Structures Sequence Structures

Case Structure ¡ ¡ Case structures provide a way to execute different code depending

Case Structure ¡ ¡ Case structures provide a way to execute different code depending on which one of several possible conditions is true. Traditional programming languages accomplish the same thing using IF statements or CASE statements. (Bishop, p. 236)

Case Structure Versus the Select Function We’ve already used the Select function, which is

Case Structure Versus the Select Function We’ve already used the Select function, which is one way in Lab. VIEW of choosing between alternatives. ¡ A Case structure serves a similar purpose, but it’s more powerful and more flexible than the Select function. ¡ l In other words, you can use a Case structure to do anything that you can do with a Select function, but not vice versa.

Case Structure Versus the Select Function (Cont’d. ) In simple cases like the on

Case Structure Versus the Select Function (Cont’d. ) In simple cases like the on the previous slide, a Select function works fine. ¡ But here are two situations where Case structures are clearly better than the Select function. ¡ 1. 2. You need to choose among many alternatives rather than just among two or three. Your choice is between different kinds of actions, such as choosing whether to display a string or to make an LED blink.

Placing a Case Structure ¡ ¡ Case structures are found on the Programming>Structures palette.

Placing a Case Structure ¡ ¡ Case structures are found on the Programming>Structures palette. As with loops, click it, and then drag to create a Case structure on the block diagram. (Bishop, p. 236) Selector terminal Selector label

Subdiagrams ¡ ¡ ¡ A Case structure has multiple subdiagrams, one of which is

Subdiagrams ¡ ¡ ¡ A Case structure has multiple subdiagrams, one of which is executed when the structure runs. These subdiagrams are “stacked” on top of each other like cards in a deck, so that only one is visible at a time. The selector label on the top border tells which case you’re looking at, and has arrow buttons to let you step through the cases.

Selector Terminal ¡ ¡ ¡ A Case structure has one terminal, called the selector

Selector Terminal ¡ ¡ ¡ A Case structure has one terminal, called the selector terminal. This terminal, labeled ? , determines which one of the structure’s subdiagrams will be executed. You’ll usually wire this terminal to a control or to the output of a function. (Bishop, p. 238)

Selecting Which Case to Execute: Boolean Example ¡ A Case structure with a Boolean

Selecting Which Case to Execute: Boolean Example ¡ A Case structure with a Boolean selector terminal will have two subdiagrams: one that executes if the condition wired to the terminal is true, and one that executes if the condition is false.

Selecting Which Case to Execute: Numeric Example ¡ A Case structure with a numeric

Selecting Which Case to Execute: Numeric Example ¡ A Case structure with a numeric (integer) selector terminal can have many subdiagrams: the one that’s executed depends on the value wired to the terminal.

Numeric Selector Labels ¡ A numeric selector label can be: l l l a

Numeric Selector Labels ¡ A numeric selector label can be: l l l a single integer, such as 24 a list of integers, such as 1, 5, 11 a range of integers, such as ¡ ¡ 10. . 20 meaning all integers from 10 to 20. . 10 meaning all integers less than or = 10 10. . meaning all integers greater than or = 10 Selector labels cannot be floating-point numbers. (Bishop, p. 238)

Adding and Deleting Cases ¡ Right-click a Case structure’s border and then l l

Adding and Deleting Cases ¡ Right-click a Case structure’s border and then l l l choose Add Case Before or Add Case After to add a case. choose Duplicate Case to copy a subdiagram to a new case. choose Delete This Case to remove a case. (Bishop, p. 239)

Default Case ¡ ¡ Numeric case structures and string case structures usually have a

Default Case ¡ ¡ Numeric case structures and string case structures usually have a default case, which will execute whenever none of the other cases apply. To make a case the default case, right-click its border and choose Make This The Default Case. (Bishop, p. 240)

Tunnels ¡ ¡ Recall that a tunnel automatically appears on a structure’s border when

Tunnels ¡ ¡ Recall that a tunnel automatically appears on a structure’s border when you run a wire across the border. If data flows from outside the structure to inside, the tunnel is an input tunnel. If data flows from inside the structure to outside, it’s an output tunnel. If an output tunnel is wired on one of a Case structure’s subdiagrams, then it must be wired on all of the subdiagrams. (Bishop, pp. 240 -241)

Example if Statement in C++ cout << "How many CDs are you buying? ";

Example if Statement in C++ cout << "How many CDs are you buying? "; cin >> CDs; if (CDs >= 5) { total. Cost = 9 * CDs; cout << "You get a discount! "; } else { total. Cost = 10 * CDs; } cout << "Total cost is $" << total. Cost;

Order of Execution in Text-Based Languages ¡ In text-based programming languages, the order of

Order of Execution in Text-Based Languages ¡ In text-based programming languages, the order of the statements determines the order of execution.

Order of Execution in Lab. VIEW ¡ In Lab. VIEW, if two functions are

Order of Execution in Lab. VIEW ¡ In Lab. VIEW, if two functions are connected by wires, you can tell which one must execute first. ¡ But you can’t always predict the order of execution. For example, in this code, which function executes first: the Subtract or the Greater Than?

Example Program ¡ On this block diagram, in which order will these operations execute?

Example Program ¡ On this block diagram, in which order will these operations execute? l l l a+b c+d Time delay

Sequence Structures ¡ ¡ Sequence structures can be used to force code to execute

Sequence Structures ¡ ¡ Sequence structures can be used to force code to execute in a specific sequence. The code to be executed sequentially is entered on subdiagrams called frames. The analogy is to frames of movie film, which are projected one after another. (Bishop, p. 247) Sequence structure with three frames

Measuring the Speed of Your Code ¡ Here’s a basic application of a sequence

Measuring the Speed of Your Code ¡ Here’s a basic application of a sequence structure. It shows how you can measure the time needed to execute some code by: 1. 2. 3. Reading the computer’s internal clock. Executing the code whose execution time you want to measure. Reading the clock again and finding the difference between the two clock readings.

Tick Count ¡ ¡ ¡ The example on the previous slide used Lab. VIEW’s

Tick Count ¡ ¡ ¡ The example on the previous slide used Lab. VIEW’s Tick Count function, which returns the number of milliseconds (ms) on your computer’s millisecond timer. Generally this timer is reset to zero whenever the computer is rebooted. Knowing that Tick Count uses the U 32 datatype, let’s figure out how long it will take for it to “roll over” to zero again.

Tick Count Computations ¡ ¡ What is the largest integer we can represent with

Tick Count Computations ¡ ¡ What is the largest integer we can represent with U 32? _______ If that number is in ms, how many seconds is that? ________ ¡ How many minutes is that? ______ ¡ How many hours is that? ______ ¡ How many days is that? ______

Flat or Stacked? ¡ Lab. VIEW has two kinds of Sequence structures: l Flat

Flat or Stacked? ¡ Lab. VIEW has two kinds of Sequence structures: l Flat Sequence structures, in which the frames are laid out side by side. l Stacked Sequence structures, in which the frames are “stacked” like a deck of cards, with only one visible at a time. (Bishop, p. 247)

Flat or Stacked? (Continued) ¡ The two kinds of Sequence structure are similar. The

Flat or Stacked? (Continued) ¡ The two kinds of Sequence structure are similar. The main difference is how much space they take up on the block diagram. But there are other differences, such as: l l If data is wired out through a tunnel from a frame in a Flat Sequence structure, the data passes out as soon as that frame finishes executing. If data is wired out through a tunnel from a frame in a Stacked Sequence structure, the data does not pass out until the entire structure finishes executing.

Use Sequence Structures Only When Necessary ¡ ¡ ¡ Sequence structures serve a valid

Use Sequence Structures Only When Necessary ¡ ¡ ¡ Sequence structures serve a valid purpose, but they also tend to hide parts of the program and interfere with the natural flow of data in Lab. VIEW. Some programmers over-use them. Avoid using them unless you have a situation where you need to guarantee the order of execution, and Lab. VIEW’s natural data flow does not provide such a guarantee. (Bishop, p. 250)