Switch case with break statement

Can anyone help me to write the following codes in ruby?

Switch(var)
{
case 1: statmnt1;
break;
case 2: statmnt2;
break;
:
:
default:statmnt3;
break;
}

there, break is not used in ruby at this place

ruby:

case var
when 1
statmnt1
when 2
statmnt2
else
statmnt3
end

and there are also short forms

C:

Switch(var)
{
case 1:
case 2: statmnt12;
break;

}

Ruby:

case var
when 1,2
statmnt12
end

Thanks Hans M. for your reply.

I tried using case, the real problem was. If iam giving user a list of
items
for ex:
puts " press 1 for adding
press 2 for subtraction
press 3 for multiplication
press 4 to exit"
var=STDIN.gets
case var
when 1
sum=a+b
when 2
sub=a-b
when 3
pr=a*b
when 4
exit
else “wrong entry”
end

after completing one task the user should get the list of items again to
do one more task. which is possible using break in C

hi Manju,

var=STDIN.gets
case var
when 1

there’s a little problem here - #gets returns a String, not an
Integer, so your case conditions will never be met… you’ll have to
convert the input to integers, or the conditions to strings for this to
work.

after completing one task the user should get the list of items again to
do one more task.

you could define the whole thing in a method, and recall that method
unless “4” is returned from #gets.

i’m sure there are better ways of doing it, but here’s what i came up
with while having my morning coffee:

#######

def do_math_stuff

puts “enter two numbers separated by a comma:”

nums = gets.chomp!.split(",")
a = nums[0].to_i
b = nums[1].to_i

puts “\tpress 1 for adding
press 2 for subtraction
press 3 for multiplication
press 4 to exit”

var = gets.chomp!.to_i

case var
when 1
result = a + b
when 2
result = a - b
when 3
result = a * b
else
result = “wrong entry” unless var == 4
end

unless var == 4
puts “result => #{result}”
puts
do_math_stuff
else
puts “bye!”
end

end

do_math_stuff

#######

hth,

  • j

Thank you all for your valuable reply,

One solution what I applied already was

def add()
a=5
b=6
return a+b
end

def sub()
a=5
b=6
return a-b
end

def pro()
a=5
b=6
return a*b
end

loop do

printf "Enter \n
1-Addition\n
2-Subtraction\n
3-Multiplication\n
4-Exit\n : "

a=STDIN.gets
b=a.to_i

case b

when 1
  add()

when 2
  sub()

when 3
  pro()

when 4
  exit

else
  puts "Wrong Entry"

end
end

hi again,

you could define the whole thing in a method, and recall that method
unless “4” is returned from #gets.

you could also skip the method, and just put the whole thing in a
while loop…

#######

var = 0
while var != 4

puts “enter two numbers separated by a comma:”

nums = gets.chomp!.split(",")
a = nums[0].to_i
b = nums[1].to_i

puts “\tpress 1 for adding
press 2 for subtraction
press 3 for multiplication
press 4 to exit”

var = gets.chomp!.to_i

case var
when 1
result = a + b
when 2
result = a - b
when 3
result = a * b
else
result = “wrong entry” unless var == 4
end

unless var == 4
puts “result => #{result}”
puts
else
puts “bye!”
end

end

#######

  • j