Author Topic: Don't get Else If  (Read 2724 times)

Why would you use the else if conditional statement? Is it a back up for if if it fails or something? I don't get the http://scatteredspace.com/forum/index.php?PHPSESSID=21fc8f5fd8963604164a2c78389ff9a5&topic=480.0 explanation on it exactly

It allows you to execute code only if all other conditionals in the block checked so far have not resulted to true. Of course you could just do another if statement, but that's inefficient. I'll explain.

Let's say you have the following code:

Code: [Select]
%a = true;
%b = false;

if(%a)
{
echo("a is true!");
}

if(%b)
{
echo("b is true!");
}

Let's assume that for whatever reason, only a OR b can be true, but never both.

The code will check if %a is true, and it is. So it echoes into the console that a is true. But then it checks if %b is true, but we already know that since %a is true, %b can't be true! It's unnecessary to check.

If we typed in else if(%b) then it would only check if %a is true, and know that %b does not need to be checked.

Let's assume that for whatever reason, only a OR b can be true, but never both.

For example,

%a = !isObject(%pl);   //This gets whether there is not a player %pl.
%b = (%a ? false : %pl.getState() $= "DEAD");   //This gets whether the player %pl is dead.


Since a non-existant object can't, strictly speaking, be dead, only one of these may be true.


This also may be a rather bad example since the exclusion of state %a && %b is hard-coded into %b itself. If %a is true, %b's state MUST be false.


%b = (%a ? false : %pl.getState() $= "DEAD");   //This gets whether the player %pl is dead.[/tt]

what does the ? mean?



Let's assume that for whatever reason, only a OR b can be true, but never both.
So they can never be both true or is that part of the example?

So they can never be both true or is that part of the example?
It's part of the example. Whether or not you need to use else all depends on the context of the problem that you're trying to solve.

It's part of the example. Whether or not you need to use else all depends on the context of the problem that you're trying to solve.

Okay. So, from what I understand is that the function will execute if the else if conditional: %a is true or if %b is true. It will not check if %b is true because %a is already true, but if both aren't true, then it will not execute.

It's like a tongue twister in my head, sorry if I get annoying >_<.

Well they way I use it is something like this: (snip of WIP)
Code: [Select]
if(%start!$="")
{
%middle=%middle SPC %note;
%q++;
Playabc2(%string,%tempo,%q,%octive);
}
else if(%note$="Af")
{
%note="Ds";
%q++;
playabc("Piano_"@%octive@%note);
$play=schedule(%tempo,0,playabc2,%string,%tempo,%q,%octive);
$Note++;
}
else if(%note$="Gf")
{
%note="Fs";
%q++;
playabc("Piano_"@%octive@%note);
$play=schedule(%tempo,0,playabc2,%string,%tempo,%q,%octive);
$Note++;
}
blah

This will check if there is a %start, with the first regular if statement, if there isn't a %start if will go on to check what the note is, but if there is a %start, then there's no need to check what the note is.

Okay. So, from what I understand is that the function will execute if the else if conditional: %a is true or if %b is true. It will not check if %b is true because %a is already true, but if both aren't true, then it will not execute.

It's like a tongue twister in my head, sorry if I get annoying >_<.

Here's an if/else block.

if(%condition)
{
    conditionIsTrue();
}
else
{
    conditionIsFalse();
}


Here's an if/elif/else block, broken down.

if(%conditionA)
{
    conditionAIsTrue();
}
else
{
    if(%conditionB)
    {
        conditionBIsTrue();
    }
    else
    {
        neitherConditionIsTrue();
    }
}



It's not quite as simple as that, but for your purposes, all you need to know is that the else if is a formalization to prevent the code from becoming a half-mile to the right of the screen.

Okay SO

Code: [Select]
function test()
{
    %a = 2;
    %b = 3;

    if(%a == 3)
        {
            dosomething();
        }
    else if(%b == 3)
        {
            dontdosomething();
        }
else
    die();
}

Will mean that test will execute dontdosomething

Right?
« Last Edit: July 19, 2014, 09:34:04 PM by chubaka452 »

Just to let you know - in TorqueScript, you need to put brackets around your functions.

function stuff()
{
    echo("Stuff!");
    //blah blah blah
}

-snip-
Yes, because you set %a to 2 and check if it's 3, which in this case, it's not.
Also, another thing - if you only have one line of code to run after a conditional statement, you don't need brackets.
Like so:

function test()
{
    %a = 2;
    %b = 3;
    if(%a == 3)
       blah();
    else
        asdf();
}

No, I'm not fixing your code, that was just an example and some tips.
« Last Edit: July 19, 2014, 09:33:22 PM by Cruxeis »

Just to let you know - in TorqueScript, you need to put brackets around your functions.

function stuff()
{
    echo("Stuff!");
    //blah blah blah
}
Yes, because you set %a to 2 and check if it's 3, which in this case, it's not.
Also, another thing - if you only have one line of code to run after a conditional statement, you don't need brackets.
Like so:

function test()
{
    %a = 2;
    %b = 3;
    if(%a == 3)
       blah();
    else
        asdf();
}

No, I'm not fixing your code, that was just an example and some tips.

Oh thanks!

%a = !isObject(%pl);   //This gets whether there is not a player %pl.
%b = (%a ? false : %pl.getState() $= "DEAD");   //This gets whether the player %pl is dead.


I'm sorry, but why would you bother using the ternary operator when he clearly doesn't understand basic if/else conditional statements. It seems self inherent that if he didn't know how "else" works he wouldn't know what the shorthand operator for an inline if/else statement is. I'm not even complaining that you used it, but why couldn't you have saved him the confusion and just explained it real quick?



Imagine you have a variable: %type. This variable holds the type of, say, an animal. Say you write a function to see if an animal is cute. You would probably do something like:
Code: [Select]
function isCute(%type) {
    if(%type $= "cat") {
        %cute = true;
    }
    else if(%type $= "bunny") {
        %cute = true;
    }
    else if(%type $= "puppy") {
         %cute = true;
    }
    else {
        %cute = false;
    }
    return %cute;
}

The "else if" statement allows you to only have to check if it's cute once and skip all the remaining checks. If it's cute at any point, it doesn't need to check if it is or isn't cute anymore.

basically


if(%a){
  do_a();
}
else if(%b){
   do_b();
}
else{
   echo("no");
}


is equivalent to


if(%a)
{
   do_a();
}
else
{
   if(%b){do_b();}
   else{echo("no");}
}


It's just cleaner and easier to read.