Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
TheEndless
Channel Surfer

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!
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
10 REPLIES 10
EnTerr
Roku Guru

Re: texmgr requests

"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
0 Kudos
TheEndless
Channel Surfer

Re: texmgr requests

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.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
NewManLiving
Visitor

Re: texmgr requests

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
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
EnTerr
Roku Guru

Re: texmgr requests

"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): ...
}
0 Kudos
RokuMarkn
Visitor

Re: texmgr requests

"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
0 Kudos
EnTerr
Roku Guru

Re: texmgr requests

"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
0 Kudos
NewManLiving
Visitor

Re: texmgr requests

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
My Channels: 2D API Framework Presentation: https://owner.roku.com/add/2M9LCVC
Updated: 11-11-2015 - Completed Keyboard interface
The Joel Channel ( Final Beta )
0 Kudos
JesUltra
Streaming Star

Re: CONTINUE is actually supported

I do not know when it was introduced, but the current Brightscript reference lists "CONTINUE FOR" and "CONTINUE WHILE" statements.  (With oddly no mention of "BREAK".)

https://developer.roku.com/docs/references/brightscript/language/program-statements.md

0 Kudos
renojim
Community Streaming Expert

Re: CONTINUE is actually supported

There's no break, but there is "exit while".  Are you saying that break is supported? 

This is a ten year old thread, so a lot has changed.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.