PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java For Loop

← All stacks

Theory

18/270

Java For Loop

for is the loop you use when you already know how many times to repeat. Shape: for (start; condition; update) { body }. Example: for (int i = 1; i <= 5; i++) print i.

Start runs once. Condition is checked before each round. Update runs after each body — usually i++. Trace i on paper: 1, 2, 3, 4, 5. Then code.

Classic work: print 1 to n, sum numbers, walk an array by index. The usual bug is off-by-one: i < n vs i <= n. Say the last value of i before you type.

Let's take this on the board with one tiny Main class — no extra files. Java For Loop — output: 1 then 2 then 3 then 4 then 5 — each on a new line. Start from main, go line by line, and stop at each print. That output is the proof for For Loop.

Diagram
i = 1
    │ i <= 5 ?
    ▼ yes
  body → i++ → check again
    │ no
    ▼
   stop
Exam tip

Trace i = 1 to 5 out loud, then write the loop.

Example

// Java For Loop
public class Main {
  public static void main(String[] args) {
    for (int i = 1; i <= 5; i++) {
      System.out.println(i);
    }
  }
}

Java For Loop — output: 1 then 2 then 3 then 4 then 5 — each on a new line.

Short notes

  • Deffor repeats when you know the count.
  • Rulefor (init; test; update). Init once, then test → body → update.
  • RememberTrace i on paper before coding.
  • Trapi < n vs i <= n (off-by-one).

Questions

1

When is for the best loop?

2

What are the three parts of for?

3

When does update run?

4

What is off-by-one?

Previous← Java Switch StatementNextFor-Each Loop in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.