Enhanced For Loop

Java Conditional Statements (if statement, switch statement, else if, nested if, positive conditions and negative conditions), Java Loop Statements (for loop, while loop, do while loop and enhanced for loop) and Java Branching Statements (break,continue and return).
Post Reply
Gowtham Ramesh
Posts: 66
Joined: Thu Nov 22, 2018 5:23 am

Enhanced For Loop

Post by Gowtham Ramesh » Tue Nov 27, 2018 11:26 am

What is the use of Java Enhanced "For Loop"?

Sridhar G
Posts: 40
Joined: Fri Nov 30, 2018 3:19 am

Re: Enhanced For Loop

Post by Sridhar G » Sat Dec 08, 2018 1:19 pm

Enhanced for Loop in Java Programming:

1) Description:

Enhanced for Loop executes all elements in an Array
(We use varaibles for Java loops like "for loop", "while loop" and "do while loop", but Arrays can be used for enhanced for loops)

2) Syntax:

Array Declaration;

for (declaration: Array){
Statements
————-
}

3) Example:

String [] languages = {“C”, “COBOL”, “Java”, “VBScript”};

for (String lang: languages){
System.out.println(lang);
}

4) Example 2:

public static void main(String[] args) {
int a=10, b=20;

int [] mathOperations = new int [3];
mathOperations[0] = a+b;
mathOperations[1] = a-b;
mathOperations[2] = a*b;

for (int operation: mathOperations){
System.out.println(operation);
}

srini
Posts: 76
Joined: Thu Jan 03, 2019 3:58 am

Re: Enhanced For Loop

Post by srini » Thu Jan 10, 2019 4:35 am

class EnhancedForLoop {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Python", "Ruby"};

for (String sample: languages) {
System.out.println(sample);
}
}
}

Post Reply

Return to “Java Control Flow”