• No se han encontrado resultados

ACTIVO INTANGIBLE

In document Cuentas anuales consolidadas (página 76-82)

b) fuentes de incertidumbre

8. ACTIVO INTANGIBLE

Order Amount

Shipping Cost

$0.00 to $19.99

$10.00

$20.00 to $39.99

$5.00

$40.00 and up

$0.00

(a) orderAmount shoppingCartTotal() shippingCost 10 [orderAmount < 20] [orderAmount > = 20] (b) [orderAmount > = 40] shippingCost 5 shippingCost 0

FIGURE 5.13 (a) Shipping cost policy. (b) Flowchart modeling a multiway selec- tion; selecting one of three alternatives.

example, the condition on line 4. This condition is only evaluated when the condition of line 2 has produced a value of false. In other words, when the condition of line 4 is evaluated we know that the order amount is not between $0 and $19.99. The order amount must therefore be either negative or at least $20. We can exploit this knowledge to reduce the complexity of our algorithm by asking questions that are easier to answer. If we assume, for example, that the order amount will never be negative, we know that the order amount must be at least $20 by the time we ask the second ques- tion. By taking the order of the decisions into account and by assuming that the order amount is not allowed to be negative, we can simplify our algorithm as shown in Figure 5.15.

Since we assume that the order amount will never be negative, we elim- inate that portion of the criterion from line 2 of Figure 5.15. This makes our algorithm more computationally efficient since the computer does

Program to Compute Shipping Cost

1. orderAmount ← shoppingCartTotal()

2. if orderAmount < 20 then 3. shippingCost ← 10

4. elseif orderAmount < 40 then 5. shippingCost ← 5

6. else

7. shippingCost ← 0 8. endif

FIGURE 5.15 More computationally efficient program for choosing a shipping cost of $0, $5, or $10 based on the order amount.

Program to Compute Shipping Cost

1. orderAmount ← shoppingCartTotal()

2. if orderAmount ≥ 0 and orderAmount < 20 then 3. shippingCost ← 10

4. elseif orderAmount ≥ 20 and orderAmount < 40 then 5. shippingCost ← 5

6. else

7. shippingCost ← 0 8. endif

FIGURE 5.14 Program for choosing a shipping cost of $0, $5, or $10 based on the order amount.

not need to check twice whether the amount is greater than or equal to zero. In addition, notice that the criteria “orderAmount >= 20” has been removed from the condition of line 4. This is possible since the computer first checks the condition of line 2 and only if that condition does not apply is the condition of line 4 checked. Logically, then, the sequencing of the conditions implies that the order amount must be greater than or equal to 20 whenever the computer checks the condition of line 4.

5.3.3 Repetition

“Wash, rinse, repeat” is a phrase that originated on the back of many brands of shampoo. This phrase has become part of pop culture and is often used as a metaphor for people who robotically follow instructions without critically thinking about what they are doing. Of course the point of this sarcastic metaphor is that anyone following these instructions will literally spend the rest of their lives endlessly repeating the same steps over and over; at least until they run out of shampoo.

Nonetheless, in everyday life we must often repeat a sequence of actions in order to achieve some greater outcome. You paint a wall, for example, by repeatedly dipping your brush into the paint can and applying the paint to the wall. You construct a fence repeatedly digging a hole and inserting a fence post. You even sing a song by repeatedly singing a verse followed by the chorus. Just as many real-life activities require repetition, computers often must also execute a sequence of actions repeatedly in order to achieve some greater outcome. In fact, one of the great strengths of a computer is its ability to robotically and quickly repeat actions without complaint!

A loop is a control structure that repeatedly executes a sequence of actions. A while loop is a type of loop where a sequence of actions is repeated as long as some logical condition holds. All while loops will have the flowchart structure shown in Figure  5.16. When the while loop is first encountered, we must determine whether to perform some action or

Actions to Repeat

[condition is true] [condition is false]

sequence of actions at least one (more) time. This decision is made by ask- ing a yes/no question. If the question is answered affirmatively, then the actions are performed after which we again ask whether we must repeat the sequence at least one more time. The repeating loop is highlighted by the red arrow.

Using everyday English we might describe a repeated sequence of actions with a phrase similar to “while the steak has not yet reached a temperature of 135°F, cook for three minutes.” In this example, the logical condition is “the steak has not yet reached 135 °F” and the repeated action is “cook for three minutes.” A flowchart that models this process is shown in Figure 5.17.

A while loop will be expressed using the notation shown in Figure 5.18 where the CONDITION and the ACTIONS contain elements that make the loop meaningful. In particular, the CONDITION must be an expres- sion that produces a logical value and the ACTIONS is a meaningful sequence of actions. The sequence of actions of a loop is often referred to as the loop body. The condition controls how many times the loop body actions will be executed.

Figure 5.19 uses this notation to express a computer program that mod- els the process of grilling a steak. In this program we assume that the steak is placed on the grill at a room temperature of 75° and that for every three minutes the steak remains on the grill the temperature increases by 13°. The first line binds the value 75 to the name steakTemp; a variable that models

Cook for three minutes

[steakTemp < 135] [steakTemp >= 135]

FIGURE 5.17 Flowchart that uses a while loop to model the process of grilling a steak.

In document Cuentas anuales consolidadas (página 76-82)