Title: Java For Loop — i-- Termination Bug Kills Batch Jobs
Open Graph Title: Java For Loop — i-- Termination Bug Kills Batch Jobs
Description: CPU pinned at 100%? A for loop using i-- runs forever. See why this one-character bug crashes batch jobs and how to prevent it in production.
Open Graph Description: CPU pinned at 100%? A for loop using i-- runs forever. See why this one-character bug crashes batch jobs and how to prevent it in production.
Opengraph URL: https://thecodeforge.io/java/for-loop-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/for-loop-java/"
},
"headline": "Java For Loop — i-- Termination Bug Kills Batch Jobs",
"description": "CPU pinned at 100%? A for loop using i-- runs forever. See why this one-character bug crashes batch jobs and how to prevent it in production.",
"keywords": "Java For Loop — i-- Termination Bug Kills Batch Jobs, Java, Beginner, Control Flow, CPU pinned at 100%? A for loop using i-- runs forever. See why this one-character bug crashes batch jobs and how to prevent it in production.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-27T00:00:00+00:00",
"url": "https://thecodeforge.io/java/for-loop-java/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Java For Loop — i-- Termination Bug Kills Batch Jobs", "item": "https://thecodeforge.io/java/for-loop-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is a for loop in Java and when should I use it?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A for loop is a control flow statement that repeats a block of code a specific number of times, using a counter to track progress. Use it whenever you know in advance how many iterations you need — iterating through every element in an array, printing a times table, applying a transformation to N items, or repeating an action a fixed number of times.\n\nThe key signal is whether the iteration count is known before the loop starts. If it is, a for loop is the right tool. If you are waiting for something to happen — user input, a network response, a file reaching end-of-file — a while loop is a better fit because you do not know upfront how many iterations it will take."
}
},
{
"@type": "Question",
"name": "Why does my Java for loop skip the last element of the array?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The most common cause is `i < array.length - 1` instead of `i < array.length`. The expression `array.length - 1` stops one element too early — it excludes the last valid index deliberately. Use `i < array.length` and the loop will correctly visit every element from index 0 through index `length - 1`.\n\nIf the first element is being skipped instead, check whether your initializer starts at 1 instead of 0. Arrays are zero-indexed, so starting at 1 misses the element at index 0 silently — same type of bug, opposite end of the array."
}
},
{
"@type": "Question",
"name": "What happens if I leave the update part of a for loop empty?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Java allows you to omit any of the three parts of the for loop header. If you leave the update empty — like `for (int i = 0; i < 10;)` — the counter never changes automatically. The condition remains true on every check and the loop runs forever, consuming 100% of a CPU core until the process is killed.\n\nThe only valid reason to omit the update is if you are updating the counter manually inside the loop body using a `break`, a return, or an explicit `i++` at the bottom. Even then, most experienced developers prefer to keep the update in the header where it is visible and expected. Hiding counter updates inside the body is a maintenance hazard."
}
},
{
"@type": "Question",
"name": "Can I declare multiple variables in the initializer of a for loop?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, but all variables must share the same type. For example: `for (int i = 0, j = 10; i < j; i++, j--)` is valid — two int counters moving toward each other from opposite ends. You can also update multiple counters in the update step using the comma operator as shown.\n\nThat said, keep loop headers simple. Multiple variables in a for header work correctly but reduce readability — a reader has to parse both counters, both conditions, and both update steps simultaneously. If the logic is genuinely two-dimensional, a nested loop with separate counters is usually clearer. If you find yourself needing three or more variables in a single for header, that is a strong signal the logic belongs in the loop body or in a dedicated method."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large