# Nested IF statements

**URL:** <https://rubytalk.org/t/nested-if-statements/27363>\
**Category:** ruby-talk\
**Created:** [11 May 2006 22:05 UTC](https://rubytalk.org/t/nested-if-statements/27363 "2006-05-11T22:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angelo](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@Angelo](https://rubytalk.org/u/Angelo)\
**Post date:** [11 May 2006 22:05 UTC](https://rubytalk.org/t/nested-if-statements/27363/1 "2006-05-11T22:05:33Z")

</div>

I'm very new to Ruby and am desperately trying to figure out what seems  
to be a fairly simple script. Is there something special I need to know  
in regards to IF statements with Ruby that may differ from most other  
languages?

The structure of the script looks like the following:

for

&nbsp;&nbsp;&nbsp;&nbsp;if

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;elsif

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;if

&nbsp;&nbsp;&nbsp;&nbsp;end

end

It's bugging at the first and only elsif. (syntax error, unexpected  
kELSIF, expecting kEND)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Guest15](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guest15](https://rubytalk.org/u/Guest15)\
**Post date:** [11 May 2006 22:18 UTC](https://rubytalk.org/t/nested-if-statements/27363/2 "2006-05-11T22:18:30Z")

</div>

> I'm very new to Ruby and am desperately trying to figure out what seems  
> to be a fairly simple script. Is there something special I need to know  
> in regards to IF statements with Ruby that may differ from most other  
> languages?
> 
> The structure of the script looks like the following:
> 
> for
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;elsif

you need an else here

> **···**
>
> On 5/11/06, Angelo \<angelogladding@gmail.com\> wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > It's bugging at the first and only elsif. (syntax error, unexpected  
> > kELSIF, expecting kEND)
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> thanks,  
> -pate  
> -------------------------
> 
> > **[On Ruby](http://on-ruby.blogspot.com)**
> >
> > The (mostly) tech related musings of Pat Eyler. Ruby, Erlang, Haskell, Scala, Ocaml, Publishing, and more ...

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [12 May 2006 15:31 UTC](https://rubytalk.org/t/nested-if-statements/27363/3 "2006-05-12T15:31:49Z")

</div>

I'm guessing it's actually a problem with your elided (not shown)  
code. If I take you structure and add in some simple conditions and  
statements, it runs fine:

&nbsp;&nbsp;for i in (1..10).to\_a  
&nbsp;&nbsp;&nbsp;&nbsp;if i \> 5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i \< 8  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "i is 6 or 7"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "i is 8, 9 or 19"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;elsif i \< 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "i is 1"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;if (i % 2).zero?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{i} is even"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

produces:

&nbsp;&nbsp;i is 1  
&nbsp;&nbsp;2 is even  
&nbsp;&nbsp;4 is even  
&nbsp;&nbsp;i is 6 or 7  
&nbsp;&nbsp;6 is even  
&nbsp;&nbsp;i is 6 or 7  
&nbsp;&nbsp;i is 8, 9 or 19  
&nbsp;&nbsp;8 is even  
&nbsp;&nbsp;i is 8, 9 or 19  
&nbsp;&nbsp;10 is even

My guess is you have some other construct requiring an 'end' (while  
loop, block, etc.) that is missing its 'end' between the first 'end'  
(of the nested if) and the 'elsif' in question. Since you're still in  
the other construct, the parser is expecting to see that construct  
closed (with a kEND token) before seeing the kELSIF token of the  
enclosing if statement.

Jacob Fugal

> **···**
>
> On 5/11/06, Angelo \<angelogladding@gmail.com\> wrote:
> 
> > I'm very new to Ruby and am desperately trying to figure out what seems  
> > to be a fairly simple script. Is there something special I need to know  
> > in regards to IF statements with Ruby that may differ from most other  
> > languages?
> > 
> > The structure of the script looks like the following:
> > 
> > for
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;elsif
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > end
> > 
> > It's bugging at the first and only elsif. (syntax error, unexpected  
> > kELSIF, expecting kEND)
