# Thread: Programming Puzzle 23 - For / Next Loop

1. ## Programming Puzzle 23 - For / Next Loop

Write an xbasic script that solves this puzzle using a For / Next loop. Include comments so the "professor" can figure out what you're doing without losing any more of his quickly disappearing hair! Display your result in a message box on screen. For extra credit write your result to the trace window, too.

Puzzle 23:

Jennifer arranged for a party and had a basket of strawberries to serve. She ate 5 strawberries before the party began, and gave a friend 3 more. Eight girls came to the party. The first girl took one strawberry. The second girl took three. The third girl took five, and so on. Her basket was empty after the last girl took hers. So, how many strawberries did she start with?

Have fun!

Puzzle courtesy easycalculation.com

3. ## Re: Programming Puzzle 23 - For / Next Loop

Very nice, Andrew!

Professor Pickypicky asked me to remind you that you can't always count on Alpha Five to convert your data types for you. At line 38 you pass a numeric value to an argument in the ui_msg_box() function that is expecting a text string. In this case Alpha autoconverts your numeric to character data so the msg box does not fail. You cannot always count on autoconvert to correct data type mismatches. The professor thinks a better approach would be to convert the numeric value to a string of characters and pass that string to the msg box.

The professor was wondering, also, whether you can think of a way to populate array elements 3 through 10 using a loop, to avoid having to repeat the same code over and over... ? The professor's intern wants to know something else... is there some way to compute the sum for the 8 "partiers" using a For .. Next loop that does not involve an array ? If so, maybe that would be a tiny bit more efficient.

Thanks for playing!

-- tom

4. ## Re: Programming Puzzle 23 - For / Next Loop

Second attempt

Sweet!

6. ## Re: Programming Puzzle 23 - For / Next Loop

I'd be interested to find an example of showing the result in the trace window grammatically. I attempted using debug(1) and trace.write(), but to see the value of the variables, had to enter them in the debug screen.

7. ## Re: Programming Puzzle 23 - For / Next Loop

Tony,

Add this to the end of your script.

Then run the script, then open Trace Window from the View Menu.

Code:
```' tom's code follows:

dim vc_berries_count as C = ""   ' initialize character value variable
vc_berries_count = ltrim(str(Stawberries_taken,10))   'convert numeric to string, trim leading blanks

' display in trace window
trace.writeln("Jennifer started with " + vc_berries_count + " strawberries.")```

8. ## Re: Programming Puzzle 23 - For / Next Loop

Thanks Tom,

Turns out that I didn't know what the Trace Window was. That will be helpful going forward.

I'm curious about why you create a type=C variable rather than allowing A5 to handle the conversion through context here. Is this a momentum and/or comfort and/or "haven't sufficiently tested the new thing" thing, or do you understand there to be perils in using the context based conversion?

9. ## Re: Programming Puzzle 23 - For / Next Loop

There is potential for perils in context conversion of data types.

Some languages I have used you had to explicitly convert the data types to use them in a different context. So for some this is a hold over of having to it manually. For others it is done to make sure issues do not occur in the future.

For me, I try to always convert the data types instead of having Alpha5 do it, but sometimes I get lazy and/or forget.

10. ## Re: Programming Puzzle 23 - For / Next Loop

Tony, I explicitly convert all data types as a matter of course. In part this is because I don't know the "limits" for the built in auto-conversion routines. Do they work in all contexts? For all data types? I don't know. Guess I'm too lazy to figure it out. I also think that handling this explicitly simplifies debugging. If I screw up the data conversion I see it right away and can fix it. If Alpha screws up the data conversion the trail to the problem is not as clearly marked.

And, yes, the trace window is a very very handy tool. The professor thinks maybe its underutilized, so that's why she frequently offers extra credit for solutions displayed there.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•