Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Switch case with nested if inside

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch case with nested if inside

    Hi Good day.. can give me a sample program which solves with switch case statement with nested if inside?

    thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch case with nested if inside

    Can you post the code you are having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Switch case with nested if inside

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Switch case with nested if inside

    //package
    // imports
     
    // class
    {
       // main
       {
          // switch
          {
             // if statements
          }
       }
    }

    That's the base for what it should look like.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Switch case with nested if inside

    If i understood you correctly you wish to see an example of a switch being implemented in if-else methods.
    If you're trying to decide which is better, know that in practice, they function almost alike, even performance-wise.

    Public static void main(String[] args) {
     
    int _number;
     
    //... a command which will assign an integer into '_number' (_number = 2 for example)
     
        Switch (_number) {
            Case 0 : //What to do when _number == 0 ;
                        //Break;
            Case 1 : //What to do when _number == 1 ;
                        //Break;
            Case 2 : //What to do when _number == 2 ;
                        //Break;
            Default : //What to do when its none of the above
                        //Break;
        }
    }

    Public static void main(String[] args) {
     
    int _number;
     
    //... a command which will assign an integer into '_number' (_number = 2 for example)
     
    If (_number == 0) { //What to do when _number == 0 ;
    }  else if (_number == 1) { //What to do when _number == 1 ;
    }  else if (_number == 2) { //What to do when _number == 2 ;
    }  else { //What to do when its none of the above
    }

    You could ofcourse do it like

    If (_number == 0) { //What to do when _number == 0 ; }
    If (_number == 1) { //What to do when _number == 1 ; }
    If (_number == 2) { //What to do when _number == 2 ; }
    ...

    But that wouldn't be as efficient, as it continues checking and comparing, again and again, regardless to the outcome.

    a better explanation and examples can be found at java docs:
    The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Last edited by Stum; July 16th, 2014 at 04:46 AM.

Similar Threads

  1. Switch case problem
    By pavani in forum Loops & Control Statements
    Replies: 4
    Last Post: August 11th, 2013, 09:34 PM
  2. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  3. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  4. Switch Case Problem help
    By Kikiam in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 18th, 2012, 06:11 PM
  5. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM