Jump to content
UBot Underground

A question regarding nested ifs..


Recommended Posts

Fellas?

 

Just wanted to know from your experience with using nested ifs-

 

If I have code like this -  multiple nested ifs..

 

if("") {
    then {
        if("") {
            then {
                if("") {
                    then {
                        if("") {
                            then {
                                if("") {
                                    then {
                                    }
                                    else {
                                    }
                                }
                            }
                            else {
                            }
                        }
                    }
                    else {
                    }
                }
            }
            else {
            }
        }
    }
    else {
    }
}

 

Does this cause Ubot to be unstable ..?

Do I need to limit to how many nested ifs we can use inside our script?

 

I came across this thread..  Its an old thread , but with Ubot  - many issues still remain.

Thanks.

 

 

Link to post
Share on other sites

anyway  neested "if" are not a good codingstyle use elseif  and/or set parameters like

if(bla=true)then{#parameter=$true}

if(#parameter=$true)then{do what u have to do}

and so on

  • Like 1
Link to post
Share on other sites

 

first thing is to separate your functions from functionality, thats very messy, and poor coding technique

 

 

anyway  neested "if" are not a good codingstyle use elseif  and/or set parameters like

if(bla=true)then{#parameter=$true}

if(#parameter=$true)then{do what u have to do}

and so on

 

Thanks for your reply - but I find in some circumstances nested ifs  become unavoidable,

also, Ubot does not appear to have a clean way to  return back  - if you are inside a 'loop' node?

 

 

I don't think there is a limit to keep UBot stable.

 

Tadej, I take it you mean  multiple nested ifs will not make Ubot Unstable -no matter how deep the nesting?

 

Thanks.

Link to post
Share on other sites

Thanks for your reply - but I find in some circumstances nested ifs  become unavoidable,

also, Ubot does not appear to have a clean way to  return back  - if you are inside a 'loop' node?

 

 

Tadej, I take it you mean  multiple nested ifs will not make Ubot Unstable -no matter how deep the nesting?

 

Thanks.

No, it shouldn't be a problem, although as other guys say, it's not a good practice to use nesting extensively.

Link to post
Share on other sites

 

No, it shouldn't be a problem, although as other guys say, it's not a good practice to use nesting extensively.

 

Thanks.

Link to post
Share on other sites
  • 3 weeks later...

Thanks for your reply - but I find in some circumstances nested ifs  become unavoidable,

also, Ubot does not appear to have a clean way to  return back  - if you are inside a 'loop' node?

 

If you use the command "return" it will return to the beginning of the loop.

 

If you use "return" in a "loop while" it will return back to the beginning and check the condition.

  • Like 1
Link to post
Share on other sites

If you use the command "return" it will return to the beginning of the loop.

 

If you use "return" in a "loop while" it will return back to the beginning and check the condition.

??? Example pls !!!

Link to post
Share on other sites

??? Example pls !!!

Hi Blumi,

 

These are not necessarily useful examples, I created them just to demonstrate the concept:

 

testing()

alert("Counter: {#counter}")

define testing {

    set(#counter, 0, "Global")

    loop(5) {

        increment(#counter)

        return("")

        alert("Commands below the return won\'t get excecuted. You will not see this message when running the script.")

    }

}

 

As you can see even though there is a return command, the bot will just jump back to the beginning of the loop = there will still be 5 iterations, but commands below return won't get executed (obviously you could add an if statement for the return command).

 

Similar example with loop while:

 

testing()

alert("Counter: {#counter}")

define testing {

    set(#counter, 0, "Global")

    loop while($comparison(#counter"<", 5)) {

        increment(#counter)

        return("")

        alert("Commands below the return won\'t get excecuted. You will not see this message when running the script.")

    }

}

 
The return command will jump back (return) to check the condition, and only truly exit the loop when the given condition is met. So 5 iterations here as well.

 

The problem with these is that the return will also exit from the define, so commands after the loop in the same define won't get executed. So this is not the best coding habit either, but could be used in some cases. Anyway, this is how return works when used in loop or loop while statements.

Link to post
Share on other sites

Absolute new for me!
as i know $return is for give back a val in a function

 

and where is the differnce between

testing()
alert("Counter: {#counter}")
define testing {
    set(#counter, 0, "Global")
    loop while($comparison(#counter"<", 5)) {
        increment(#counter)
        return("")
        alert("Commands below the return won\'t get excecuted. You will not see this message when running the script.")
    }
}

 

and

 

testing()

alert("Counter: {#counter}")
define testing {
    set(#counter, 0, "Global")
    loop while($comparison(#counter"<", 5)) {
        increment(#counter)
    }
}

 

i see no benefit on it

Link to post
Share on other sites

Absolute new for me!

as i know $return is for give back a val in a function

 

As with most programming languages, return can also be used without value, not just in functions, but in command defines as well. It can be used to "return" from a define (exit) back to Main. That is used by many programmers, because there are cases when it's cleaner to use return to exit from a define instead of using IF statements for everything in the define. Of course -depending on your code- using lots of returns can be considered as bad coding habit too.

 

So the main purpose of knowing what I just demonstrated with loop and loop while, is to keep in mind that the return will not immediately exit the current command define (if that's what you wanted in your code) like it would normally do, it will simply go back to the beginning of the loop. So if you're using a return, keep all that in mind if it happens to be in a loop (or don't use it in a loop).

  • Like 1
Link to post
Share on other sites

thx marton
i thing i need a better example to understand this!
so ...next time u coding and u use it thing about me if u work with a return outside a func :)

Link to post
Share on other sites

thx marton

i thing i need a better example to understand this!

so ...next time u coding and u use it thing about me if u work with a return outside a func :)

 

 

Marton,

 

Just like Blumi, I thought the RETURN returns a value of something after reading about it in the pdf tutorials but I never really quiet understood how to use it.

And, if it can also exit the script out of loops and DEFINES like you say then great!

Show us some more examples when you can. Ok ?

 

Cheers!

 

Here's what you need to know: return will EXIT the function/custom command immediately, returning to the caller. It doesn't necessarily need a value either. You could have something like this in your custom command (define) anywhere:

 

if("condition") {

    then {

        return("")

    }

}

 

So you could have many commands before and/or after this IF statement. If the condition is met, it will exit immediately, but if not, it will just go on with the rest of the code in the define. Yes, this could be solved with conditional statements instead of having a return in the middle of your define, but sometimes visually this can be a better choice, rather than having many long, nested IFs in your code.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...