Sign in to follow this  
Followers 0
NochnoiDozor

Very simple if statement question

9 posts in this topic

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

Share this post


Link to post
Share on other sites



Please show some actual example code which you have problems with.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

HI

because youve used elseif use endif.

but in your first post you have it written right!!

Edited by Cue

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

EDIT: Fixed it! All is working well - thanks guys.

Edited by NochnoiDozor

Share this post


Link to post
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
Sign in to follow this  
Followers 0