<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Irza Hashim</title>
    <description>The latest articles on DEV Community by Irza Hashim (@irza_hashim).</description>
    <link>https://dev.to/irza_hashim</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4015652%2F6691513b-4ffb-4e19-ba60-7559c3b17a1c.png</url>
      <title>DEV Community: Irza Hashim</title>
      <link>https://dev.to/irza_hashim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/irza_hashim"/>
    <language>en</language>
    <item>
      <title>Java While vs. Do-While Loops: The Critical Difference</title>
      <dc:creator>Irza Hashim</dc:creator>
      <pubDate>Sun, 05 Jul 2026 01:16:01 +0000</pubDate>
      <link>https://dev.to/irza_hashim/java-while-vs-do-while-loops-the-critical-difference-396f</link>
      <guid>https://dev.to/irza_hashim/java-while-vs-do-while-loops-the-critical-difference-396f</guid>
      <description>&lt;p&gt;In Java, both &lt;code&gt;while&lt;/code&gt; and &lt;code&gt;do-while&lt;/code&gt; loops repeat a block of code as long as a condition is true. However, there is one critical difference between them: when the condition is checked. &lt;/p&gt;

&lt;p&gt;Let's look at how they work using two simple code examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Java While Loop
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;while&lt;/code&gt; loop checks the condition &lt;em&gt;before&lt;/em&gt; executing the code block. If the condition is false at the very beginning, the code inside the loop will never run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ticketCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticketCount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Watching the movie..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ticketCount&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it works:
&lt;/h3&gt;

&lt;p&gt;Because &lt;code&gt;ticketCount&lt;/code&gt; is 0, the condition &lt;code&gt;ticketCount &amp;gt; 0&lt;/code&gt; is instantly false. The computer skips the loop completely. Nothing prints on the screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Java Do-While Loop
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;do-while&lt;/code&gt; loop executes the code block &lt;em&gt;first&lt;/em&gt;, and then checks the condition at the end. This means the loop will always run &lt;strong&gt;at least once&lt;/strong&gt;, even if the condition is completely false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;ticketCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Trying the ride once..."&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;ticketCount&lt;/span&gt;&lt;span class="o"&gt;--;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ticketCount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why it works:
&lt;/h3&gt;

&lt;p&gt;The computer enters the &lt;code&gt;do&lt;/code&gt; block first and prints the message. Then it checks the condition &lt;code&gt;ticketCount &amp;gt; 0&lt;/code&gt;. Since it is false, the loop stops. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Output Summary
&lt;/h2&gt;

&lt;p&gt;If you run both codes, your terminal output will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[While Loop Output]: (Nothing prints)

[Do-While Loop Output]:
Trying the ride once...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Conclusion for Beginners
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;strong&gt;&lt;code&gt;while&lt;/code&gt; loop&lt;/strong&gt; when you want to check the condition first.&lt;/li&gt;
&lt;li&gt;Use a &lt;strong&gt;&lt;code&gt;do-while&lt;/code&gt; loop&lt;/strong&gt; when you need the code to run at least one time, no matter what.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>A Beginner's Guide to For Loops in Java</title>
      <dc:creator>Irza Hashim</dc:creator>
      <pubDate>Sun, 05 Jul 2026 01:09:44 +0000</pubDate>
      <link>https://dev.to/irza_hashim/a-beginners-guide-to-for-loops-in-java-1g12</link>
      <guid>https://dev.to/irza_hashim/a-beginners-guide-to-for-loops-in-java-1g12</guid>
      <description>&lt;p&gt;Looping is a core concept in computer science. If you need to repeat a task multiple times, you use a loop. In this short tutorial, we will learn exactly how a Java for loop works using a simple code example.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Code Example
&lt;/h2&gt;

&lt;p&gt;Here is a basic Java loop that prints a message five times:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of i is: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How the Code Works Step-by-Step
&lt;/h2&gt;

&lt;p&gt;A for loop has three main parts inside the parentheses, separated by semicolons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;int i = 0;&lt;/code&gt; (Initialization):&lt;/strong&gt; This creates a counter variable named &lt;code&gt;i&lt;/code&gt; and sets it to 0. It only runs once when the loop starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;i &amp;lt; 5;&lt;/code&gt; (Condition):&lt;/strong&gt; The loop checks this condition before every single run. If &lt;code&gt;i&lt;/code&gt; is less than 5, the loop runs. If &lt;code&gt;i&lt;/code&gt; becomes 5, the loop stops immediately.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;i++&lt;/code&gt; (Increment):&lt;/strong&gt; This adds 1 to the counter variable &lt;code&gt;i&lt;/code&gt; at the end of every loop cycle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Output
&lt;/h2&gt;

&lt;p&gt;When you run this Java program, the computer will print this exact output on your screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The value of i is: 0
The value of i is: 1
The value of i is: 2
The value of i is: 3
The value of i is: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
