IF Then Question


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2005
    Posts
    37

    Default IF Then Question

    Hi!

    Can someone please tell me if the following code is equivelent?

    If B0 <> 10 Then
    B0 = B0 + 1
    B1 = B1 - 1
    Endif


    Is the above the same as this below?


    If B0 <> 10 Then B0 = B0 + 1 :B1 = B1 - 1

    I am trying to determine whether (in the second example) "B1 = B1 + 1" would be executed only if B0 <> 10.

    Thanks.

  2. #2
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    The two if/then instructions are doing the same thing.

    Surely you ment B1= B1 - 1

    Cheers

    Al.
    All progress began with an idea

  3. #3
    Join Date
    Jun 2005
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    Thanks for your help. And yes, I meant B1 = B1 -1.

    I saw conflicting informaition on posts that suggested in a single line statement, consecutive stements on the same line, when separatred by ":" are treated as a statement on a new line and is not part of the If Then.

    So just to confirm, as long as I am not using "else", I have a choice to use mulitple lines blocked with "endif" as the last line or I can have a single line with mulitple statments separated by ":" and no endif? Each will be treated the same?

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    I gave you an uncorrected answer! If you need to use multiple lines command then use the if then/Endif statement. Using the ":" is a new line statement.

    To stay on the same line your code should be:

    If B0<>10 then B0 = B0 + 1 and B1 = B1 - 1

    Al.
    All progress began with an idea

  5. #5
    Join Date
    Jun 2005
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    Thanks Al!

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


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    From the good ole manual
    IF..THEN can operate in 2 manners. In one form, the THEN in an IF..THEN is essentially a GOTO. If the condition is true, the program will GOTO the label after the THEN. If the condition evaluates to false, the program will continue at the next line after the IF..THEN. Another statement may not be placed after the THEN, it must be a label.

    If Pin0 = 0 Then pushd ' If button connected to Pin0 is pushed (0), jump to label pushd
    If B0 >= 40 Then old ' If the value in variable B0 is greater than or equal to 40, jump to old
    If PORTB.0 Then itson ' If PORTB, pin 0 is high (1), jump to itson
    If (B0 = 10) AND (B1 = 20) Then loop
    In the second form, IF..THEN can conditionally execute a group of Statements following the THEN. The Statements must be followed by an ELSE or ENDIF to complete the structure.

    If B0 <> 10 Then
    B0 = B0 + 1
    B1 = B1 - 1
    Endif

    If B0 = 20 Then
    led = 1
    Else
    led = 0
    Endif
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Jun 2005
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    Thanks Dave.

    The PBP3 manual itself left me confused as per the examples below;

    More Examples:
    IF B0 <> 10 THEN
    B0 = B0 + 1
    B1 = B1 - 1
    ENDIF
    IF B0 = 20 THEN
    led = 1
    ELSE
    led = 0
    ENDIF
    IF B0 >= 40 THEN old
    IF PORTB.0 THEN itson
    IF (B0 = 10) AND (B1 = 20) THEN mainloop
    IF B0 <> 10 THEN B0 = B0 + 1: B1 = B1 - 1

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


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    It is only a little bit out dated, but for the most part, IMHO, this is still the best manual.
    http://melabs.com/resources/pbpmanual/
    It is always open on a tab in my browser... for those special moments...
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    Jun 2005
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: IF Then Question

    I agree Dave!

    Thanks.

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