More Tutorials| Bioinformatics| Open Source| Photoshop| Questions? | Software Development
 

Using "Case" control in JRuby

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

                         

» View all related tutorials
Related Tags: java c ruby class tutorials tree io classes jruby vi tutorial ria set example program to learn ram exam sse

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.