Forum Discussion

TheEndless's avatar
TheEndless
Channel Surfer
12 years ago

BrightScript wishlist (Continue, Swith/Select, etc.)

"renojim" wrote:
"TheEndless" wrote:
"NewManLiving" wrote:
Is there a continue or a loop
Statement that I missed as well. Now that would save you a bunch of if statements

No "continue" that I'm aware of.

^ Would be really nice to have!

While we're at it, a "switch" (or "select" to stick with the VBScript-like nomenclature) statement would be awesome to have as well!

10 Replies

  • "TheEndless" wrote:
    "renojim" wrote:
    "TheEndless" wrote:
    No "continue" that I'm aware of.

    ^ Would be really nice to have!

    While we're at it, a "switch" (or "select" to stick with the VBScript-like nomenclature) statement would be awesome to have as well!

    Continue is easy to emulate with Goto:
    for i = 1 to 1000
    ...
    if condition then goto continue
    ...
    continue:
    end for
    though why won't you just
    for i = 1 to 1000
    ...
    if not condition:
    ...
    end if
    end for
    With GOTO one can pop out of multiple loops - something that ordinary break/continue won't do.

    Emulating switch/select is more complicated but there are options: obviously easiest is IF ... ELSEIF ... ELSEIF... ELSE... ENDIF. If speed is concern, can use AA of functions with expression as selector - this is hairy because of variable scope (has to pass as arguments). Also, can do binary-search-tree of IFs - it's a contrived example but for big N will need only log2(N) comparisons:
    if i < 4:
    if i < 2:
    if i < 1:
    ... '0
    else:
    ... '1
    endif
    else:
    if i < 3:
    ... '2
    else:
    ... '3
    endif
    endif
    else:
    if i < 6:
    if i < 5:
    ... '4
    else:
    ... '5
    endif
    else:
    if i < 7:
    ... '6
    else:
    ... '7+
    endif
    endif
    endif
  • This should probably be split out into a new wishlist thread...
    "EnTerr" wrote:
    Continue is easy to emulate with Goto:


    (source: http://xkcd.com/292/)

    "EnTerr" wrote:
    Emulating switch/select is more complicated but there are options: obviously easiest is IF ... ELSEIF ... ELSEIF... ELSE... ENDIF.

    Of course there are options, but if you read the original post that started the conversation, the whole reason "continue" was brought up was to avoid having to use a ton of nested if/elseif/endif blocks.
  • Just don't like goto. Never did. Seems unnatural
    I know that jumps and the like are implemented
    At lower levels, machine code... But I'm
    In the school that thinks goto produces
    Spaghetti code. I remember having to debug
    A program years ago. The former programmer
    Was a obsessed with goto. Or simply did not
    Know how to structure a program. It was a nightmare
    I was gotoing all over the place like some corn maze
    Trying to figure out everything
  • "TheEndless" wrote:
    "EnTerr" wrote:
    Continue is easy to emulate with Goto:

    [velociraptor cutout, http://xkcd.com/292/]

    Of course there are options, but if you read the original post that started the conversation, the whole reason "continue" was brought up was to avoid having to use a ton of nested if/elseif/endif blocks.

    Continue typically it is replaced with a single if not ... end if. Off hand i cannot think of example where "a ton of nested if/elseif/endif blocks" will be needed for that, can you?

    Regarding the "here be dragons" part, hoped not having to explain but okay: during the process of learning, first we have to learn a simplified version of truth - for example first they tell you in school "you cannot subtract bigger number from a smaller one" - and a year later they say "meet the negative numbers". Or "5 is not divisible on 3" before meeting fractions, "you cannot take square root from negative number" before imaginary numbers etc. In the same way "thou shalt not use GOTO" when learning to program. First you need some training wheels - true, they prevent you from making sharp turns - but without them might not have even learned how to ride the programming bike. Later they come off.

    Here is list of limited cases where GOTO is beneficial. Here is example of multi-level exit that break can't muster. Do i use GOTOs - i can't remember when was the last time i did - but it is also rare for me to use break/continue either. In case have not thought about it, break/continue/return are just GOTOs in disguise, addressing the most beneficial cases/needs. Even Dijkstra (who made the case against GOTO) - for all his arrogance - later cautioned "Please don't fall into the trap of believing that I am terribly dogmatical about [the go to statement]. I have the uncomfortable feeling that others are making a religion out of it, as if the conceptual problems of programming could be solved by a single trick, by a simple form of coding discipline!" There is no silver bullet in programming.

    PS. Even switch/case can be used to make a bloody mess, exhibit Duff's Device - first time i saw it, i was like, "that's not even syntactically correct, interleaving switch and while loop" - but it is in C, compiles and works - and can give you hangover just from trying to understand it. If it helps, switch(expr) in C is just an arithmetic goto expr where case X are the "X:" labels for that goto.

    PPS. Never mind GOTO - apparently in PHP one can do effectively the mythical COMEFROM statement:
    switch (true) {
    case $x: ...
    case ($y > 10): ...
    case (rand(1,10) < 3): ...
    }
  • "EnTerr" wrote:

    Continue typically it is replaced with a single if not ... end if. Off hand i cannot think of example where "a ton of nested if/elseif/endif blocks" will be needed for that, can you?


    Yes:

    while something
    if first then continue
    number(1)
    if second then continue
    number(2)
    if third then continue
    number(3)
    ' etc
    end while


    Without continue, this would be written as

    while something
    if not first then
    number(1)
    if not second then
    number(2)
    if not third then
    number(3)
    ' etc
    end if
    end if
    end if
    end while


    That's the main purpose of continue, to avoid this style of coding where you've got to tilt your head 45 degrees to read the code.

    --Mark
  • "RokuMarkn" wrote:
    "EnTerr" wrote:

    Continue typically it is replaced with a single if not ... end if. Off hand i cannot think of example where "a ton of nested if/elseif/endif blocks" will be needed for that, can you?
    Yes:
    [cut]
    That's the main purpose of continue, to avoid this style of coding where you've got to tilt your head 45 degrees to read the code.

    Good illustration, thanks - was distracted by the /elseif/ part. I like the "45deg reading" expression. There might be better way to do the example semantically (can't work with "number(1)" etc) - but the goto exit strategy is obviously:

    while something
    if first then goto continue
    number(1)
    if second then goto continue
    number(2)
    if third then goto continue
    number(3)
    ' etc
    continue:
    end while
  • My remark about if statements was meant to be cumulative
    In respect to an application. There are times that
    Conditions prevail esp in event loops where you
    Don't want something to occur under certain
    Conditions. In those cases you may have to wrap
    Successive statements in an if statement. Where it would
    Be easier to just to use continue. As stared this
    Can be done with a jump and is how the underlying
    Instructions are executed but I'm not comfortable with
    Goto for some reason