# How do I code this conditional statement in Ruby

**URL:** <https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994>\
**Category:** Rails\
**Created:** [July 4, 2006, 1:47am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994 "2006-07-04T01:47:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 4, 2006, 1:47am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/1 "2006-07-04T01:47:48Z")

</div>

Hi, I am a COBOL programmer and I am busy teaching myself Rails and  
Ruby.

In COBOL I can code this conditional

If x = 1  
next sentence  
else  
…

The “next sentence” statement enables me to get out of the  
conditional. How would I code the same thing in Ruby? In C you could  
use break but I understand that Ruby has no break statement.

Regards,

Paul

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 4, 2006, 1:57am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/2 "2006-07-04T01:57:06Z")

</div>

On 7/3/06, Paul Jonathan T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> conditional. How would I code the same thing in Ruby? In C you could  
> use break but I understand that Ruby has no break statement.

Can you give a better example of what you’re really trying to do? Why  
would you ever want to break out of a conditional? Just don’t put  
code in there that you don’t want executed.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 4, 2006, 2:09am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/3 "2006-07-04T02:09:43Z")

</div>

James L. wrote:

> > 
> 
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Ruby does too have ‘break’. I’m assuming your in a loop here, as you  
mentioned doing the ‘same thing’ in C. It works just as well in Ruby:

loop.construct do  
if x == 1  
break  
end  
end

will kick you out of the loop. If you really mean just jumping out of  
the conditional, you should then rewrite the conditional to be the  
opposite, aka:

if x != 1  
do the other stuff  
end

Jason

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 4, 2006, 6:08am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/4 "2006-07-04T06:08:04Z")

</div>

Hello, It looks like what you want is something that executes unless  
x==1. You could do if x != 1 or the more popular (and more positive)  
unless x==1.

Chris  
Paul Jonathan T. wrote:

> Hi, I am a COBOL programmer and I am busy teaching myself Rails and  
> Ruby.
> 
> In COBOL I can code this conditional
> 
> If x = 1  
> next sentence  
> else  
> …
> 
> The “next sentence” statement enables me to get out of the  
> conditional. How would I code the same thing in Ruby? In C you could  
> use break but I understand that Ruby has no break statement.
> 
> Regards,
> 
> Paul

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 4, 2006, 3:26am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/5 "2006-07-04T03:26:20Z")

</div>

If I remember my COBOL right, can’t you execute a number of instructions  
up  
to a period and call that a sentence? You’ll find most languages  
designed  
after the '70s (exception: Basic) had more clearly delineated blocks  
associated with conditionals instead of the concept of sentences.

The paradigm is:

if a  
do something  
else  
do something else  
elseif x== 1  
do something with x  
end

I think the elseif may be what you are looking for. Consider using the  
case  
statement instead:

case x  
when 1 then puts “a partridge in a pear tree”  
when 2 then puts “two turteldoves…”  
else puts “that’s all, folks”  
end

## These constructs take you away from the “goto” flow of control that earlier procedural languages relied so heavily on.

View this message in context:  
[http://www.nabble.com/How-do-I-code-this-conditional-statement-in-Ruby-tf1887308.html#a5160746](http://www.nabble.com/How-do-I-code-this-conditional-statement-in-Ruby-tf1887308.html#a5160746)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [July 5, 2006, 1:49am UTC](https://www.ruby-forum.com/t/how-do-i-code-this-conditional-statement-in-ruby/96994/6 "2006-07-05T01:49:29Z")

</div>

Hi,

Thank you to all who replied, problem sorted out now. When you are  
used to a procedural language, OOP takes a bit of getting used too and  
a fundamentally different approach.

Regards,

Paul
