Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Junky

Thread: using logical OR in switch

  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default using logical OR in switch

    Hi,

    I've been trying to use "||" in switch, but it won't let me.
    I'm pretty sure there must be a way, coz if i want the same result for e or E, it's just weird to copy paste everything again in a new case.

    here's a part of my program:

    switch (user_choice.charAt(0)) {
    case 'a' : JOptionPane.showMessageDialog(null ,num1 + " + " + num2 + " = " + (num1+num2));
    break;

    case 'A' : JOptionPane.showMessageDialog(null ,num1 + " + " + num2 + " = " + (num1+num2));
    break;

    case 'm' : JOptionPane.showMessageDialog(null ,num1 + " * " + num2 + " = " + (num1*num2));
    break;

    case 'M' : JOptionPane.showMessageDialog(null ,num1 + " * " + num2 + " = " + (num1*num2));
    break;

    default : JOptionPane.showMessageDialog (null,"Unknown command, sorry!\n");


    if there is a way.. i'm dying to know :|

    TNX!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,037
    Rep Power
    10

    Default Re: using logical OR in switch

    Just let the case fall through:

    case 'A':
    case 'a':
    //whatever
    break;

    This is covered in the basic switch tutorial, which is the first hit for googling "java switch": The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: using logical OR in switch

    You can label more than one case label to a sequence of statements:

    [code]

    Java Code:
    switch (user_choice.charAt(0)) {
    case 'A':
    case 'a': JOptionPane.showMessageDialog(null ,num1 + " + " + num2 + " = " + (num1+num2));
    	break;
    
    case 'M':
    case 'm': JOptionPane.showMessageDialog(null ,num1 + " * " + num2 + " = " + (num1*num2));
    	break;
    // etc.
    kind regards,

    Jos

    edit: I'm the slowest old sod again ...
    Dark likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: using logical OR in switch

    Oh,
    Great idea!

    It's funny he hasn't showed us that in class... well.. that's academy.. u should learn 90% of the stuff urself.................. (or in forums xD)

    Thank you both!!

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: using logical OR in switch

    Quote Originally Posted by Mapisto View Post
    u should learn 90% of the stuff urself..................
    Unlike spelling and grammar which is taught in school.
    KevinWorkman likes this.

Similar Threads

  1. Logical problem in aplication
    By lostmind in forum Advanced Java
    Replies: 4
    Last Post: 12-24-2010, 11:21 PM
  2. if block with logical or
    By Ranu in forum New To Java
    Replies: 6
    Last Post: 07-01-2010, 09:11 AM
  3. Some Logical problem
    By MuslimCoder in forum New To Java
    Replies: 4
    Last Post: 03-01-2010, 09:12 AM
  4. Logical Gates
    By lingz89 in forum New To Java
    Replies: 1
    Last Post: 08-17-2009, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •