Simple 'For' loop question!

Blitz3D Forums/Blitz3D Beginners Area/Simple 'For' loop question!

Jono(Posted 2003) [#1]
Is it possible to do, 'For A = 1 to 10 If Score > 0 Then Score = Score + 1 Next' ? - Or am I missing something.

Instead of having to use 3 lines of code and do:

For 1 to 10
If Score > 0 Then Score = Score + 1
Next


soja(Posted 2003) [#2]
Normally I would say to just use the statement seperator ':' becuase those are seperate expressions, but apparently the compiler has a problem if there's a conditional between the For and Next, so something like this:
For a = 1 To 10 : If s > 0 Then a=a+1 : Next

...does not work, whereas something like this:
For a = 1 To 10 : a=a+1 : Next

...does.

But you CAN do this:
For a = 1 To 10 : If s > 0 Then a=a+1 : s=s+1 : b=b+1
Next

...if you really want to.


Jono(Posted 2003) [#3]
Thats exellent thanks. :D


jfk EO-11110(Posted 2003) [#4]
Or this way:
for a=1 to 10: if s>0 : b=b+1 : endif : next


MSW(Posted 2003) [#5]

"It's better to burn out, than to fade away." - Kurt Cobain



Thats also a lyric to the song Rock of Ages....

...Was Kurt a fan of Def Lepperd?


Jono(Posted 2003) [#6]
jfk: Cheers I appriate any examples.
MSW: It was written on his suicide note. And probably was a fan of Def Leppard he was a fan of alot of bands.


Koriolis(Posted 2003) [#7]
Out of curiosity, why can't you use 3 lines?


Ross C(Posted 2003) [#8]
Yeah, readable really does suffer.


Jono(Posted 2003) [#9]
Cos the less amount of lines of code, the easier it is to read and manage. Well thats the thoery anyway.


Koriolis(Posted 2003) [#10]
That's just a bad theory. The less code you write, the better it is, yes. But less code doesn't mean less lines of code.
Try to stick all your hundreds of lines of code into a single one (or even use only one character long variable names), and tell me how it looks ;p


soja(Posted 2003) [#11]
I would say that there are some cases that code might be more readable using a lot of statement seperators. What comes to mind is a Select/Case structure where all the cases only do one thing, like:
Select WaitEvent()
    Case evTimerTick : AnimateFrame()
    Case evMenuEvent : HandleMenuEvent(EventData())
    Case evWindowClose : End
End Select

... or maybe another special case, like an iterator through a Type list:
While this.Item <> that.Item : this = After this : Wend


Anyway, I don't think there should be any hard and fast rules about it. I understand it's all objective (whatever "looks" most readable is what one should use).


eBusiness(Posted 2003) [#12]
Why don't you just use:
If score>0 Then score=score+10



Jono(Posted 2003) [#13]
Becuase thats just a piece of example code.


puki(Posted 2003) [#14]
"For A=1 To 10: If Score>0 Then Score=Score+1: Next". Is probably never going to happen as the compiler will probably have to do too much to facilitate this (assuming you want Blitz to be able to do it in the future). I think you'll find it would produce overheads that are traded off by what you already know:

For A=1 To 10
If Score>0 Then Score=Score+1
Next

It's easier for Blitz to currently compile and debug this code than it would be to do your single line - what if score isn't greater than 0? Or perhaps I'm wrong - perhaps the overheads would be the same? I wouldn't bank on "For A=1 To 10: If Score>0 Then Score=Score+1: Next" being made legal as a 1 liner.


Ash(Posted 2003) [#15]
Personally, regarding readability, I find it easier to read a block of code with three statements, rather than one long line of code containing three statements. I find I have trouble searching for colons in long lines of code, whereas I can just read everything easily if it was a block of code. Of course, with blocks of code, indentation helps loads, too.


puki(Posted 2003) [#16]
Branching slightly off the point - but it could be a programming tip - I don't know if it applies to Blitz (I've not bothered testing it, but it probably is) The execution of statements involving "AND", is probably slower than if you cascade "IF - THEN" conditionals to obtain the same logical result (of course this will be dependent on what is "inside" the if/and/then's).