[{"data":1,"prerenderedAt":253},["ShallowReactive",2],{"tag-labels":3,"site-header":4,"article-java-loops-en":72},{},{"featuredPost":5,"navigations":29},{"id":6,"title":7,"title_id":8,"slug":9,"excerpt":10,"excerpt_id":11,"has_translation":12,"published_at":13,"reading_time":14,"hidden_from_listing":15,"tags":16,"thumbnail_image":21,"og_image":22,"series":23,"authors":28},110,"When You Pick a Framework Just to Look Different","Ketika Pilih Framework Cuma Biar Kelihatan Beda","ketika-pilih-framework-cuma-biar-kelihatan-beda","The last time you chose a new framework or library, was it because your problem needed that tool, or because it sounded cool and few people around you were using it? That question is uncomfortable. Technology decisions are often made for reasons that never appear in any technical document.","Terakhir kali kamu milih framework atau library baru, itu karena masalahmu butuh alat itu, atau karena alatnya kedengaran keren dan belum banyak yang pakai? Pertanyaan ini nggak enak dijawab. Soalnya keputusan teknologi sering diambil dengan alasan yang nggak pernah ditulis di dokumen teknis mana pun.",true,"2026-10-06T15:25:21.000Z",5,false,[17,18,19,20],"karier","framework","opini","pengalaman",null,"\u002Fog\u002Fketika-pilih-framework-cuma-biar-kelihatan-beda.png",{"id":24,"title":25,"title_id":26,"slug":27},9,"Articles","Artikel","all",[],[30,46,67],{"collection":31,"item":32},"series",{"__typename":31,"title":25,"title_id":26,"slug":27,"children_series":33},[34,38,42],{"title":35,"title_id":36,"slug":37},"Artificial Intelligence","Kecerdasan Artifisial","kecerdasan-artifisial",{"title":39,"title_id":40,"slug":41},"Programming Fundamentals","Konsep Dasar Pemrograman","konsep-dasar-pemrograman",{"title":43,"title_id":44,"slug":45},"Algorithms and Data Structures","Algoritma dan Struktur Data","algoritma-struktur-data",{"collection":31,"item":47},{"__typename":31,"title":48,"title_id":48,"slug":49,"children_series":50},"Tutorial","tutorial",[51,55,59,63],{"title":52,"title_id":53,"slug":54},"Learning Python","Belajar Python","belajar-python",{"title":56,"title_id":57,"slug":58},"Learning Go","Belajar Golang","belajar-golang",{"title":60,"title_id":61,"slug":62},"Learning Java","Belajar Java","belajar-java",{"title":64,"title_id":65,"slug":66},"Learning Pascal","Belajar Pascal","belajar-pascal",{"collection":31,"item":68},{"__typename":31,"title":69,"title_id":69,"slug":70,"children_series":71},"Lab","lab",[],{"article":73,"renderedBody":145,"renderedTakeaway":146,"latestArticles":147,"toc":233},{"id":74,"title":75,"title_id":76,"slug":77,"excerpt":78,"excerpt_id":79,"has_translation":12,"published_at":80,"reading_time":81,"hidden_from_listing":15,"tags":82,"thumbnail_image":21,"og_image":85,"series":86,"authors":137,"seoTitle":21,"seoDescription":21,"body_locale":140,"available_locales":141,"body":143,"takeway":144,"prerequisite":21},150,"Java Loops: the Last Price That Never Reached the Sum","Loop Java: Harga Terakhir yang Tidak Pernah Masuk ke Total","java-loops","Four prices add up to 47000, and the cart program prints 36500. The bound is `prices.length - 1`, so the last item is never read while the average is still divided by four.","Empat harga berjumlah 47000, tapi program keranjang mencetak 36500. Batasnya `prices.length - 1`, jadi item terakhir tidak pernah dibaca sementara rata-ratanya tetap dibagi empat.","2026-11-13T01:00:00.000Z",6,[83,84],"java","loop","\u002Fog\u002Fjava-loops.png",{"id":87,"title":60,"title_id":61,"slug":62,"description":88,"description_id":89,"articles":90,"children_series":115,"parent_series":128},13,"Java is one of the most used languages in the world. It earned that with how it handles large applications and how stable it stays over the years. You'll find it in web apps, mobile apps, and enterprise systems alike. Start here and take it one step at a time.","Java adalah bahasa pemrograman yang sangat populer dan banyak digunakan di seluruh dunia. Dikenal karena kekuatannya dalam pengembangan aplikasi besar dan stabilitasnya, Java dipakai untuk berbagai macam aplikasi seperti aplikasi web, mobile, dan enterprise. Yuk mulai belajar Java bareng Inva!",[91,110],{"id":92,"title":93,"title_id":94,"slug":95,"excerpt":96,"excerpt_id":97,"has_translation":12,"published_at":98,"reading_time":81,"hidden_from_listing":15,"tags":99,"thumbnail_image":21,"og_image":101,"series":102,"authors":103},149,"Java switch: the 78 That Came Out as a C","Java switch: 78 yang Keluar sebagai C","java-switch","A grading program marks a paper 78 and prints `grade: C`. `case 7` assigns `B` and then runs into `case 6` because the `break` is missing, and the compiler says nothing about it.","Sebuah program penilaian memberi nilai 78 pada satu lembar ujian lalu mencetak `grade: C`. `case 7` memberi `B` lalu jatuh ke `case 6` karena `break`-nya hilang, dan kompilator tidak mengatakan apa pun soal itu.","2026-11-12T01:00:00.000Z",[83,100],"percabangan","\u002Fog\u002Fjava-switch.png",{"id":87,"title":60,"title_id":61,"slug":62},[104],{"authors_id":105},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},1,"Daffa Izzuddin","izzudd","Writer",{"id":74,"title":75,"title_id":76,"slug":77,"excerpt":78,"excerpt_id":79,"has_translation":12,"published_at":80,"reading_time":81,"hidden_from_listing":15,"tags":82,"thumbnail_image":21,"og_image":85,"series":111,"authors":112},{"id":87,"title":60,"title_id":61,"slug":62},[113],{"authors_id":114},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},[116,122],{"id":117,"title":118,"title_id":119,"slug":120,"description":121,"cover_image":21},21,"Getting Started with Java","Berkenalan dengan Java","java-intro","Meet Java from scratch: your first program, how the pieces fit together, and how to read input and print output.",{"id":123,"title":124,"title_id":125,"slug":126,"description":127,"cover_image":21},22,"Variables, Types, and Operations","Variabel, Tipe Data, dan Operasi","java-variabel-tipe-data","In Java the type has to be clear from the start. We go through the primitive types, then integers and floats in more depth.",{"id":106,"title":48,"title_id":48,"slug":49,"parent_series":21,"children_series":129},[130,132,134,135],{"id":131,"title":52,"title_id":53,"slug":54,"cover_image":21},10,{"id":133,"title":56,"title_id":57,"slug":58,"cover_image":21},11,{"id":87,"title":60,"title_id":61,"slug":62,"cover_image":21},{"id":136,"title":64,"title_id":65,"slug":66,"cover_image":21},14,[138],{"authors_id":139},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},"en",[140,142],"id","\nYou run a small program that totals a cart of four items. It prints three lines, and the second one says the total is 36,500. The third gives an average price. The run exits normally, and not one of the three lines looks broken. Only the arithmetic disagrees with the receipt.\n\n## The loop that stops at the third item\n\nThe file holds an `int` array of four prices, a counter, a sum, and an average.\n\n```java\n\u002F\u002F Total.java\npublic class Total {\n    public static void main(String[] args) {\n        int[] prices = {12000, 15000, 9500, 10500};\n        System.out.println(\"items: \" + prices.length);\n        int total = 0;\n        for (int i = 0; i \u003C prices.length - 1; i++) {\n            total += prices[i];\n        }\n        System.out.println(\"total: \" + total);\n        System.out.println(\"average: \" + total \u002F prices.length);\n    }\n}\n```\n\nCompile and run it with `javac Total.java && java Total`. The exit status is 0, with no warning and no error.\n\n```plain\n$ javac Total.java && java Total\nitems: 4\ntotal: 36500\naverage: 9125\n```\n\nThat is three lines, and all of them are plausible. The count says 4, the total is a believable number, and so is the average. But the four prices add to 47000, so the total is 10,500 short.\n\n## Why the last item never enters the loop\n\nLook at the bound: `prices.length - 1`. The array holds four prices, so that is `3`, and the condition `i \u003C 3` holds for `i` equal to 0, 1 and 2. Index 3 is never read. The last price, 10500, never reaches the sum.\n\nThe damage has a second half. The average still divides by `prices.length`, which is `4`. So two lines are wrong, not one. The total is short by 10500, and that short total is then divided by four items when only three were added. The right average is `47000 \u002F 4`, which is `11750`.\n\nThe mirror mistake keeps the same array and the same loop body, and changes only the bound. Write `i \u003C= prices.length` instead. This version does not skip an item. It reads past the end, and the program stops with `java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4`. The trace points at `Total.main(Total.java:8)`, and the process exits with status 1.\n\nOne of these mistakes is silent and the other is loud. The silent one ships.\n\n## A check you can run\n\nYou can catch the quiet version with a second program. This checker reads the three lines the program prints and compares them against the values the four prices should produce.\n\n```java\n\u002F\u002F CheckTotal.java\nimport java.util.List;\nimport java.util.Scanner;\n\npublic class CheckTotal {\n    public static void main(String[] args) {\n        List\u003CString> expected = List.of(\"items: 4\", \"total: 47000\", \"average: 11750\");\n        Scanner in = new Scanner(System.in);\n        int passed = 0;\n        for (int i = 0; i \u003C expected.size(); i++) {\n            String got = in.hasNextLine() ? in.nextLine() : \"\u003Cnothing>\";\n            if (got.equals(expected.get(i))) {\n                passed++;\n                System.out.println(\"PASS  line \" + (i + 1) + \": \" + got);\n            } else {\n                System.out.println(\"FAIL  line \" + (i + 1) + \": expected \" + expected.get(i) + \", got \" + got);\n            }\n        }\n        if (in.hasNextLine()) {\n            System.out.println(\"FAIL  extra line: \" + in.nextLine());\n        }\n        System.out.println();\n        System.out.println(passed + \" of \" + expected.size() + \" checks passed\");\n        if (passed != expected.size()) {\n            System.exit(1);\n        }\n    }\n}\n```\n\nPipe the program into it with `javac CheckTotal.java && java Total | java CheckTotal`.\n\n```plain\n$ javac CheckTotal.java && java Total | java CheckTotal\nPASS  line 1: items: 4\nFAIL  line 2: expected total: 47000, got total: 36500\nFAIL  line 3: expected average: 11750, got average: 9125\n\n1 of 3 checks passed\n```\n\nThe line `items: 4` passes. The total and the average do not, so the checker prints `1 of 3 checks passed` and exits non-zero. The program alone said nothing, and the checker caught it at once.\n\n## Stop counting\n\nThe fix is to stop counting. Swap the counted loop for a `for`-each loop and change nothing else.\n\n```java\n\u002F\u002F Total.java\npublic class Total {\n    public static void main(String[] args) {\n        int[] prices = {12000, 15000, 9500, 10500};\n        System.out.println(\"items: \" + prices.length);\n        int total = 0;\n        for (int price : prices) {\n            total += price;\n        }\n        System.out.println(\"total: \" + total);\n        System.out.println(\"average: \" + total \u002F prices.length);\n    }\n}\n```\n\n```plain\n$ javac Total.java && java Total\nitems: 4\ntotal: 47000\naverage: 11750\n```\n\n```plain\n$ java Total | java CheckTotal\nPASS  line 1: items: 4\nPASS  line 2: total: 47000\nPASS  line 3: average: 11750\n\n3 of 3 checks passed\n```\n\nThe `for`-each loop hands you the value instead of the position. Against this file the checker prints `3 of 3 checks passed` and exits 0. The loop visits every element exactly once, in order. There is no bound to write down, so there is nothing to get one short.\n\nThe trade is honest: you lose the index. If you need to know which position you are on, the counted `for` stays the right tool.\n\n## The other two loops\n\nA `while` loop earns its place when you repeat work as long as a condition holds and there is no counter to advance. You state the condition, and the loop runs until it turns false.\n\nA `do`-`while` loop tests its condition after the body. That means the body always runs once before the first test.\n\n```java\n\u002F\u002F Rows.java\npublic class Rows {\n    public static void main(String[] args) {\n        int[] empty = {};\n        int i = 0;\n        while (i \u003C empty.length) {\n            System.out.println(\"while row \" + i);\n            i++;\n        }\n        int j = 0;\n        do {\n            System.out.println(\"do-while row \" + j);\n            j++;\n        } while (j \u003C empty.length);\n    }\n}\n```\n\n```plain\n$ javac Rows.java && java Rows\ndo-while row 0\n```\n\nThe `while` loop never runs on the empty array, because its condition is false before the body starts, and the `do`-`while` loop runs once. Inside any of these loops, `break` leaves the loop early and `continue` jumps to the next pass. As a rule of thumb, pick the loop whose stopping condition you can state without arithmetic.\n\n## Before the next article\n\nRemove one price from the array, so it holds three values such as `{12000, 15000, 10500}`. Run the fixed program. It prints `items: 3`, `total: 37500` and `average: 12500`, and all three lines are correct.\n\nNow run the unchanged checker. It reports `0 of 3 checks passed`, even though the code is now correct, because it expects four items. The checker's expectations are fixed at four items, so it tests the input as much as it tests the code.\n\nWhich line of your own last loop would you have to change if the array grew by one tomorrow?\n","- `i \u003C prices.length - 1` stops the loop one index early, so the last price never enters the sum.\n- The average still divides by `prices.length`, so the total and the average are both wrong while the count line stays right.\n- The mirror bound `i \u003C= prices.length` throws `ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4`.\n- `for (int price : prices)` walks the array with no index, so there is no bound to write one short, and it gives up the position.\n- `while` tests before the body and prints nothing on an empty array; `do`-`while` tests after and runs the body once.\n- A checker whose expectations are fixed fails on correct code when the input changes, so it tests the input as much as the code.","\u003Cp>You run a small program that totals a cart of four items. It prints three lines, and the second one says the total is 36,500. The third gives an average price. The run exits normally, and not one of the three lines looks broken. Only the arithmetic disagrees with the receipt.\u003C\u002Fp>\n\u003Ch2 id=\"the-loop-that-stops-at-the-third-item\">The loop that stops at the third item\u003C\u002Fh2>\n\u003Cp>The file holds an \u003Ccode>int\u003C\u002Fcode> array of four prices, a counter, a sum, and an average.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden bg-[#24292e] text-[#e1e4e8] transition-all\">\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ff5f56] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ffbd2e] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#27c93f] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"ml-2 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">JAVA\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%2F%2F%20Total.java%0Apublic%20class%20Total%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20int%5B%5D%20prices%20%3D%20%7B12000%2C%2015000%2C%209500%2C%2010500%7D%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22items%3A%20%22%20%2B%20prices.length)%3B%0A%20%20%20%20%20%20%20%20int%20total%20%3D%200%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20prices.length%20-%201%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20total%20%2B%3D%20prices%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println(%22total%3A%20%22%20%2B%20total)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22average%3A%20%22%20%2B%20total%20%2F%20prices.length)%3B%0A%20%20%20%20%7D%0A%7D\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed bg-[#24292e]\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#6A737D\">\u002F\u002F Total.java\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> Total\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> static\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> void\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> main\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] \u003C\u002Fspan>\u003Cspan style=\"color:#FFAB70\">args\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] prices \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">12000\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">15000\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">9500\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">10500\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">};\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"items: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices.length);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        for\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">; i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices.length \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">-\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 1\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">; i\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">++\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices[i];\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"total: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"average: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">\u002F\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices.length);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>Compile and run it with \u003Ccode>javac Total.java &amp;&amp; java Total\u003C\u002Fcode>. The exit status is 0, with no warning and no error.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper code-output my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden text-[#e1e4e8] transition-all\" data-output-block>\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"code-output-mark\" aria-hidden=\"true\">&gt;_\u003C\u002Fspan>\n      \u003Cspan class=\"ml-1 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">Output\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%24%20javac%20Total.java%20%26%26%20java%20Total%0Aitems%3A%204%0Atotal%3A%2036500%0Aaverage%3A%209125\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\">\u003Ccode>\u003Cspan class=\"code-output-cmd\">$ javac Total.java &amp;&amp; java Total\u003C\u002Fspan>\nitems: 4\ntotal: 36500\naverage: 9125\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>That is three lines, and all of them are plausible. The count says 4, the total is a believable number, and so is the average. But the four prices add to 47000, so the total is 10,500 short.\u003C\u002Fp>\n\u003Ch2 id=\"why-the-last-item-never-enters-the-loop\">Why the last item never enters the loop\u003C\u002Fh2>\n\u003Cp>Look at the bound: \u003Ccode>prices.length - 1\u003C\u002Fcode>. The array holds four prices, so that is \u003Ccode>3\u003C\u002Fcode>, and the condition \u003Ccode>i &lt; 3\u003C\u002Fcode> holds for \u003Ccode>i\u003C\u002Fcode> equal to 0, 1 and 2. Index 3 is never read. The last price, 10500, never reaches the sum.\u003C\u002Fp>\n\u003Cp>The damage has a second half. The average still divides by \u003Ccode>prices.length\u003C\u002Fcode>, which is \u003Ccode>4\u003C\u002Fcode>. So two lines are wrong, not one. The total is short by 10500, and that short total is then divided by four items when only three were added. The right average is \u003Ccode>47000 \u002F 4\u003C\u002Fcode>, which is \u003Ccode>11750\u003C\u002Fcode>.\u003C\u002Fp>\n\u003Cp>The mirror mistake keeps the same array and the same loop body, and changes only the bound. Write \u003Ccode>i &lt;= prices.length\u003C\u002Fcode> instead. This version does not skip an item. It reads past the end, and the program stops with \u003Ccode>java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4\u003C\u002Fcode>. The trace points at \u003Ccode>Total.main(Total.java:8)\u003C\u002Fcode>, and the process exits with status 1.\u003C\u002Fp>\n\u003Cp>One of these mistakes is silent and the other is loud. The silent one ships.\u003C\u002Fp>\n\u003Ch2 id=\"a-check-you-can-run\">A check you can run\u003C\u002Fh2>\n\u003Cp>You can catch the quiet version with a second program. This checker reads the three lines the program prints and compares them against the values the four prices should produce.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden bg-[#24292e] text-[#e1e4e8] transition-all\">\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ff5f56] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ffbd2e] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#27c93f] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"ml-2 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">JAVA\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%2F%2F%20CheckTotal.java%0Aimport%20java.util.List%3B%0Aimport%20java.util.Scanner%3B%0A%0Apublic%20class%20CheckTotal%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20List%3CString%3E%20expected%20%3D%20List.of(%22items%3A%204%22%2C%20%22total%3A%2047000%22%2C%20%22average%3A%2011750%22)%3B%0A%20%20%20%20%20%20%20%20Scanner%20in%20%3D%20new%20Scanner(System.in)%3B%0A%20%20%20%20%20%20%20%20int%20passed%20%3D%200%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20expected.size()%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20String%20got%20%3D%20in.hasNextLine()%20%3F%20in.nextLine()%20%3A%20%22%3Cnothing%3E%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(got.equals(expected.get(i)))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20passed%2B%2B%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22PASS%20%20line%20%22%20%2B%20(i%20%2B%201)%20%2B%20%22%3A%20%22%20%2B%20got)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22FAIL%20%20line%20%22%20%2B%20(i%20%2B%201)%20%2B%20%22%3A%20expected%20%22%20%2B%20expected.get(i)%20%2B%20%22%2C%20got%20%22%20%2B%20got)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(in.hasNextLine())%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22FAIL%20%20extra%20line%3A%20%22%20%2B%20in.nextLine())%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%20%20%20%20System.out.println(passed%20%2B%20%22%20of%20%22%20%2B%20expected.size()%20%2B%20%22%20checks%20passed%22)%3B%0A%20%20%20%20%20%20%20%20if%20(passed%20!%3D%20expected.size())%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.exit(1)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed bg-[#24292e]\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#6A737D\">\u002F\u002F CheckTotal.java\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">import\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> java.util.List;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">import\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> java.util.Scanner;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> CheckTotal\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> static\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> void\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> main\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] \u003C\u002Fspan>\u003Cspan style=\"color:#FFAB70\">args\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        List&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">> expected \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> List.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">of\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"items: 4\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"total: 47000\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"average: 11750\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        Scanner in \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> new\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> Scanner\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(System.in);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> passed \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        for\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">; i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> expected.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">size\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(); i\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">++\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            String got \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> in.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">hasNextLine\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">() \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">?\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> in.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">nextLine\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">() \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">:\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \"&#x3C;nothing>\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">            if\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (got.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">equals\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(expected.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">get\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(i))) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">                passed\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">++\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">                System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"PASS  line \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 1\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \": \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> got);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            } \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">else\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">                System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"FAIL  line \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 1\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \": expected \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> expected.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">get\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(i) \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \", got \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> got);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        if\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (in.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">hasNextLine\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">()) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"FAIL  extra line: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> in.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">nextLine\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">());\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">();\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(passed \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \" of \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> expected.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">size\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">() \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\"> \" checks passed\"\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        if\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (passed \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">!=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> expected.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">size\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">()) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            System.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">exit\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">1\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>Pipe the program into it with \u003Ccode>javac CheckTotal.java &amp;&amp; java Total | java CheckTotal\u003C\u002Fcode>.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper code-output my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden text-[#e1e4e8] transition-all\" data-output-block>\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"code-output-mark\" aria-hidden=\"true\">&gt;_\u003C\u002Fspan>\n      \u003Cspan class=\"ml-1 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">Output\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%24%20javac%20CheckTotal.java%20%26%26%20java%20Total%20%7C%20java%20CheckTotal%0APASS%20%20line%201%3A%20items%3A%204%0AFAIL%20%20line%202%3A%20expected%20total%3A%2047000%2C%20got%20total%3A%2036500%0AFAIL%20%20line%203%3A%20expected%20average%3A%2011750%2C%20got%20average%3A%209125%0A%0A1%20of%203%20checks%20passed\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\">\u003Ccode>\u003Cspan class=\"code-output-cmd\">$ javac CheckTotal.java &amp;&amp; java Total | java CheckTotal\u003C\u002Fspan>\nPASS  line 1: items: 4\nFAIL  line 2: expected total: 47000, got total: 36500\nFAIL  line 3: expected average: 11750, got average: 9125\n\n1 of 3 checks passed\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>The line \u003Ccode>items: 4\u003C\u002Fcode> passes. The total and the average do not, so the checker prints \u003Ccode>1 of 3 checks passed\u003C\u002Fcode> and exits non-zero. The program alone said nothing, and the checker caught it at once.\u003C\u002Fp>\n\u003Ch2 id=\"stop-counting\">Stop counting\u003C\u002Fh2>\n\u003Cp>The fix is to stop counting. Swap the counted loop for a \u003Ccode>for\u003C\u002Fcode>-each loop and change nothing else.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden bg-[#24292e] text-[#e1e4e8] transition-all\">\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ff5f56] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ffbd2e] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#27c93f] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"ml-2 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">JAVA\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%2F%2F%20Total.java%0Apublic%20class%20Total%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20int%5B%5D%20prices%20%3D%20%7B12000%2C%2015000%2C%209500%2C%2010500%7D%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22items%3A%20%22%20%2B%20prices.length)%3B%0A%20%20%20%20%20%20%20%20int%20total%20%3D%200%3B%0A%20%20%20%20%20%20%20%20for%20(int%20price%20%3A%20prices)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20total%20%2B%3D%20price%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println(%22total%3A%20%22%20%2B%20total)%3B%0A%20%20%20%20%20%20%20%20System.out.println(%22average%3A%20%22%20%2B%20total%20%2F%20prices.length)%3B%0A%20%20%20%20%7D%0A%7D\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed bg-[#24292e]\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#6A737D\">\u002F\u002F Total.java\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> Total\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> static\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> void\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> main\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] \u003C\u002Fspan>\u003Cspan style=\"color:#FFAB70\">args\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] prices \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">12000\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">15000\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">9500\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">, \u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\">10500\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">};\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"items: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices.length);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        for\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> price \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">:\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">+=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> price;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"total: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"average: \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> total \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">\u002F\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> prices.length);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003Cdiv class=\"code-block-wrapper code-output my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden text-[#e1e4e8] transition-all\" data-output-block>\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"code-output-mark\" aria-hidden=\"true\">&gt;_\u003C\u002Fspan>\n      \u003Cspan class=\"ml-1 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">Output\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%24%20javac%20Total.java%20%26%26%20java%20Total%0Aitems%3A%204%0Atotal%3A%2047000%0Aaverage%3A%2011750\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\">\u003Ccode>\u003Cspan class=\"code-output-cmd\">$ javac Total.java &amp;&amp; java Total\u003C\u002Fspan>\nitems: 4\ntotal: 47000\naverage: 11750\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003Cdiv class=\"code-block-wrapper code-output my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden text-[#e1e4e8] transition-all\" data-output-block>\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"code-output-mark\" aria-hidden=\"true\">&gt;_\u003C\u002Fspan>\n      \u003Cspan class=\"ml-1 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">Output\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%24%20java%20Total%20%7C%20java%20CheckTotal%0APASS%20%20line%201%3A%20items%3A%204%0APASS%20%20line%202%3A%20total%3A%2047000%0APASS%20%20line%203%3A%20average%3A%2011750%0A%0A3%20of%203%20checks%20passed\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\">\u003Ccode>\u003Cspan class=\"code-output-cmd\">$ java Total | java CheckTotal\u003C\u002Fspan>\nPASS  line 1: items: 4\nPASS  line 2: total: 47000\nPASS  line 3: average: 11750\n\n3 of 3 checks passed\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>The \u003Ccode>for\u003C\u002Fcode>-each loop hands you the value instead of the position. Against this file the checker prints \u003Ccode>3 of 3 checks passed\u003C\u002Fcode> and exits 0. The loop visits every element exactly once, in order. There is no bound to write down, so there is nothing to get one short.\u003C\u002Fp>\n\u003Cp>The trade is honest: you lose the index. If you need to know which position you are on, the counted \u003Ccode>for\u003C\u002Fcode> stays the right tool.\u003C\u002Fp>\n\u003Ch2 id=\"the-other-two-loops\">The other two loops\u003C\u002Fh2>\n\u003Cp>A \u003Ccode>while\u003C\u002Fcode> loop earns its place when you repeat work as long as a condition holds and there is no counter to advance. You state the condition, and the loop runs until it turns false.\u003C\u002Fp>\n\u003Cp>A \u003Ccode>do\u003C\u002Fcode>-\u003Ccode>while\u003C\u002Fcode> loop tests its condition after the body. That means the body always runs once before the first test.\u003C\u002Fp>\n\n\u003Cdiv class=\"code-block-wrapper my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden bg-[#24292e] text-[#e1e4e8] transition-all\">\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ff5f56] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#ffbd2e] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"w-3 h-3 rounded-full bg-[#27c93f] border border-black\u002F40 inline-block\">\u003C\u002Fspan>\n      \u003Cspan class=\"ml-2 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">JAVA\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%2F%2F%20Rows.java%0Apublic%20class%20Rows%20%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20int%5B%5D%20empty%20%3D%20%7B%7D%3B%0A%20%20%20%20%20%20%20%20int%20i%20%3D%200%3B%0A%20%20%20%20%20%20%20%20while%20(i%20%3C%20empty.length)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22while%20row%20%22%20%2B%20i)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20i%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20int%20j%20%3D%200%3B%0A%20%20%20%20%20%20%20%20do%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22do-while%20row%20%22%20%2B%20j)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20j%2B%2B%3B%0A%20%20%20%20%20%20%20%20%7D%20while%20(j%20%3C%20empty.length)%3B%0A%20%20%20%20%7D%0A%7D\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed bg-[#24292e]\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\" tabindex=\"0\">\u003Ccode>\u003Cspan class=\"line\">\u003Cspan style=\"color:#6A737D\">\u002F\u002F Rows.java\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> class\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> Rows\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">    public\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> static\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> void\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\"> main\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">String\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] \u003C\u002Fspan>\u003Cspan style=\"color:#FFAB70\">args\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">[] empty \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {};\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        while\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (i \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> empty.length) {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"while row \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> i);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            i\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">++\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        int\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> j \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">=\u003C\u002Fspan>\u003Cspan style=\"color:#79B8FF\"> 0\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#F97583\">        do\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> {\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            System.out.\u003C\u002Fspan>\u003Cspan style=\"color:#B392F0\">println\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">(\u003C\u002Fspan>\u003Cspan style=\"color:#9ECBFF\">\"do-while row \"\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\"> +\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> j);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">            j\u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">++\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\">;\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">        } \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">while\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> (j \u003C\u002Fspan>\u003Cspan style=\"color:#F97583\">&#x3C;\u003C\u002Fspan>\u003Cspan style=\"color:#E1E4E8\"> empty.length);\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">    }\u003C\u002Fspan>\u003C\u002Fspan>\n\u003Cspan class=\"line\">\u003Cspan style=\"color:#E1E4E8\">}\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\n\u003Cdiv class=\"code-block-wrapper code-output my-6 not-prose rounded-xl border-2 border-black dark:border-gray-600 shadow-neo overflow-hidden text-[#e1e4e8] transition-all\" data-output-block>\n  \u003Cdiv class=\"code-block-header flex items-center justify-between px-4 py-2.5 bg-[#1f2428] border-b-2 border-black dark:border-gray-600 font-mono text-xs font-bold text-gray-300 select-none\">\n    \u003Cdiv class=\"flex items-center gap-2\">\n      \u003Cspan class=\"code-output-mark\" aria-hidden=\"true\">&gt;_\u003C\u002Fspan>\n      \u003Cspan class=\"ml-1 font-mono font-bold text-xs uppercase tracking-wider text-gray-300\">Output\u003C\u002Fspan>\n    \u003C\u002Fdiv>\n    \u003Cbutton type=\"button\" class=\"copy-code-btn flex items-center gap-1.5 px-3 py-1 text-xs font-bold bg-[#2f363d] text-gray-200 border-2 border-black dark:border-gray-500 rounded-lg shadow-neo-sm hover:translate-x-0.5 hover:translate-y-0.5 hover:shadow-none hover:bg-gray-700 transition-all cursor-pointer\" data-code=\"%24%20javac%20Rows.java%20%26%26%20java%20Rows%0Ado-while%20row%200\" title=\"Copy\">\n      \u003Csvg class=\"w-3.5 h-3.5 copy-icon-svg\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\n        \u003Cpath stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z\">\u003C\u002Fpath>\n      \u003C\u002Fsvg>\n      \u003Cspan class=\"copy-text\">Copy\u003C\u002Fspan>\n    \u003C\u002Fbutton>\n  \u003C\u002Fdiv>\n  \u003Cdiv class=\"code-block-content p-4 overflow-x-auto text-sm font-mono leading-relaxed\">\n    \u003Cpre class=\"shiki github-dark\" style=\"background-color:#24292e;color:#e1e4e8\">\u003Ccode>\u003Cspan class=\"code-output-cmd\">$ javac Rows.java &amp;&amp; java Rows\u003C\u002Fspan>\ndo-while row 0\u003C\u002Fcode>\u003C\u002Fpre>\n  \u003C\u002Fdiv>\n\u003C\u002Fdiv>\u003Cp>The \u003Ccode>while\u003C\u002Fcode> loop never runs on the empty array, because its condition is false before the body starts, and the \u003Ccode>do\u003C\u002Fcode>-\u003Ccode>while\u003C\u002Fcode> loop runs once. Inside any of these loops, \u003Ccode>break\u003C\u002Fcode> leaves the loop early and \u003Ccode>continue\u003C\u002Fcode> jumps to the next pass. As a rule of thumb, pick the loop whose stopping condition you can state without arithmetic.\u003C\u002Fp>\n\u003Ch2 id=\"before-the-next-article\">Before the next article\u003C\u002Fh2>\n\u003Cp>Remove one price from the array, so it holds three values such as \u003Ccode>{12000, 15000, 10500}\u003C\u002Fcode>. Run the fixed program. It prints \u003Ccode>items: 3\u003C\u002Fcode>, \u003Ccode>total: 37500\u003C\u002Fcode> and \u003Ccode>average: 12500\u003C\u002Fcode>, and all three lines are correct.\u003C\u002Fp>\n\u003Cp>Now run the unchanged checker. It reports \u003Ccode>0 of 3 checks passed\u003C\u002Fcode>, even though the code is now correct, because it expects four items. The checker&#39;s expectations are fixed at four items, so it tests the input as much as it tests the code.\u003C\u002Fp>\n\u003Cp>Which line of your own last loop would you have to change if the array grew by one tomorrow?\u003C\u002Fp>\n","\u003Cul>\n\u003Cli>\u003Ccode>i &lt; prices.length - 1\u003C\u002Fcode> stops the loop one index early, so the last price never enters the sum.\u003C\u002Fli>\n\u003Cli>The average still divides by \u003Ccode>prices.length\u003C\u002Fcode>, so the total and the average are both wrong while the count line stays right.\u003C\u002Fli>\n\u003Cli>The mirror bound \u003Ccode>i &lt;= prices.length\u003C\u002Fcode> throws \u003Ccode>ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4\u003C\u002Fcode>.\u003C\u002Fli>\n\u003Cli>\u003Ccode>for (int price : prices)\u003C\u002Fcode> walks the array with no index, so there is no bound to write one short, and it gives up the position.\u003C\u002Fli>\n\u003Cli>\u003Ccode>while\u003C\u002Fcode> tests before the body and prints nothing on an empty array; \u003Ccode>do\u003C\u002Fcode>-\u003Ccode>while\u003C\u002Fcode> tests after and runs the body once.\u003C\u002Fli>\n\u003Cli>A checker whose expectations are fixed fails on correct code when the input changes, so it tests the input as much as the code.\u003C\u002Fli>\n\u003C\u002Ful>\n",[148,171,192,197,202,219],{"id":149,"title":150,"title_id":151,"slug":152,"excerpt":153,"excerpt_id":154,"has_translation":12,"published_at":155,"reading_time":156,"hidden_from_listing":15,"tags":157,"thumbnail_image":21,"og_image":162,"series":163,"authors":168},48,"What a Linked List Actually Is","Tentang Struktur Data LinkedList","tentang-struktur-data-linked-list","A linked list is a linear chain of nodes, each holding data and a pointer to the next node. Here is what that chain looks like, what it costs next to an array, and the same minimal implementation in Java, Python, and JavaScript.","Linked list adalah rantai linear berisi node, dan setiap node menyimpan data plus pointer ke node berikutnya. Ini bentuk rantainya, ongkosnya dibanding array, dan implementasi paling sederhana dalam Java, Python, serta JavaScript.","2026-11-21T01:00:00.000Z",4,[158,159,160,161],"struktur-data","linked-list","pointer","konsep","\u002Fog\u002Ftentang-struktur-data-linked-list.png",{"id":164,"title":165,"title_id":166,"slug":167},28,"Linear Data Structures","Struktur Data Linear","asd-struktur-linear",[169],{"authors_id":170},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},{"id":172,"title":173,"title_id":174,"slug":175,"excerpt":176,"excerpt_id":177,"has_translation":12,"published_at":178,"reading_time":179,"hidden_from_listing":15,"tags":180,"thumbnail_image":21,"og_image":183,"series":184,"authors":189},47,"Why Understanding Algorithms and Data Structures Actually Matters","Pentingnya Memahami Algoritma dan Struktur Data","pentingnya-memahami-algoritma-dan-struktur-data","An algorithm is an ordered list of steps, and that order decides how expensive a problem is. The examples are on the back of an instant noodle packet, in a factorial, and behind ctrl+z.","Sebagai konsep fundamental, algoritma menentukan seberapa mahal sebuah masalah diselesaikan. Contohnya ada di bungkus mie instan, di bilangan faktorial, dan di tombol undo.","2026-11-19T01:00:00.000Z",7,[181,158,161,182],"algoritma","dasar","\u002Fog\u002Fpentingnya-memahami-algoritma-dan-struktur-data.png",{"id":185,"title":186,"title_id":187,"slug":188},27,"About Algorithms and Data Structures","Tentang Algoritma dan Struktur Data","asd-intro",[190],{"authors_id":191},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},{"id":74,"title":75,"title_id":76,"slug":77,"excerpt":78,"excerpt_id":79,"has_translation":12,"published_at":80,"reading_time":81,"hidden_from_listing":15,"tags":82,"thumbnail_image":21,"og_image":85,"series":193,"authors":194},{"id":87,"title":60,"title_id":61,"slug":62},[195],{"authors_id":196},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},{"id":92,"title":93,"title_id":94,"slug":95,"excerpt":96,"excerpt_id":97,"has_translation":12,"published_at":98,"reading_time":81,"hidden_from_listing":15,"tags":99,"thumbnail_image":21,"og_image":101,"series":198,"authors":199},{"id":87,"title":60,"title_id":61,"slug":62},[200],{"authors_id":201},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},{"id":203,"title":204,"title_id":205,"slug":206,"excerpt":207,"excerpt_id":208,"has_translation":12,"published_at":98,"reading_time":14,"hidden_from_listing":15,"tags":209,"thumbnail_image":21,"og_image":214,"series":215,"authors":216},108,"At 11 a.m. the AI Quota Runs Out, and the Work Stops There","Jam 11 siang kuota AI-nya habis, dan kerjaan berhenti di situ","jam-11-kuota-ai-habis-kerjaan-berhenti","The panel says the usage limit is reached and the work stops, not because it is finished. Three guesses explain it, and all three are wrong: the limit is shaped like a cliff, and the day is built on top of one tool.","Panel di layar bilang batas pemakaian tercapai, dan kerja berhenti di situ. Bukan karena selesai. Ada tiga tebakan kenapa ini terjadi, dan ketiganya salah: batasnya berbentuk tebing, dan harinya dibangun di atas satu alat.",[210,211,212,213],"ai","tooling","rate-limit","produktivitas","\u002Fog\u002Fjam-11-kuota-ai-habis-kerjaan-berhenti.png",{"id":24,"title":25,"title_id":26,"slug":27},[217],{"authors_id":218},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},{"id":220,"title":221,"title_id":21,"slug":222,"excerpt":223,"excerpt_id":21,"has_translation":15,"published_at":224,"reading_time":81,"hidden_from_listing":15,"tags":225,"thumbnail_image":21,"og_image":228,"series":229,"authors":230},148,"Go Variables: the Mean That Printed 0.0","go-program-structure-and-variables","Six scores arrive, all six are added up, and Go prints a mean of 0.0. A second `total := 0` inside the `if` block is the whole bug, and the compiler that refuses unused variables has nothing to say about it.","2026-11-11T01:00:00.000Z",[226,182,227],"go","variabel","\u002Fog\u002Fgo-program-structure-and-variables.png",{"id":133,"title":56,"title_id":57,"slug":58},[231],{"authors_id":232},{"id":106,"name":107,"slug":108,"role":109,"profile_picture":21},[234,238,241,244,247,250],{"text":235,"depth":236,"id":237},"The loop that stops at the third item",2,"the-loop-that-stops-at-the-third-item",{"text":239,"depth":236,"id":240},"Why the last item never enters the loop","why-the-last-item-never-enters-the-loop",{"text":242,"depth":236,"id":243},"A check you can run","a-check-you-can-run",{"text":245,"depth":236,"id":246},"Stop counting","stop-counting",{"text":248,"depth":236,"id":249},"The other two loops","the-other-two-loops",{"text":251,"depth":236,"id":252},"Before the next article","before-the-next-article",1794557078179]