Java - for loop Expression List

Introduction

The expression-list part is optional.

It may contain one or more expressions separated by a comma.

The following code shows how to print all integers between 1 and 10 as follows:

The for-loop statement uses two expressions in the expression-list, which are separated by a comma.

for(int num = 1; num <= 10; System.out.println(num), num++);

Demo

public class Main {
  public static void main(String[] args) {
    for(int num = 1; num <= 10; System.out.println(num), num++);

    //rewrite the above for-loop statement to make it more compact and accomplish the same task:

    for(int num = 1; num <= 10; System.out.println(num++));

  }//ww w  .  j  av  a2  s .  com
}

Result

Related Topic