guessing game

Blitz3D Forums/Blitz3D Beginners Area/guessing game

melonhead(Posted 2009) [#1]
i got the book 3d programming for teens and I'm at the guessing game and I'm having trubble getting it to work the right way it stay in the first loop and doesn't continual to the net

here the code


Print "welcome To guessig game!"

AppTitle "guessing game"

SeedRnd MilliSecs()

numberofquess = Rand(1,100)
numofquesses = 0

.loopbegin

quess = Input$("guess a number ")

If guess > 100 Or guess <1

Print " pick a number from 1 to 100, silly!!"

Goto loopbegin
EndIf

numofquesses = numofguesses + 1

If guess < numbertoguess Then
Goto loopbegin
Print" the number was to o low."
Else If guess > numbertoguess Then
Print " the number was too high."
Goto loopbegin
EndIf
Print " you guess the number"+numbertoguess+"in"+numberofguesses + "tries!"
Delay 5000


Nate the Great(Posted 2009) [#2]
hehe u spelled "guess" "quess" I have to say g and q are not that close on the keyboard although they do look alike lol. that screwed up ur logic which is perfectly right, here it is with fixed spelling errors :)




AJ00200(Posted 2009) [#3]
Nate fixed this, but you need the then if you use end if.
If statements without thens snd end ifs must be kept on one line.
Also, next time, could you type this :

[ codebox ] <PASTE CODE HERE> [ /codebox ]

Without spaces inside the brackets.
It makes everything nicer (and shorter with long code samples.

PS-that is a good book. I learned from there too.


GfK(Posted 2009) [#4]
"Games Programming for Teens" suggests that you code in this way?? Its a pretty awful specimen of code, considering its supposed to be teaching you how to program. For example:
If guess < numbertoguess Then
Goto loopbegin
Print" the number was to o low."
You will never ever see "the number was to o low." because of the Goto before it.

but you need the then if you use end if.
No, you don't.


Ginger Tea(Posted 2009) [#5]
sometimes books intentionally added typo's and other code errors just so people wouldnt just run from disk/cd see that it works and just cut and paste, im not saying its true in this books case, some typeos are unintentional, but its good to know what the code someone else wrote does so you can understand how to reuse it


AJ00200(Posted 2009) [#6]

no you don't

Really? I think that book said this.
So I can do
If (condition) ;no then
   command
   command
   comnand
end if

without a then?
I know you don't need a then in single line conditional statements.
Ill check this out soon.


Warner(Posted 2009) [#7]
I believe 'then' is optional, in any case.


AJ00200(Posted 2009) [#8]
Oh OK.
Well, I guess theres no problem then.
Good luck with Blitz 3D.
Need help? Ask.
Thats why were here


_PJ_(Posted 2009) [#9]
The following are all acceptable syntax for If/Then :

If (a) Then DoSomething()

If (a)
DoSomething()
End If

If (a) Then
DoSomething()
End If

If (a) Then DoSomething() : DoSomethingElse()

If (a) Then
DoSomething()
ElseIf (b)
DoSomethingElse()
ElseIf (c)
DoSomethingElseAgain()
End If

If (a)
DoSomething()
Else
DoSomethingElse()
End If

If ((a) And (b) Or( (c) Or (d) And(a And b)))
DoSomething()
Else
If (Not(a)) Then If(b) Then DoSomethingElse()
End If


Then is only necessary if a True argument reconciliation is to be parsed within the same line, however, if more than one line follows Then, an End If is required.


AJ00200(Posted 2009) [#10]
OK, thanks.


Warner(Posted 2009) [#11]
But this is a valid syntax?
If (3=3) Print "pfew .. no bugs here" Else Print "aie .. something's wrong"



_PJ_(Posted 2009) [#12]
That's a new one, Warner! It seems that End If is not required when Else is present n the same line either. It till holds for
If (3=3) Then Print "pfew .. no bugs here" Else Print "aie .. something's wrong"
too!

For readability/clarity and possible future-editing's sake, I tend to always use nets of If ... End If, and avoid Then's (and Else if I can help it!)


_PJ_(Posted 2009) [#13]
I've had a little investigation of If/Else and End
It's sometimes a little strange, but generally speaking they always follow a format of:

If (Argument) Then Print "Only If True" : Print "Also Only If True" Else Print "Only If False":Print "Also Only If False"
Print"Always"

The last line (Print "Always") isn't truly necessary, but is added to show the end of the If/Else selection.

The above is identical to:
If (Argument)
Print "Only If True"
Print "Also Only If True"
Else
Print "Only If False"
Print "Also Only If False"
End If
Print"Always"


So, my earlier statement should have been that END IF is only required if the statements fall on separate lines.

Considering:
If (Argument)Print "Only If True"
Print"Always"

It seems that THEN is completely redundant in all cases. However, it may help in readability?

Else, however, is a little more necessary in some situations.

Multiple ELSE can also be combined, even along with : statements, that keep the conditions the same for the group of commands until the end of the line or the next Else.

If (Fruit=Banana) Print "It's a banana" Else If (Fruit=Apple) Print "Apple" Else If (Fruit<>Orange) Print "It's none of the fruits I like!": Print "So, it must be watermelon" Else Print "It has To be an orange?"
Print"Always"

If Any of the commands again, fall onto another line, then END IF must be used.

So, back to the main topic -

THEN is entirely optional, and never actually required.
My guess is that the book, if specifically for B3D may have been developed from a basis in other languages (or even the Amiga Blitz lang????) where Then's are necessary or such. Some folks would agree and call it good programming practice to use THEN but overall I think it may be down to personal preference. Personally, I think having a THEN stuck on the end of lines is confusing, it LOOKS LIKE it suggests If (blah) Then DO NOTHING, though that's not the case at all if there's an End If further down.