IF...THEN won't let numbers to be summed


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938

    Default IF...THEN won't let numbers to be summed

    Hello,

    I can't find out why this doesn't work:
    Code:
    if mem_l = 2 then speed = speed + 1 : gosub game_speed
    while this does:
    Code:
    if mem_l = 2 then speed =  1 : gosub game_speed
    According to the PBP Compiler's manual, both syntax look okay.

    When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.

    I've attached my code (the concerned lines are labeled 'Set game speed according to level).

    Any help is appreciated.
    Attached Files Attached Files
    Roger

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    Hello,
    I can't find out why this doesn't work:
    Code:
    if mem_l = 2 then speed = speed + 1 : gosub game_speed
    while this does:
    Code:
    if mem_l = 2 then speed =  1 : gosub game_speed
    According to the PBP Compiler's manual, both syntax look okay.
    When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.
    I've attached my code (the concerned lines are labeled 'Set game speed according to level).
    Any help is appreciated.
    You can't have a 'colon' in a single line if/then statement. You have to split it up, like this:

    If mem_1 = 2 then
    speed = speed + 1
    gosub game_speed
    EndIf

    or

    If mem_1 = 2 then
    speed = speed + 1 : gosub game_speed
    EndIf

    I don't think the manual says it specifically, but that's the way it is.

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default ?

    Hi skimask,

    Thanks for the help. I'm in the office now and I'm not sure about this anymore but I think I have tried what you suggest already and didn't work too.

    Nevertheless, I'll try again tonight (I'm in the office now).

    BTW, this is an example one can find in the COMPILER's manual (confusing):

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1533&stc=1&d=117644278 8">
    I've used this syntax many times before and never had trouble.
    Attached Images Attached Images  
    Roger

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    That non-optional ENDIF to complete the structure might have something to do with it?

    I do not see it in your code.
    Dave
    Always wear safety glasses while programming.

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Don't know why one works and the other doesn't.
    I just stick with what does work...
    I know, no help, but it's the facts...


    Also, I just looked thru your code.
    There's an END statement in the middle before GAME_SPEED.
    You probably don't want that in there.

    And a couple of your for/next statements, you don't specifically tell PBP which variable you are 'next'ing with your 'for'.
    Might help, might not...
    Last edited by skimask; - 13th April 2007 at 14:10.

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    The second example in the first post does not work correctly.
    When I run my prog with the second example, my Speed VAR will go crazy and show up numbers between 50 and 240(?). Speed is declared as VAR Byte and set to "0" at beginning of prog.
    Look at this from the posted code.
    Code:
    'Set game speed according to level
        if mem_l = 2  then speed = 1 : gosub game_speed
        if mem_l = 3  then speed = 2 : gosub game_speed
        if mem_l = 4  then speed = 3 : gosub game_speed
        if mem_l = 5  then speed = 4 : gosub game_speed
    ''    if mem_l = 2  then speed = speed + 1 : gosub game_speed
    ''    if mem_l = 3  then speed = speed + 1 : gosub game_speed
    ''    if mem_l = 4  then speed = speed + 1 : gosub game_speed
    ''    if mem_l = 5  then speed = speed + 1 : gosub game_speed
    
        value = value + 1           'To change the "seed" for random #
        Goto MAIN
    Do you see a problem?
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Feb 2005
    Location
    Kolkata-India
    Posts
    563


    Did you find this post helpful? Yes | No

    Wink Does this makes sense

    Hi,



    Code:
    IF MEM_1 = 2 THEN SPEED = 1 : GOSUB SOMETHING
    is equivalent to
    Code:
    IF MEM_1 = 2 THEN SPEED = 1
    GOSUB SOMETHING
    or
    Code:
    IF MEM_1 = 2 THEN
    SPEED = 1
    ENDIF
    GOSUB SOMETHING
    Regards

    Sougata

Similar Threads

  1. Replies: 4
    Last Post: - 15th April 2009, 01:54
  2. Maths - Summing small numbers with decimals
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 22nd March 2008, 19:21
  3. Working with 3 byte numbers
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 26th July 2007, 22:59
  4. Splitting numbers and recombining them(EEPROM Error)
    By selbstdual in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd June 2007, 06:40
  5. Returning whole numbers for DS1820?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th February 2007, 12:15

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts