Commas are used to separate multiple statements within the initialization and iteration parts of a forstatement.
In Core Java, commas are used in the initialization and iteration parts of a for statement to allow multiple expressions. The syntax of a for statement in Java is as follows:
java
for (initialization; condition; iteration) {
// body of the loop
}
Here, commas can be used in the initialization and iteration parts to include multiple expressions. For example:
java
for (int i = 0, j = 10; i < 5; i++, j--) {
// body of the loop
}
In this example, there are multiple expressions separated by commas in both the initialization (int i = 0, j = 10
) and iteration (i++, j--
) parts of the for statement. Each expression is executed in order, allowing for more complex loop control logic.