If-Then and "End"

S

Sam C.

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?
 
C

Chris Shea

Sam said:
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?

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
 
G

gwtmp01

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?

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 Wright
 
W

Wang Dong

Sam C. said:
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?

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

Robert Klemme

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

Alternatively

exit if uinput == "EXIT"
....

robert
 
B

Ben Giddings

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,757
Latest member
PDIJaclyn

Latest Threads

Top