Using for-each loop


\begin{lstlisting}
class Main {
public static void main(String[] args) {
\par
/...
...p
for (char item: vowels) {
System.out.println(item);
}
}
}
\end{lstlisting}

Ouput


\begin{lstlisting}
a
e
i
o
u
\end{lstlisting}

Here, the output of both programs is the same. However, when we analyze both the programs, we can see the for-each loop is easier to write. It also makes our code more readable. This is why for-each loop is preferred more than for loop in while working with arrays or collections.