René's URL Explorer Experiment


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/

direct link

Domain: thecodeforge.io


Hey, it has json ld scripts:
{
  "@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:typewebsite
og:imagehttps://thecodeforge.io/assets/og-image.png
og:image:width1200
og:image:height630

Links:

TheCodeForge https://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Buy me a coffee https://ko-fi.com/thecodeforge
1if-else Statement in Javahttps://thecodeforge.io/java/if-else-java/
2switch Statement in Javahttps://thecodeforge.io/java/switch-statement-java/
3for Loop in Javahttps://thecodeforge.io/java/for-loop-java/
4while and do-while Loop in Javahttps://thecodeforge.io/java/while-do-while-java/
5break and continue in Javahttps://thecodeforge.io/java/break-continue-java/
6Nested Loops in Javahttps://thecodeforge.io/java/nested-loops-java/
7Enhanced for Loop in Javahttps://thecodeforge.io/java/enhanced-for-loop-java/
8Ternary Operator in Javahttps://thecodeforge.io/java/ternary-operator-java/
9Labeled break and continue in Javahttps://thecodeforge.io/java/labeled-break-continue-java/
← Back to Java track https://thecodeforge.io/java/
Homehttps://thecodeforge.io/
Javahttps://thecodeforge.io/java/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
 ● Production Incidenthttps://thecodeforge.io/java/for-loop-java/#v2-production-incident
arrayshttps://thecodeforge.io/java/arrays-java/
while loophttps://thecodeforge.io/python/while-loop-python/
recordshttps://thecodeforge.io/java/records-java16/
arrayhttps://thecodeforge.io/dsa/array-data-structure/
designhttps://thecodeforge.io/java/design-patterns-java/
nested loopshttps://thecodeforge.io/java/nested-loops-java/
HashMaphttps://thecodeforge.io/java/hashmap-java/
strategyhttps://thecodeforge.io/java/strategy-pattern-java/
control flowhttps://thecodeforge.io/c-cpp/control-flow-c/
queuehttps://thecodeforge.io/dsa/queue-data-structure/
Arrays in Javahttps://thecodeforge.io/java/arrays-java/
ArrayListhttps://thecodeforge.io/java/arraylist-java/
→Enhanced for Loop in Java Explained — Syntax, Use Cases and PitfallsEnhanced for loop in Java made simple. Learn when to use it, how it works under the hood, and the 3 mistakes that trip uhttps://thecodeforge.io/java/enhanced-for-loop-java/
→Nested Loops in Java — How 100M Comparisons Caused HTTP 504Two nested loops over 10,000 users each create 100 million comparisons, causing a 28-second timeout. Discover how flat dhttps://thecodeforge.io/java/nested-loops-java/
→break and continue in Java — The while Loop CPU TrapA continue before counter increment caused 100% CPU in production. See where break and continue fail in while loops andhttps://thecodeforge.io/java/break-continue-java/
→Java Arrays — Why <= Crashed 2.4 Million RecordsOne character difference: < vs <= in array loops. ArrayIndexOutOfBoundsException after 2.4M records triggered full rollbhttps://thecodeforge.io/java/arrays-java/
→Java While/Do-While — Forgotten Increment Breaks EverythingIn Java, do-while runs at least once; while may skip. A forgotten increment causes infinite loops. See the production buhttps://thecodeforge.io/java/while-do-while-java/
AES — Advanced Encryption StandardAdvancedhttps://thecodeforge.io/dsa/aes-encryption/
Follow https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
←Previousswitch Statement in Javahttps://thecodeforge.io/java/switch-statement-java/
Nextwhile and do-while Loop in Java→https://thecodeforge.io/java/while-do-while-java/
LinkedIn https://www.linkedin.com/in/thecodeforge-io-a0676b3b5/
Facebook https://www.facebook.com/profile.php?id=61583020033815
YouTube https://www.youtube.com/channel/UCMar7CF2EmCF1I2tw1tNVuQ
Javahttps://thecodeforge.io/java/
Pythonhttps://thecodeforge.io/python/
C / C++https://thecodeforge.io/c-cpp/
JavaScripthttps://thecodeforge.io/javascript/
DSAhttps://thecodeforge.io/dsa/
Databasehttps://thecodeforge.io/database/
System Designhttps://thecodeforge.io/system-design/
DevOpshttps://thecodeforge.io/devops/
C# / .NEThttps://thecodeforge.io/csharp/
PHPhttps://thecodeforge.io/php/
ML / AIhttps://thecodeforge.io/ml-ai/
Interviewhttps://thecodeforge.io/interview/
CS Fundamentalshttps://thecodeforge.io/cs-fundamentals/
Rusthttps://thecodeforge.io/rust/
Toolshttps://thecodeforge.io/tools/
Cheatsheetshttps://thecodeforge.io/cheatsheets/
Mobilehttps://thecodeforge.io/mobile/
Data Analyticshttps://thecodeforge.io/data-analytics/
Securityhttps://thecodeforge.io/security/
Frontendhttps://thecodeforge.io/frontend/
Data Engineeringhttps://thecodeforge.io/data-engineering/
Testinghttps://thecodeforge.io/testing/
Cloudhttps://thecodeforge.io/cloud/
Observabilityhttps://thecodeforge.io/observability/
Web Platformhttps://thecodeforge.io/web-platform/
🧯 Errors Explainedhttps://thecodeforge.io/errors/
⚒ Blueprintshttps://thecodeforge.io/blueprints/
⌨ Practicehttps://thecodeforge.io/practice/
Interview Prephttps://thecodeforge.io/interview/
DSA Problemshttps://thecodeforge.io/dsa/
Developer Toolshttps://thecodeforge.io/tools/
Cheat Sheetshttps://thecodeforge.io/cheatsheets/
Abouthttps://thecodeforge.io/about/
Sitemaphttps://thecodeforge.io/sitemap.xml
Privacyhttps://thecodeforge.io/privacy.html

Viewport: width=device-width, initial-scale=1.0

Robots: max-image-preview:large


URLs of crawlers that visited me.