Interviews - C# Corner

Vijay Kumari
Describe the Switch Statement.
By Vijay Kumari in Java onSep 17 2019
  • Rohit Gupta
    Sep, 2019 17

    Syntax

    1. Switch(variable)
    2. {
    3. case 0:
    4. .
    5. .
    6. .
    7. case n-1:
    8. default:
    9. }

    This is a type of control statement in which we are a single variable is checked for multiple values and if neither value matches the code corresponding to default is executed.

    • Switch case does not allow variables to be taken as the case value.
    • Only uniques case values are acceptable.
    • Each case is suffixed by a break statement, to make sure that only the matched case code executes
    1. import static java.lang.System.out;
    2. class Csharpcorner
    3. {
    4. public static void main(String[] args)
    5. {
    6. int i=0;
    7. switch(i)
    8. {
    9. case 0:
    10. case 1:
    11. case 2:
    12. case 3:
    13. case 4:
    14. case 5:
    15. case 6:out.print(i);
    16. break;
    17. default: out.print("No value");
    18. }
    19. }
    20. }

    For a detailed tutorial on Java Control and Loop Statements please visit https://www.c-sharpcorner.com/article/java-loops-and-control-statements/

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS