Syntax of for each loop n it's application
Answers
Answered by
1
Here's an example to iterate through elements of an array using standard for loop:
class ForLoop {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (int i = 0; i < vowels.length; ++ i) {
System.out.println(vowels[i]);
}
}
}
You can perform the same task using for-each loop as follows:
class AssignmentOperator {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
// foreach loop
for (char item: vowels) {
System.out.println(item);
}
}
}
The output of both programs will be same:
a
e
i
o
u
Similar questions