# \*if \*elseif and \*else

**URL:** <https://forum.choiceofgames.com/t/if-elseif-and-else/79959>\
**Category:** ChoiceScript Help\
**Created:** [May 27, 2020, 4:13am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959 "2020-05-27T04:13:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evan3Michaels](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/evan3michaels/32/171767_2.png) [@Evan3Michaels](https://forum.choiceofgames.com/u/Evan3Michaels)\
**Post date:** [May 27, 2020, 4:13am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/1 "2020-05-27T04:13:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![EclecticEccentric](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/eclecticeccentric/32/112816_2.png) [@EclecticEccentric](https://forum.choiceofgames.com/u/EclecticEccentric)\
**Post date:** [May 27, 2020, 4:30am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/2 "2020-05-27T04:30:51Z")

</div>

Yep, you’ve got it right! You can use multiple `*elseif`s.

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [May 27, 2020, 4:35am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/3 "2020-05-27T04:35:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Evan3Michaels](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/evan3michaels/32/171767_2.png) [@Evan3Michaels](https://forum.choiceofgames.com/u/Evan3Michaels)\
**Post date:** [May 27, 2020, 4:37am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/4 "2020-05-27T04:37:21Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![MeltingPenguins](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/meltingpenguins/32/255855_2.png) [@MeltingPenguins](https://forum.choiceofgames.com/u/MeltingPenguins)\
**Post date:** [May 27, 2020, 9:44am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/5 "2020-05-27T09:44:31Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Szaal](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/szaal/32/112202_2.png) [@Szaal](https://forum.choiceofgames.com/u/Szaal)\
**Post date:** [May 27, 2020, 9:53am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/6 "2020-05-27T09:53:31Z")

</div>

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

> [@\*if - \*if versus \*if - \*else/\*elseif](https://forum.choiceofgames.com/t/if-if-versus-if-else-elseif/23434/):
>
> Yo Everyone I was tinkering a 'lil bit with my story, then found something that intrigues me, then go to [the wiki](http://choicescriptdev.wikia.com) But I found no much regarding \*if paired to another \*if commands Well, you see… conditional statement uses \*if paired to \*elseif/else for different condition 
> 
> > **Like this**
> >
> > \*set money 100 \*label repeat \*choice #Buy a PC \*if (money \>= 99) \*set money -99 \*goto repeat \*else You don't have enough money \*goto repeat #Buy a flashd…

---

<div class="post-metadata">

**Author:** ![MonkeyLottery](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/monkeylottery/32/138005_2.png) [@MonkeyLottery](https://forum.choiceofgames.com/u/MonkeyLottery)\
**Post date:** [May 27, 2020, 11:49am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/7 "2020-05-27T11:49:10Z")

</div>

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:

```choicescript
*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…

```choicescript
*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.](https://choicescriptdev.fandom.com/wiki/Implicit_Control_Flow)

---

<div class="post-metadata">

**Author:** ![MeltingPenguins](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/meltingpenguins/32/255855_2.png) [@MeltingPenguins](https://forum.choiceofgames.com/u/MeltingPenguins)\
**Post date:** [May 27, 2020, 12:01pm UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/8 "2020-05-27T12:01:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Evan3Michaels](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/evan3michaels/32/171767_2.png) [@Evan3Michaels](https://forum.choiceofgames.com/u/Evan3Michaels)\
**Post date:** [May 27, 2020, 9:54pm UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/9 "2020-05-27T21:54:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MeltingPenguins](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/meltingpenguins/32/255855_2.png) [@MeltingPenguins](https://forum.choiceofgames.com/u/MeltingPenguins)\
**Post date:** [May 27, 2020, 11:57pm UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/10 "2020-05-27T23:57:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoldenSilver](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/goldensilver/32/110153_2.png) [@GoldenSilver](https://forum.choiceofgames.com/u/GoldenSilver)\
**Post date:** [May 28, 2020, 4:42am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/11 "2020-05-28T04:42:02Z")

</div>

> [@MonkeyLottery](#):
>
> It will display:
> 
> > var2 is false

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.

```choicescript
*temp var1 false 
*temp var2 false
*temp var3 true
*temp var4 true
*temp var5 true
*temp var6 false

```

```choicescript
*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:

```choicescript
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:

```choicescript
*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:

```choicescript
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:

```choicescript
*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:

```choicescript
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.

---

<div class="post-metadata">

**Author:** ![MonkeyLottery](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/monkeylottery/32/138005_2.png) [@MonkeyLottery](https://forum.choiceofgames.com/u/MonkeyLottery)\
**Post date:** [May 28, 2020, 5:00am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/12 "2020-05-28T05:00:23Z")

</div>

Oh! My mistake! Thank you for correcting me.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/choiceofgames/original/3X/e/f/efe1497a762edecdb6d569a1c4abee87508bf002.png) [@system](https://forum.choiceofgames.com/u/system)\
**Post date:** [May 29, 2020, 5:00am UTC](https://forum.choiceofgames.com/t/if-elseif-and-else/79959/13 "2020-05-29T05:00:28Z")

</div>

Closed on May 29, 2020, 5:00am UTC

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