none
More if statement trouble RRS feed

  • Question

  • If (n1 Or n2 Or n3 Or n4 Or n5 Or n6 Or n7 = "##") Then 
    TextWindow.WriteLine("Error") 
    Else 
    TextWindow.WriteLine("Congratulations...") 
    Endif 
    With the above code, why does the If statement only execute if n7 = "##" and not say n1 or n6? How do I change it so the if statement does infact execute for n1 to n7? Thanks

    Thursday, November 27, 2008 3:28 PM

Answers

  • The way that expression gets transformed in Small Basic (and most other languages) is this:

    If ((n1 = "True") or (n2 = "True") or (n3 = "True")... or (n7 = "##") Then
    ...
    ...

    In order to achieve what you intended, you'd have to rewrite your if condition as:

    If (n1 = "##" or n2 = "##" or n3 = "##" or n4 = "##" or n5 = "##" or n6 = "##" or n7 = "##) Then
    ...


    Thursday, November 27, 2008 6:07 PM
    Moderator
  • A possible approach might be to replace n1, n2, n3, n4, n5, n6, n7 with an array "n", and loop through it.

    For x=1 To 7 
        if (Array.GetValue("n", x)="##") then 
            TextWindow.WriteLine("Error")
            Goto end 
        EndIf  
    EndFor 
    TextWindow.WriteLine("Congratulations")
    end: 

    • Marked as answer by Dink87522 Thursday, November 27, 2008 11:53 PM
    Thursday, November 27, 2008 7:53 PM

All replies

  • The way that expression gets transformed in Small Basic (and most other languages) is this:

    If ((n1 = "True") or (n2 = "True") or (n3 = "True")... or (n7 = "##") Then
    ...
    ...

    In order to achieve what you intended, you'd have to rewrite your if condition as:

    If (n1 = "##" or n2 = "##" or n3 = "##" or n4 = "##" or n5 = "##" or n6 = "##" or n7 = "##) Then
    ...


    Thursday, November 27, 2008 6:07 PM
    Moderator
  • A possible approach might be to replace n1, n2, n3, n4, n5, n6, n7 with an array "n", and loop through it.

    For x=1 To 7 
        if (Array.GetValue("n", x)="##") then 
            TextWindow.WriteLine("Error")
            Goto end 
        EndIf  
    EndFor 
    TextWindow.WriteLine("Congratulations")
    end: 

    • Marked as answer by Dink87522 Thursday, November 27, 2008 11:53 PM
    Thursday, November 27, 2008 7:53 PM
  • Thanks for all the great help. I've got no programming experience so sorry for the sometimes quite simple questions I may ask. 
    Thursday, November 27, 2008 11:52 PM