• We just launched and are currently in beta. Join us as we build and grow the community.

Using switch Structures

ushowardlink

Traffic Strategist
U Rep
0
0
0
Rep
0
U Vouches
0
0
0
Vouches
0
Posts
144
Likes
42
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
The following is a Java Program using switch structures. In Java, switch, case, break and default are reserved words. In a switch structure, the expression is evaluated first. The value of expression is then used to perform the actions specified in the statements that follow the reserved word case. I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code:

  1. import

    java.util.*

    ;
  2. public

    class

    BreakStatementInSwitch
  3. {
  4. static

    Scanner console =

    new

    Scanner(

    System

    .in

    )

    ;

  5. public

    static

    void

    main (

    String

    [

    ]

    args)
  6. {
  7. int

    num;

  8. System

    .out

    .println

    (

    "Enter an integer between 0 and 10: "

    )

    ;

  9. num =

    console.nextInt

    (

    )

    ;
  10. System

    .out

    .println

    (

    )

    ;

  11. System

    .out

    .println

    (

    "The number you enter is "

    +

    num)

    ;

  12. switch

    (

    num)
  13. {
  14. case

    0

    :
  15. case

    1

    :

    System

    .out

    .print

    (

    "Hello "

    )

    ;
  16. case

    2

    :

    System

    .out

    .print

    (

    "there "

    )

    ;
  17. case

    3

    :

    System

    .out

    .print

    (

    "I am "

    )

    ;
  18. case

    4

    :

    System

    .out

    .println

    (

    "Dodgers "

    )

    ;
  19. break

    ;
  20. case

    5

    :

    System

    .out

    .print

    (

    "How "

    )

    ;
  21. case

    6

    :
  22. case

    7

    :
  23. case

    8

    :

    System

    .out

    .print

    (

    "are you "

    )

    ;
  24. break

    ;
  25. case

    9

    :

    break

    ;
  26. case

    10

    :

    System

    .out

    .println

    (

    "Have a nice day!"

    )

    ;
  27. break

    ;
  28. default

    :

    System

    .out

    .println

    (

    "Sorry the number is out of range!"

    )

    ;


  29. }
  30. System

    .out

    .println

    (

    "Out of the switch structure!"

    )

    ;

  31. }
  32. }

Sample Run 1:
Enter an integer between 0 and 10:
1
The number you enter is 1
Hello there I am Dodgers
Out of the switch structure!

Sample run 2:
Enter an integer between 0 and 10:
3

The number you enter is 3
I am Dodgers
Out of the switch structure!

Sample run 3:

Enter an integer between 0 and 10:
4

The number you enter is 4
Dodgers
Out of the switch structure!

Sample run 4:

Enter an integer between 0 and 10:
5

The number you enter is 5
How are you Out of the switch structure!

Sample run 5:

Enter an integer between 0 and 10:
7

The number you enter is 7
are you Out of the switch structure!

Sample run 6:

Enter an integer between 0 and 10:
9

The number you enter is 9
Out of the switch structure!

Sample run 7:

Enter an integer between 0 and 10:
10

The number you enter is 10
Have a nice day!
Out of the switch structure!

Sample run 8:

Enter an integer between 0 and 10:
11

The number you enter is 11
Sorry the number is out of range!
Out of the switch structure!

A walk through of this program, using certain values of the switch expression, num

, can help you understand how the break

statement functions.

If the value of num =

0

, the value of the switch expression matches the case value .

The first break appears break

;

, just before the case value of 5.

When the value of the switch statement expression matches a case value, all statements execute until a break is encountered, and the program skips all case labels in between.

If the value of num is 3, it matches the case value of 3 and the statements following this label execute until the break statement is encountered in break

;

.

If the value of num is 9, it matches the case value of 9.

In this situation, the action is empty, because only break statement case

9

:

break

;

, follows the case value of 9.

 

452,292

323,666

323,675

Top