# If-Then and "End"

**URL:** <https://www.ruby-forum.com/t/if-then-and-end/83031>\
**Category:** Ruby\
**Created:** [January 23, 2007, 5:32am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031 "2007-01-23T05:32:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 5:32am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/1 "2007-01-23T05:32:22Z")

</div>

I am making a program with various commands. One of them is “Exit” which  
is supposed to exit the program. Here is my code:

if uinput = “EXIT” then  
end  
end

The problem is that the ruby compiler thinks that the first “end” is the  
end to the If-Then statement, instead of the “end” that ends the  
program. How do I work around this?

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 5:40am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/2 "2007-01-23T05:40:48Z")

</div>

Sam C. wrote:

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

if uinput = “EXIT” then  
exit  
end

That should work. Each ‘end’ is matched with the closest previous  
unmatched block opener (‘if’ in this case). It doesn’t have anything to  
do with terminating a script. hth

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 5:45am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/3 "2007-01-23T05:45:49Z")

</div>

On Jan 22, 2007, at 11:32 PM, Sam C. wrote:

> end to the If-Then statement, instead of the “end” that ends the  
> program. How do I work around this?

I think you are looking for ‘exit’ as the method to call to terminate  
a program.  
Also you aren’t doing a comparison in your if condition, you are  
doing an assignment.  
So you want something like:

if uinput == “EXIT” then  
exit  
else

# do something else

end

Gary W.

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 5:55am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/4 "2007-01-23T05:55:50Z")

</div>

“Sam C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Ð´ÈëÏûÏ¢ÐÂÎÅ:removed\_email\_address@domain.invalid…

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

It’s not the Pascal£¬program end with an ‘End.’, just use exit.

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 10:48am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/5 "2007-01-23T10:48:32Z")

</div>

removed\_email\_address@domain.invalid writes:

> you are doing an assignment. So you want something like:
> 
> if uinput == “EXIT” then  
> exit  
> else
> 
> # do something else
> 
> end

Just a small point: you don’t need an ‘else’ clause after an ‘exit’,  
since nothing could follow that statement. Hence, you can just do this  
if you want, and it will have the same effect:

if uinput == “EXIT” then  
exit  
end

# do something else

I tend to do that with ‘exit’, ‘return’, ‘raise’, ‘throw’, ‘next’,  
‘redo’, and ‘break’ statements, to avoid excessive nesting. But that’s  
just a stylistic thing.

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/6 "2007-01-23T22:16:28Z")

</div>

On Jan 23, 2007, at 04:47, Lloyd Z. wrote:

> I tend to do that with ‘exit’, ‘return’, ‘raise’, ‘throw’, ‘next’,  
> ‘redo’, and ‘break’ statements, to avoid excessive nesting. But  
> that’s  
> just a stylistic thing.

For that matter, you don’t need “then” unless it’s all on one line:

if uinput == “EXIT” then exit end

exit if uinput == “EXIT”

or

if uinput == “EXIT”  
exit  
end

For safety reasons, you could even do:

if “EXIT” == uinput  
exit  
end

this way if you accidentally use “=” instead of “==” you’ll get an  
error:

Ruby will give you a warning if it spots a “=” in a conditional, but  
often you want an error instead.

Ben

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 9:01am UTC](https://www.ruby-forum.com/t/if-then-and-end/83031/7 "2007-01-23T09:01:14Z")

</div>

On 23.01.2007 05:45, removed\_email\_address@domain.invalid wrote:

> > The problem is that the ruby compiler thinks that the first “end” is the  
> > exit  
> > else
> 
> # do something else
> 
> end

Alternatively

exit if uinput == “EXIT”  
…

robert
