What IF ,Then What?

Blitz3D Forums/Blitz3D Beginners Area/What IF ,Then What?

Tibit(Posted 2003) [#1]
In blitz you can use the If statement in more than one way. This is verygood because it shortens code and makes programming faster. Still I'm not sure that I have understood IF statement correctly, It works for me but I see alot of talented programmers that use unneccecary syntax. -thinking about THEN-

This is a simple example. It only proves 2 ways of doing the same thing.

If A=5
B = 10
Endif

If A=5 then B = 10 ;no need for endif

If I understand this correclty this is the same thing. And the use of THEN makes it possible to write it on the same line. true?

I see many examples where people write like this:
If A=8 then
B=10
C=3
Endif

The thing about the above code is the STRANGE use of Then, not only does it do nothing (I think) but also it makes the code hard to read because you suppose there shouldn't be an EndIf efter Then. True?

;This is the same thing whitout the Endif on one line
If A=8 then B=10:C=3

It doesn't stop here =) blitz is much smarter
If you use Then you can put in a Else statement on the same line! Like this:

;the ":" sign means a new line for the BlitzCompiler but in if statements if seams like it signals "here comes more"
If A=1 Then B=0 Else B=12 : C=10 : A=0

Is there more ways to make code shorter like this? To make a long statement short (That's what all this is about isn't it?). Am I have right in this matter?


SSS(Posted 2003) [#2]
you are right but you actually dont need the then i.e.

this
if A = 1 then b=0 else b=12 : c = 10 : A = 0
is the exact same as this
if A = 1 b=0 else b=12 : c = 10 : A = 0

but otherwise you have it right


(tu) sinu(Posted 2003) [#3]
you can alos do

if a = 1 b = 2;
or
if a = 2 b = 4 else b = 3

no need for the then either, it's cos it's basic and makes the code more user friendly, other than that i find no use for the "then" statement.


dynaman(Posted 2003) [#4]
I put then in simply because other languages require it, this way when I program with them I don't have to learn different rules for each one.


Ross C(Posted 2003) [#5]
I always put the Then in. Force of habit. For multi-line statements, i use

If a=0 then
b=3
c=2
end if

I find it easier to read. But that's just me, i prefer me code to be readable than short :D


Gabriel(Posted 2003) [#6]
This is a simple example. It only proves 2 ways of doing the same thing.

If A=5
B = 10
Endif

If A=5 then B = 10 ;no need for endif

If I understand this correclty this is the same thing. And the use of THEN makes it possible to write it on the same line. true?



Absolutely true. But the multi-line version is far easier to read. It indents the conditional statement, which will makes things a lot easier when you're debugging. You'll go past the single line version 3 or 4 times looking for it. It's just personal preference. Whatever you find easiest to read and debug is just fine.

Since there's no such thing as a single line assembly language equivalent, I can't imagine that the resulting code will be different.


Tibit(Posted 2003) [#7]
Good replies, all I can say =)


Ice9(Posted 2003) [#8]
What happens if blitz gets updated or a new version of blitz comes out that requires the "then" statement?


Ross C(Posted 2003) [#9]
I don't think mark would do that. There really would be no need to goto the trouble of putting that in, with the potential of breaking lots of ppl's code :o)


CS_TBL(Posted 2003) [#10]
I put things in an "if..endif" 'box' if I expect to add more lines in the future, even if I only have 1 statement right now.

You could easily do a single line for this example:
if keydown(200) playerY=playerY-1


instead of:

if keydown(200)
  playerY=playerY-1
endif


however, who guarantees me that this will be all I need?

if keydown(200)
  playerY=playerY-1
  if playerY<0 playerY=0
  DrawPlayer()
  EmitSound()
endif


Usually, a coder can predict how big these blocks will grow eventually..


jfk EO-11110(Posted 2003) [#11]
Using "Then" will also make it easier to format the source properly. I'd suggest you should use only these two forms:
if a=0 then b=1

or
if a=0 then
  b=1
  c=2
endif

This way it's still qbasic compatible, which was probably the most common basic on the pc platform for many years (byside visual basic, which is something comletely diffrent) and therefore it's a kind of a convention.

if you want to write multiple commands on one line, I'd suggest to always use the ":" as a seperator, eg:
if a=1 then : b=2 : else : b=0 : endif



CS_TBL(Posted 2003) [#12]
What's so important about qbasic compatibility?

A simple routine with uniform basic commands might be compatible, but what about the other not-so-standard things?


jfk EO-11110(Posted 2003) [#13]
It's not the compatibility, it's the tradition.


ford escort(Posted 2003) [#14]
what about tratitional basic ?

10 Print "hello world"
20 If inkey$=chr$(27) Then End
30 Goto 10

lol ^_^


CS_TBL(Posted 2003) [#15]
From the point of view of the modern basics, the traditional linenumber-basic (while also lacking block if's, select, while, functions etc.) sucks ! :)


ford escort(Posted 2003) [#16]
i agree, that sucks NOW :)
but that was awesome !!!


Tricky(Posted 2003) [#17]
I do know that I recently started in php, and couldn't get used that the keyword "Then" is forbidden in php.... So far I coded the traditional BASIC and Pascal where "Then" MUST be implented, and of course, Blitz in which it doesn't matter...

The line-numbering... It's never been an ideal system, and you had to point your lines out well in order not to get stuck if you needed some extra lines... But they needed an easy to code environment I suppose, and it was easy to create the line-mumbering system I suppose... Gosh, I sometimes forget what it was like in those years :)


Barnabius(Posted 2003) [#18]
How it was in those years... Actually it was much more fun than today. Even with all the line numbers one had to cope with. Aahh... the days of the good old REN 10,20 command :-)

Barney


SoggyP(Posted 2003) [#19]
Hi Folks,

@Barnabius: You goit, rubbing your REN command in the faces of us C64 users who didn't have such luxuries. Nor did we have, what was it, Autoline, which gave the next line number in sequence. Saying that, there was no plot, circle, line, rect or CLS, either. I yearn for the days when clearing the screen meant printing an inversed heart.

Thank heaven for progress.

Later,

Jes


ford escort(Posted 2003) [#20]
the best at these time was that you had your basic in rom and for free with the computer... in other hand, blitz is far more fun to use.


Ross C(Posted 2003) [#21]
Hehe, i used simons basic extension cartridge. Had lodsa cool features :)


Hansie(Posted 2003) [#22]
my old days consisted of something similar to this:

LDA #$FF
STA $D020
...
...
gosh ... can't even remember how to code C64 Assembly language anymore :-( how sad ...


BobR3D(Posted 2003) [#23]
Heck- having a CLS command at ALL almost seems like "cheating"...!

Back in the REAL "old days" (on the Ohio Scientific C1P.. well before the C64), we had to "clear the screen" by doing:

10 FOR I=1 TO 24
20 PRINT
30 NEXT I


It's the "little things" the language programmers never thought of that made programming... er.. "fun".