*if *elseif and *else

So… this seems like a very noobish question but, I could use some clarification here:

As I understand it, when you use *elseif or *else beneath *if

Like so:

*if (variable = value)
 thing happens
*elseif (variable = value)
 different thing happens
*else
 alternate different thing happens

What happens is, if the *if fails, it then checks *elseif, and if the *elseif fails, it moves on to *else.

But if the initial *if passes, it ignores the *elseif and *else.

And if the *elseif passes (but the initial *if failed) it ignores the *else

Is this correct?

And if so… does this mean that multiple *elseifs will behave the same way? For example:

*if (variable = value)
 thing 1 happens
*elseif (variable = value)
 thing 2 happens
*elseif (variable = value)
 thing 3 happens
*elseif (variable = value)
 thing 4 happens
*else
 thing 5 happens

So in this code, “thing 3” will only occur if the previous checks failed, and “thing 4” will not occur even if it would otherwise pass the check (unless “thing 3” failed)?

So, even if all the variable checks pass, only the very first check would show, correct?

1 Like

Yep, you’ve got it right! You can use multiple *elseifs.

3 Likes

CS uses priority order to interpret (parse) its code.

2 Likes

Oookay. I wasn’t sure at all (neither the wiki nor the choicescript “tutorial” offer a very clear and comprehensive explanation of the functional relationship between the three).

Thanks for the help!

1 Like

Question here:

what exactly is the difference between *elseif trees and *if trees (aside from *else covering everything else, and *if trees not needing a *goto)
(yeah, 3 years in and it’s still a bit of a headscratcher for me)

2 Likes

With *elseif, the code chooses the first correct condition from the entire set. With just *if, the code will check all the conditions in the set.

I’ll link one of my old threads about it

8 Likes

The main thing that separates if from elseif trees is that *elseif’s will “override” an *if whereas several *if statements will not.

For example:

*temp var1 true 
*temp var2 false

*if (var1 = true) 
 var1 is true
*if (var2 = false)
 var2 is false

It will display:

var1 is true var2 is false.

But with *elseif…

*if (var1 = true) 
 var1 is true
*elseif (var2 = false)
 var2 is false

It will display:

var2 is false

Also, there is a way to get rid of *elseif and *else’s *goto requirements. It’s through implicit control flow.

1 Like

You know me, I don’t trust ICF. If it works well for you, great.

2 Likes

Does anyone know what the particular reason is for forcing *else statements to use *goto in the first place?

Not that it really matters, I’m just curious if it was implemented as some kind of bug-fix to an earlier version of choicescript or something.

I think it has to do with structuring in coding in general. My knowledge is limited, but iirc it has to do with how it is / was used to call various situations.
Think of printed CYOA books, and how one’d flip to different pages/numbers if they’d have one item or another…

and for choicescript, I’d say it was because the story would sometimes fall through a inner-text branch due to wonky structuring.

1 Like

Actually, var1 is true will be displayed here.

With multiple *if, all conditions are checked and executed if their condition passes, while on *if and *elseif chains once one of the conditions meet, the entire rest of the *if *elseif *else block will be skipped.

In this case *if (var1 = true) is evaluated and since its a true condition, var1 is true text is executed, and then the *elseif below is skipped without even being checked. If you had 10 more *elseif below they wouldn’t be checked either because one of them already met its condition.

*temp var1 false 
*temp var2 false
*temp var3 true
*temp var4 true
*temp var5 true
*temp var6 false
*if (var1 = true) <----- not passed
 var1 is true
*elseif (var2 = true) <----- not passed
 var2 is true
*elseif (var3 = true) <----- PASSED AND EXECUTED, the rest is skipped
 var3 is true
*elseif (var4 = true) <----- ignored because of previous condition met
 var4 is true
*elseif (var5 = true) <----- ignored because of previous condition met
 var5 is true
*elseif (var6 = true) <----- ignored because of previous condition met
 var6 is true
*else <----- ignored because of previous condition met
 All variables are false

In this example the result would be:

var 3 is true

And that’s it, because the *elseif (var3 = true) was met first, so it will skip the 4 other *elseif and *else below it.

But if it was:

*if (var1 = true) <----- not passed
 var1 is true
*if (var2 = true) <----- not passed
 var2 is true
*if (var3 = true) <----- passed and executed
 var3 is true
*if (var4 = true) <----- passed and executed
 var4 is true
*if (var5 = true) <----- passed and executed
 var5 is true
*if (var6 = true) <----- not passed
 var6 is true

The result would be:

var3 is true var4 is true var5 is true

Each *if is checked individually, so if they are true they will be executed. Note how I removed the *else, as there isn’t a way to use an *else in this block (unless you chained an enormous *if condition with many or including all these variables), if I added it like this:

*if (var1 = true) <----- not passed
 var1 is true
*if (var2 = true) <----- not passed
 var2 is true
*if (var3 = true) <----- passed and executed
 var3 is true
*if (var4 = true) <----- passed and executed
 var4 is true
*if (var5 = true) <----- passed and executed
 var5 is true
*if (var6 = true) <----- not passed
 var6 is true
*else <----- passed and executed
 All variables are false

The result would be:

var3 is true var4 is true var5 is true All variables are false

Because the *else is only working for the *if (var6 = true) condition. In the previous case of the *if/*elseif/*else the *else will only execute if none of the others are true in that block.

5 Likes

Oh! My mistake! Thank you for correcting me.

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.