Need Help with Switch Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thatos
    New Member
    • Aug 2007
    • 105

    #1

    Need Help with Switch Statement

    I had trouble answering the questions, I'm a beginner.
    Switch Statement
    a)Explain the basic syntax and sematics of the switch statement?
    b)Can we select on a range of values using a sitch?
    c)Compare and contrast
    Code:
    if
    and
    Code:
    switch
    ?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      read any book about java and you will understand it.

      good luck
      hsn

      Comment

      • rashmiharitas
        New Member
        • Apr 2008
        • 6

        #4
        Originally posted by thatos
        I had trouble answering the questions, I'm a beginner.
        Switch Statement
        a)Explain the basic syntax and sematics of the switch statement?
        b)Can we select on a range of values using a sitch?
        c)Compare and contrast
        Code:
        if
        and
        Code:
        switch
        ?

        hello beginner

        the basic idea and good programming practice to avoid lot of if else statements which becomes tedious
        to overcome this tedious job we use switch statement

        syntax is

        int choice;
        switch (choice)
        {
        case 1:
        case 2:
        :
        :
        case 10 :so on
        }
        choice can be of byte,short,int data type

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          Originally posted by rashmiharitas
          hello beginner

          the basic idea and good programming practice to avoid lot of if else statements which becomes tedious
          to overcome this tedious job we use switch statement

          syntax is

          int choice;
          switch (choice)
          {
          case 1:
          case 2:
          :
          :
          case 10 :so on
          }
          choice can be of byte,short,int data type
          don't forget after each case there should be a break
          [CODE]
          switch(choic)
          {
          case 1:
          //some code
          break;

          case 2:
          //some code
          break;

          case 3:
          //some code
          break;

          //and so on............. .
          }
          [/CODE/

          GOOD LUCK

          hsn

          Comment

          • hsn
            New Member
            • Sep 2007
            • 237

            #6
            Originally posted by rashmiharitas
            hello beginner

            the basic idea and good programming practice to avoid lot of if else statements which becomes tedious
            to overcome this tedious job we use switch statement

            syntax is

            int choice;
            switch (choice)
            {
            case 1:
            case 2:
            :
            :
            case 10 :so on
            }
            choice can be of byte,short,int data type
            don't forget after each case there should be a break
            Code:
            switch(choic)
            {
            case 1: 
            //some code
            break;
            
            case 2: 
            //some code
            break;
            
            case 3: 
            //some code
            break;
            
            //and so on..............
            }
            GOOD LUCK

            hsn

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

              #7
              Originally posted by hsn
              don't forget after each case there should be a break

              hsn
              Only if you don't want to run into the next statement. In some programs it makes sense not to use a break.

              But what you should not forget for sure (and what beginners always forget), is the "default"-statement at the end.
              Forgetting this is the root of all evil and leads to endless debugging sessions.

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Er, all this was discussed in the tutorial linked in the first reply :-|

                Comment

                • thatos
                  New Member
                  • Aug 2007
                  • 105

                  #9
                  Thanks for your answer you realy helped
                  Originally posted by rashmiharitas
                  hello beginner

                  the basic idea and good programming practice to avoid lot of if else statements which becomes tedious
                  to overcome this tedious job we use switch statement

                  syntax is

                  int choice;
                  switch (choice)
                  {
                  case 1:
                  case 2:
                  :
                  :
                  case 10 :so on
                  }
                  choice can be of byte,short,int data type

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by BigDaddyLH
                    Er, all this was discussed in the tutorial linked in the first reply :-|
                    Are you implying that we should actually *read* replies? Tssk ...

                    kind regards,

                    Jos ;-)

                    Comment

                    • evoloyeu
                      New Member
                      • Mar 2008
                      • 3

                      #11
                      Originally posted by rashmiharitas
                      hello beginner

                      the basic idea and good programming practice to avoid lot of if else statements which becomes tedious
                      to overcome this tedious job we use switch statement

                      syntax is

                      int choice;
                      switch (choice)
                      {
                      case 1:
                      case 2:
                      :
                      :
                      case 10 :so on
                      }
                      choice can be of byte,short,int data type
                      choice must be int or char.

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by evoloyeu
                        choice must be int or char.
                        From the JLS:

                        Originally posted by JLS
                        The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (§8.9), or a compile-time error occurs.
                        kind regards,

                        Jos

                        Comment

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by JosAH
                          From the JLS:
                          Originally posted by JLS
                          The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type (§8.9), or a compile-time error occurs.
                          Jos
                          kind regards,

                          Jos
                          Er, this was discussed in the tutorial .... oh never mind ;-)

                          Comment

                          Working...