Page 1 of 1

IF / ELSE / ENDIF question

PostPosted: Thu Apr 26, 2012 3:29 pm
by dpayer
I am trying to see the error of my logic here. In a simple app with a text entry (creating variable value of [textentry1] ) and a button with the following action code:

Code: Select all
If "[textentry1]" "<>" ""

   If "[textentry1]" "=" "1"
    Alertbox "test" "you entered 1 (one)"
   Else
   If "[textentry1]" "=" "2"
    Alertbox "test" "you entered 2 (two)"
    Else
     If "[textentry1]" "=" "3"
    Alertbox "test" "you entered 3 (three)"
    Else
    If "[textentry1]" "=" "4"
    Alertbox "test" "you entered 4 (four)"
   
    Endif
Else
Alertbox "test" "you didn't enter anything!"

Endif


This is similar to some code I am using elsewhere with conditional statements within other conditional statements. I would think that if I have no value in [textentry1] when I click the button with this code, it would give me an alertbox saying "you didn't enter anthing!" but it does not work that way. If I put in 1 or 2 or 3 or 4 and click the button. It does popup as expected.

1) Is the IF / ELSE / ENDIF only meant to have one use of 'ELSE'?
2) If using a lengthy IF / ELSE / ENDIF within another IF section, does this cause a confusion?

TIA,
David P.

PostPosted: Thu Apr 26, 2012 4:42 pm
by Rasl
If "[textentry1]" "<>" ""
If "[textentry1]" "=" "1"
Alertbox "test" "you entered 1 (one)"
Else
If "[textentry1]" "=" "2"
Alertbox "test" "you entered 2 (two)"
Else
If "[textentry1]" "=" "3"
Alertbox "test" "you entered 3 (three)"
Else
If "[textentry1]" "=" "4"
Alertbox "test" "you entered 4 (four)"
Endif
EndIf
EndIf

EndIf
Else
Alertbox "test" "you didn't enter anything!"
Endif

salu2

PostPosted: Thu Apr 26, 2012 6:06 pm
by Gaev
David P:

You were missing a couple of EndIf commands ... and for completeness, I added the condition where it was none of "nothing, 1, 2, 3 or 4"
Code: Select all
If "[textentry1]" "<>" ""
   If "[textentry1]" "=" "1"
      Alertbox "test" "you entered 1 (one)"
   Else
      If "[textentry1]" "=" "2"
         Alertbox "test" "you entered 2 (two)"
      Else
         If "[textentry1]" "=" "3"
            Alertbox "test" "you entered 3 (three)"
         Else
            If "[textentry1]" "=" "4"
               Alertbox "test" "you entered 4 (four)"
            Else
               Alertbox "test" "you entered something else"
            EndIf
         EndIf
      Endif
   EndIf
Else
   Alertbox "test" "you didn't enter anything!"
Endif

Put it another way ...
Code: Select all
L1:If "[textentry1]" "<>" ""
   L2:If "[textentry1]" "=" "1"
      Alertbox "test" "you entered 1 (one)"
   L2:Else
      L3:If "[textentry1]" "=" "2"
         Alertbox "test" "you entered 2 (two)"
      L3:Else
         L4:If "[textentry1]" "=" "3"
            Alertbox "test" "you entered 3 (three)"
         L4:Else
            L5:If "[textentry1]" "=" "4"
               Alertbox "test" "you entered 4 (four)"
            L5:Else
               Alertbox "test" "you entered something else"
            L5:EndIf
         L4:EndIf
      L3:Endif
   L2:EndIf
L1:Else
   Alertbox "test" "you didn't enter anything!"
L1:Endif

PostPosted: Thu Apr 26, 2012 8:05 pm
by dpayer
Gaev wrote:David P:

You were missing a couple of EndIf commands ... and for completeness, I added the condition where it was none of "nothing, 1, 2, 3 or 4"


Well darn.... bad logic has struck me again. :)

I *thought* that the IF state was evaluated and if the following statement was true then all other 'else' statements were ignored leaving only the closing endif.

Thanks for the insight Rasl and Gaev.

David P.