Using an Enumeration with a for Statement

In this lesson, we will look at a variation of the for statement known as a for-each statement and see how to use an enumerated data type with it.

We'll cover the following

The for-each statement

Suppose that we define an enumeration such as

private enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}

If we want to repeat statements for each object in an enumeration, we can use a for statement whose form differs from the ones we have just seen. For example, the following loop in lines 7 and 8 displays the values in the enumeration Suit:

Get hands-on with 1200+ tech skills courses.