Jump to content

Very simple if statement question


Recommended Posts

Forgive the simplicity of teh question, but I have code with a lot of nested if statements

if...

...endif

if...

...endif

if...

...endif

and so on

However, when the first 'if' is fulfilled, it doesn't continue to do the rest. How can I code it to complete all if statements after the first one?

btw, I have tried using 'elseif' statements, but still the same problem

Link to comment
Share on other sites

If $number = $counter Then
Send("hello1")
sentIt()
adding()
ElseIf $number = $counter then
Send("hello2")
sentIt()
adding()
ElseIf $number = $counter then
send("hello3")
sentIt()
adding()

and so on

(btw, counter and number are both incrememnted in sentIt()

So, is, say, number and counter are both 1, the first if statement to send hello1 is done, but it doesn't do the rest, even though number and counter are both incremented and the 2nd if statement is true.

Link to comment
Share on other sites

If's are easy to understand once you learn to follow the 'nest'. If you have not, use SciTe or equivilent to create a script in.

Now, just make a bunch of If EndIf. Nested inside one another, something like this

If
    If
        If
            
        Else
            
        EndIf
    Else
        If
        
        Else
        
        EndIf
    EndIf
Else
    If
        If
            
        Else
            
        EndIf
    Else
        If
            
        Else
            
        EndIf
    EndIf
EndIf

SciTe will let you 'fold' the script up with the + or - box on the left. You will quickly note that every If needs an EndIf, and that SciTe will visually show you where you are missing them. You can then 'fold' them up, and then start to follow them out, one 'fold' at a time on each branch until you see where an action is performed. Depending upon where the action is, the parent if may end, or it may follow a prior If.

It found it much easier using an IDE for those things, especially one like Scite that lets you fold.

Just my 2cents tho.

Sul

Link to comment
Share on other sites

But in my first post, if I use that code it only ever does one if statement, but I want it to do them all

Eehm, that's not true. If you build up your code like that you will get exactly what you want,

while if you do like on your second post it will only execute the statements for the first true

expression.

Link to comment
Share on other sites

THe best is to add a new function I name result and in the main code call it

The $number and $counter will be changed by each call with sentIt (if $number=$counter)

The code will be a lot clearer

func result($strResult)
  If $number = $counter Then
    Send($strResult)
    sentIt()
    adding()
  endif
endfunc

result("Hello1")

result("Hello2")

result("Hello3")

result("Hello4")
Edited by tresa
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...