ID:178210
 
With all the talk going on in the Creations forum, I was kinda wondering. Just what is a nested if statement?

I have something like this, are there any of those nested if statements in it? Is it something that can be avoided?

mob/proc/getname(mob/M)
var/Blah = input(Blah)
if(!loginname)
blah
return
if(loginname == "new")
Blah
if(!loginname)
Blah
if(fexists("Players/[ckey(loginname)]"))
Blah
return
if(newpassword=="Blah1" && confirmpassword=="Blah2")
if(loginname=="Zagreus"||"Deraxian"||"BobJr")
Blah
if(newpassword != confirmpassword)
Blah
getname(M)
return
if(newpassword==null)
Blah
getname(M)
return

Blah
return
else
if(fexists("Players/[ckey(loginname)]"))
Blah
if(correctpassword==passwordprompt)

if(F["Loaded"] == 1)
Blah
return
LoadMob(M, loginname)
return

else
Blah
getname(M)
return
else

Blah
getname(M)
return

I "blah"ed out all the irrelevant stuff because I really like my little login sequence, even if it may be ineffecient :) , and don't want to see it elsewhere. Plus I doubt anyone would read through this if it was 1500+ lines. :)
Hrmm, how bad would it be if I didn't use mob vars at all, and just kept checking and updating the stuff I store in savefiles instead? I guess that would really cause a lot of problems in the long run?

Would be nice though, real time updates of savefiles, no having to have a huge list of mob vars either.

And, I know loops are really bad, but just about all muds need some sort of timer. If I just had one little proc with a while(1) that started up in world.New() and did three for(mob/M in world)s and two for(obj/O in world)s and a single for(area/A in world) every 15 seconds or so. Just to serve as an all purpose timer and and update some misc. stuff I plan on, would that be bad?
You basically answered your own post with the code snippet. A nested if, or even a nested loop, is a series of statements that get deeper and deeper into itself in order to come to some descision about some code (or in the case of loops, to perform a sequence of code over again and again). Not exactly the best definition, by the way...

A non-nested set of ifs may look like this (non-language specific, but the concept is the same):
if (jimm == "great") then
  echo "jimm is great"
if (jimm <> "great") then
  echo "jimm is not great"
if (jimm == "great" and time == "daytime") then
  echo "jimm is great and sleeping!"
if (jimm == "great" and time == "nighttime") then
  echo "jimm is great and on the prowl!"

nested ifs can allow you to make complex descisions in fewer lines of code, or at least reduce the amount of repeated content (usually)... here is the above example converted to nested ifs:
if (jimm == "great) then       <-- in DM: if(jimm=="great")
  echo "jimm is great"         <-- in DM: world << "jimm is great"
  if (time == "daytime") then
    echo " and sleeping!"
  if (time == "nighttime") then
    echo " and on the prowl!"
else
  echo "jimm is not great"

nested loops are cool because you can create an outer loop than does events n-number of times, and have inner loops perform other actions within the scope of the outer loop (this time attempted in DM):
for(i=1, i<6, i++)     <-- loop from 1 to 5
  world <<  "the variable i is [i]"           <-- display the value of i
  for (b = 1 to 2)
    world << " b is [b]"
this will create the output:

the variable i is 1
b is 1
b is 2
the variable i is 2
b is 1
b is 2
the variable i is 3
b is 1
b is 2
the variable i is 4
b is 1
b is 2
the variable i is 5
b is 1
b is 2

loops are good for multiple repetitive tasks that would involve the same object (as in the case of BYOND).

Hope that helps... I'm sure others can provide more elaborate examples...
In response to digitalmouse
Ohh, and those are bad?

So your example of a non-nested if in DM would be:
mob/proc/Blah()
if(Blah1 != Blah2)
blah
return
if(Blah1 != blah2 && blah3 == blah3)
blah
return
if(blah1 != blah2 && blah3 == blah3 && blah4 != blah5 && blah5 == blah6)
blah
return

I guess I understand, but to me it seems like the software would handle it the same way, I guess not though. Thanks for your explination.

One other question though. Would it be more effecient to, when something fails, such as like if the player inserts a blank line for a character name or something, to restart the proc alltogether with getname(M), or would it be better to use goto? In other words, having procs calling themselves, good or bad?
In response to Deraxian
Deraxian wrote:
I guess I understand, but to me it seems like the
software would handle it the same way, I guess not
though. Thanks for your explination.

Depending on how the compiler handles things, nested IFs may or may not be more efficient. That's a question that DanTom would handle better, since this is their baby.

In my thinking, nested IFs give your program logic better complexity to handle multiple decisions ("if gas is in the tank is true, then check: if key is in slot, then start car")... you can bypass a whole series of un-needed checks, thereby speeding up logic processing. On the other hand, poor design would make nested IFs a drain on resources...

...In other words, having procs calling themselves, good
or bad?

The way DM deals with this is something I'm not familiar with (may be different than say PHP or Java), so I'll let someone else comment on that...