If, Then, Else problems

Blitz3D Forums/Blitz3D Beginners Area/If, Then, Else problems

Waz(Posted 2003) [#1]
I am trying a little go at NESTED IF's.

I have this

billy = 20
sam = 10


If billy = 10
If sam = 5
Text 0,130, "Sam and billy are 15 in total"
Else
If billy = 20
If sam = 10
Text 0,130, "same and billy are 30 in total"
Else
If billy = 50
If sam = 50
Text 0,130, "sam and billy are 100 in total"
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf

now if i edit sam and billy varible to higher like 20 for billy and sam 15 then nothing gets printed?

I basicly checked this over with the book I have and its basicly the same but slightly edited....
any help plz

I even tried doing

billy = billy +10
sam = sam +5
but still nowt text comes up stating their age....


pjd(Posted 2003) [#2]
you don't have any code for if billy = 20 and sam = 15 !!

It will help to see what's going on if you "indent"
your code, ie line up the ifs and end-ifs so you can
see which end-if corresponds with which if :-

If billy = 10 
   If sam = 5 
      Text 0,130, "Sam and billy are 15 in total" 
   Else 
      If billy = 20 
         If sam = 10 
            Text 0,130, "same and billy are 30 in total" 
         Else 
            If billy = 50 
               If sam = 50 
                  Text 0,130, "sam and billy are 100 in total" 
               EndIf 
            EndIf 
         EndIf 
      EndIf 
   EndIf 
EndIf


This might look better if you use "AND" statements
instead of all those IFs :-

If billy = 10 and sam = 5 
   Text 0,130, "Sam and billy are 15 in total" 
Else 
   If billy = 20 and sam = 10 
      Text 0,130, "same and billy are 30 in total" 
   Else 
      If billy = 50 and sam = 50 
         Text 0,130, "sam and billy are 100 in total" 
      EndIf 
   EndIf 
EndIf



You should have an extra "else" in to cater for any
situations which you have not explicitly checked
for in your "IF" statements :-

If billy = 10 and sam = 5 
      Text 0,130, "Sam and billy are 15 in total" 
Else 
   If billy = 20 and sam = 10 
      Text 0,130, "sam and billy are 30 in total" 
   Else 
      If billy = 50 and sam = 50 
         Text 0,130, "sam and billy are 100 in total"
      else
         Text 0,130, "Something else !!"
      EndIf 
   EndIf 
EndIf



You might want to consider what you want to happen if
billy = 8 and sam = 7 ......
In the above program, it will say "something else"
when you might expect it to say "Sam and billy are 15 in total".
It's easy to forget that the computer will print out
everything you tell it to without checking if it's
correct or not !


If billy + sam = 15 
      Text 0,130, "Sam and billy are 15 in total" 
Else 
   If billy + sam = 30 
      Text 0,130, "sam and billy are 30 in total" 
   Else 
      If billy + sam = 100
         Text 0,130, "sam and billy are 100 in total"
      else
         Text 0,130, "Something else !!"
      EndIf 
   EndIf 
EndIf



When you've mastered this, have a look at the
"case" and "select" commands (see the help text for
info on these)


Beaker(Posted 2003) [#3]
I helps to indent your code so you can visualize it better:
billy = 20 
sam = 10 


If billy = 10 
	If sam = 5 
		Text 0,130, "Sam and billy are 15 in total" 
	Else 
		If billy = 20 
			If sam = 10 
				Text 0,130, "same and billy are 30 in total" 
			Else 
				If billy = 50 
					If sam = 50 
						Text 0,130, "sam and billy are 100 in total" 
					EndIf 
				EndIf 
			EndIf 
		EndIf 
	EndIf 
EndIf 


What pjd said. :)


Waz(Posted 2003) [#4]
ok cool

What I was basicly trying to do was be able to change billy and sam so it would print out the diffrent results kinda thing....

so much to learn and only so much my brain can handle.. its very confusing stuff LOL


Waz(Posted 2003) [#5]
What I am thinking now is.. What can I do to use this.
Example I tried doing above that code


billy = 50
sam = 50

but none of the if get typed except somthing else .

What can I do to use this sorta tihng, wht I am trying to aim at doing and learning using ifs, then, endif for my own use kinda thing.


Beaker(Posted 2003) [#6]
billy = 20 
sam = 10 
Text 0,130, "Sam and billy are "+(billy+sam)+" in total"


Sorted.


Waz(Posted 2003) [#7]
I think the most confusing thing for me like I said in another post. Is basicly I learn this stuff but still not got a clue how it would work in a game.. I even looked at other games code and samples and it all confuses me.


Beaker(Posted 2003) [#8]
Rule 1: Start simple:

Exercise 1:
Make a paddle/blob/block move left or right according to which arrow key you press.

Exercise 2:
2a) Make a smaller blob fly up/out of the paddle when you press SPACE. 2b) Make it come back instantly when it goes off the screen.

Exercise 3:
Make a blob move left and right on its own at the top of the screen, changing direction when it hits the screen edge.

Exercise 4:
Do the same as exercise 3 - but make 10 of them appear in different places, but with the same basic behaviour.


When you've done all the exercises come back.


Rule 2:
Ignore anything that confuses you too much. You can always look at it again later.


Waz(Posted 2003) [#9]
ok I try that..
Not a clue how to do it but urmmm I read somewhere

Thankz!


Waz(Posted 2003) [#10]
Anyone know some tutorails on select statement.

My book merely has 1 page about it and does not explain too well...
and blitzcoder has no tuts...

Far as I know its like a type... well not like it but typed like a type..

Select (varible name)
Case etc etc

End select

It basicly allows u to check a varible for whats going on instead of typing


if player hit then
;take 2 points away
playerlife = playerlife -2


but with select it can be done like this right?

Select Playerhit
Case Playerhit
Playerlife = playerlife - 2
Case Playerjumped And hit
playerlife = playerlife - 3
Case Playerdead
playerlife = playerlife = DEAD or what ever... LOL


Am i correct or wrong here


Tricky(Posted 2003) [#11]
I see you are once again thinking way to difficult...

Let's first start on how the Select statement works, and come with complicated versions, later since I don't think your source will work at all...

Let's have a simple example

Global Number

Number = Input("Enter a number from 1 till 10")

Select Number
    case 1 Print "One"
    Case 2 Print "Two"
    Case 3 Print "Three"
    Case 4 Print "Four"
    Case 5 Print "Five"
    Case 6 Print "Six"
    Case 7 Print "Seven"
    Case 8 Print "Eight"
    Case 9 Print "Nine"
    Case 10 Print "Ten"
    Default Print "That was not a number between 1 and 10"
    End Select
End


Okay, as you may know this program will first ask the use to enter a number between 1 and 10 and the result will be stored in the variable "Number"....

With "Select Number", the system knows you are going to let the program act according to the number stored in "Number"...

The "Case" command will check if the number is in the variable is according to the number in the "Case" and execute the commands under that case part until the next "Case", "Default" or "End Select" command...

Thus when the use types 4, it'll ignore all cases until it sees "Case 4"...

Then it'll be
"Case 4 Print "Four""

That means that the program must put "Four" on the screen, and that is what will happen...

If no case is matching the variable then it will execute the commands behind "Default", if there's no "Default" the select routine will be ignored completely if no "Cases" match...

You get the idea?


Waz(Posted 2003) [#12]
yep.

kinda, how would such a thing be used in a game?


Waz(Posted 2003) [#13]
I did my own kinda thing and I was impressed.
It basicly goes to start until they type 1 to 4..
ehehe


; select example


Global number%
.start
number = Input ("Enter a number 1 to 4: ")

Select number

Case 1 Print "One"
Case 2 Print "Two"
Case 3 Print "Three"
Case 4 Print "Four"
Default Print "You did not enter a number between 1 to 4!!"
Goto start
End Select

WaitKey()


Tricky(Posted 2003) [#14]
Yup that works... Very well coded...

How much good it could do in a game... That's hard to explain in a few words... but it can really speed up your way of coding if you use it well...