Coding/Learn/java/for loops
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnStandard For Loops
19 / 31 0%
Lesson 19 of 31
6 mins read

Standard For Loops

PreviousNext Lesson
In Plain English

The standard `for` loop packages counter initialization, loop condition, and step update all into a single, elegant header. It is the gold standard when you know exactly how many times you need to repeat an action.

The Mental Model

"A for loop is like a lap counter on a running track with a set goal: "Start at lap 1, run until lap 5, adding 1 lap each time around.""

Deep Dive: How It Works

Structure: `for (initialization; condition; update) { ... }`

Step 1: Initialization runs once at the beginning (`int i = 0`).

Step 2: Condition is evaluated before each iteration (`i < 5`).

Step 3: Body statements execute.

Step 4: Update runs after each iteration (`i++`), then returns to Step 2.

Syntax Blueprint

for (int i = 0; i < 5; i++) {
    System.out.println("Index: " + i);
}

The loop variable i only exists inside the for loop scope.

PointCore Rules to Remember

Point star
Compact Syntax: Initialization, condition, and increment in one line.
Point star
Scoped Loop Variable: int i declared in the header is isolated to the loop.
Point star
Flexible Increments: Can step by any amount: i += 2, i--, etc.

Common Beginner Traps & How to Fix Them

Mistake:Using commas instead of semicolons in loop header: for (int i = 0, i < 5, i++)

Why it happens: Clauses in a for header must be separated by semicolons.

How to fix: Use semicolons: for (int i = 0; i < 5; i++).

Pro Tip: You can count backwards smoothly with for (int i = 10; i >= 1; i--).

Live Interactive Example

Hit Run Code to see it live
Counting and Accumulating with For Loops
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: The for loop runs exactly 5 times, adding i to totalSum in each pass.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Print even numbers up to 6

  • PointWrite a for loop that prints even numbers: 2, 4, 6.
  • PointStart at int i = 2; continue while i <= 6; increment by i += 2.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates