pxt-microbit icon indicating copy to clipboard operation
pxt-microbit copied to clipboard

Feature: more advanced `for` loops

Open jaustin opened this issue 4 years ago • 11 comments
trafficstars

Is your feature request related to a problem? Please describe. Working with frequencies and sweeping frequencies is a really fun way to make sound with the micro:bit V2.

The 'for loop' is an ideal construct for this, as it lets you make some good sounds with very little code.

Describe the solution you'd like I'd like a block that lets the user set the start and increment value for the for loop index variable.

I think this could make sense as a new block

for <index> from <start> to <end> with step <step> translating to:

for (let index = start; index <= end; index+=step) {
}

Describe alternatives you've considered

The user can always use maths in the loop to do this, but for many students this greatly increases the complexity of the code required for simple sound effects.

We've done whole activities around playing with a 'frequencySweep' function which could be entirely replaced with a for loop

stinging together a range of these with different start, end, step and delays would make cool sounds. image

jaustin avatar Jan 12 '21 18:01 jaustin

@jaustin Until this feature is built into MakeCode you could create a custom block similar to this one: https://github.com/1010Technologies/pxt-makerbit-pins/blob/master/loops.ts

philipphenkel avatar Feb 09 '21 20:02 philipphenkel

options:

image

after discussion:

  1. it would be useful to give the default for-index-from loop a variable start value. we would then also need "step". important to ensure that users who are used to the old block can still recognize the updated version (testing required?)
  2. the advanced test-for block is still useful in many scenarios (eg looping over an array). we should talk about some form of "extended" toolbox setting where advanced users get more blocks while beginner users can keep the "lean" toolbox, with simpler blocks

for now: removing next release label, this feature will probably be tied with identity

shakao avatar May 06 '21 22:05 shakao

A way to set the start value is also requested in https://support.microbit.org/a/tickets/53785 (private)

The makecode Loops blocks "library" includes a conventional for loop. But this block has an artificial restriction on the starting index value (zero) This is unnecessary and restrictive. Programs which require a non-zero start value are hard to read and debug for beginners. Please generate a new block which has a programmable start value. It would be useful to also have a programmable step value for the index.

microbit-mark avatar May 23 '22 09:05 microbit-mark

Can this be done as extension?

abchatra avatar Mar 09 '23 07:03 abchatra

A couple of years ago, I added a custom loop block to this extension: https://github.com/1010Technologies/pxt-makerbit-pins The custom loop appears nicely in the Loops category:

custom loop

https://github.com/1010Technologies/pxt-makerbit-pins/blob/master/loops.ts

philipphenkel avatar Mar 09 '23 19:03 philipphenkel

CC @microbit-giles I like this as an extension, thought you might too. The discovery of this is tough

Are there any other 'advanced loops' that would make sense, or could this go in a more generic 'advanced blocks' extension"? I think ideally I'd prefer more scoped.

Might make sense as part of a discussion on extension organisation/categorisation.

jaustin avatar Jan 30 '24 17:01 jaustin

@jaustin Was there a reason why originally the '0' in the start of the 'for' loop block was hard-coded and unalterable rather than a default value? I can certainly see the value/need for a block that allows you to set the start, end and step as in pretty much any programming language.

microbit-giles avatar Jan 31 '24 09:01 microbit-giles

@jaustin Was there a reason why originally the '0' in the start of the 'for' loop block was hard-coded and unalterable rather than a default value?

@pelikhan may remember the reason - I'm afraid I don't

jaustin avatar Jan 31 '24 09:01 jaustin

Most likely pure legacy from TouchDevelop -> Blockly transition.

pelikhan avatar Jan 31 '24 19:01 pelikhan

Thanks @pelikhan. @jaustin Would an 'advanced' block that says for (index) in range (0) to (4) step (1) be an option? The step number as a 'free number' field that you can set to negative numbers?

microbit-giles avatar Feb 01 '24 09:02 microbit-giles

Yea, I think if introducing an advanced block changing the step would be good. That we we don't need to have a discussion again in a few months about 'yet another' block!

Even better for me would be if the 'step' was behind a "+" like in some of our other blocks that you can extend.

jaustin avatar Mar 20 '24 17:03 jaustin