Home Tutorials Jruby Using "Case" control in JRuby



Using "Case" control in JRuby
Posted on: September 30, 2008 at 12:00 AM
In the previous section of JRuby tutorial you have learned to use for-loop in JRuby.

Using "Case" control in JRuby 

     

In the previous section of JRuby tutorial you have learned to use for-loop in JRuby. Now we will discuss that how to use "Case" control in our program to provide working of program on selected case values.

This example program takes user's choice by the user to perform operations on numbers then it takes two numbers to perform operation on them. We have taken input values with the use of "gets". Sample syntax for "Case" control is as follows

 

 

Syntax of Case is as follows:

case choice
    choiceNo 1
  statements...
  ................
  choiceNo 2
   statements...
  ................
   choiceNo 3
   statements...
  ................ 

end

Here is the full example code of JRubyCaseExample.rb as follows:

JRubyCaseExample.rb

# Using case in JRuby

print "<=== Use of case in JRuby ===>\n"
print "******Select your choice*******\n"
  print " 1. Add \n"
  print " 2. Substract \n"
  print " 3. Multiply \n"
  print " 4. Divide \n"
 print "====> Choice :"
  choice = gets
   print "Enter First Number =: "
   n1=gets
   print "Enter Second Number =: "
   n2=gets
case choice.to_i
  when 1
  puts " Addition is =: #{n1.to_i + n2.to_i}"
   when 2
  puts " Subtraction is =: #{n1.to_i - n2.to_i}"
   when 3
  puts " Multiplication is =: #{n1.to_i * n2.to_i}"
   when 4
  puts " Division is =: #{n1.to_i / n2.to_i}"
end

To execute this JRuby example type jruby command with filename JRubyCaseExample.rb and you will get the following output:

Output:

On selecting choice 1(Addition) will be performed on entered two numbers:

C:\JRuby>jruby JRubyCaseExample.rb
<=== Use of case in JRuby ===>
******Select your choice*******
1. Add
2. Subtract
3. Multiply
4. Divide
====> Choice :1
Enter First Number =: 2
Enter Second Number =: 3
Addition is =: 5

When we will select 2( Subtraction), program will perform subtraction on both numbers and result will be shown as 

C:\JRuby>jruby JRubyCaseExample.rb
<=== Use of case in JRuby ===>
******Select your choice*******
1. Add
2. Substract
3. Multiply
4. Divide
====> Choice :2
Enter First Number =: 3
Enter Second Number =: 5
Substraction is =: -2

Download Source Code

     

Related Tags for Using "Case" control in JRuby:
crubyoopideselectcontroliofor-looploopjrubyvitutorialvalueriaidoocasecasforcteworkprogramvaluestolearnramselectedeareilsectionuseinnoasmnttrcajesworkingasehowprotorsdiscatkishallprediscusseaarvatutorssrithavaluhatpronogrolo


More Tutorials from this section

Ask Questions?    Discuss: Using "Case" control in JRuby  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.