The Java syntax for a for loop is as follows:

for (initialization; condition; increment) {
    // Loop body code
}

Here's what each part means:

  • initialization: A statement executed at the start of the loop, typically used to initialize a counter or other control variable.
  • condition: A Boolean expression that determines whether the loop continues to execute. The loop body runs only if the condition evaluates to true.
  • increment: A statement executed after each iteration of the loop body, typically used to update the counter or control variable.

Example: Outputting Numbers 1 to 10

for (int i = 1; i <= 10; i++) {
    System.out.println(i);
}

This code snippet will print the numbers from 1 to 10, one per line.

Java For Loop: Syntax and Example

原文地址: https://www.cveoy.top/t/topic/n4Me 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录