<?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>Forem: Thisu</title>
    <description>The latest articles on Forem by Thisu (@thisu_writes).</description>
    <link>https://forem.com/thisu_writes</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3627479%2Fc0a4bc70-4d2c-405e-a62c-fd981429f6de.jpg</url>
      <title>Forem: Thisu</title>
      <link>https://forem.com/thisu_writes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/thisu_writes"/>
    <language>en</language>
    <item>
      <title>Loops (Explained Like You're in Uni With Me)</title>
      <dc:creator>Thisu</dc:creator>
      <pubDate>Mon, 24 Nov 2025 15:31:40 +0000</pubDate>
      <link>https://forem.com/thisu_writes/loops-explained-like-youre-in-uni-with-me-1gna</link>
      <guid>https://forem.com/thisu_writes/loops-explained-like-youre-in-uni-with-me-1gna</guid>
      <description>&lt;p&gt;Before I started my degree, I honestly thought “loops” were just something that happened in Spotify playlists. But two months into coding, I discovered their actual magic and now I’m here to explain what loops really are, in the simplest (and funniest) way possible, so you too can feel like a genius.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Do We Even Need Loops?
&lt;/h3&gt;

&lt;p&gt;Imagine your lab instructor says:&lt;br&gt;&lt;br&gt;
“Write a program to print ‘Hello World’ 100 times.”  &lt;/p&gt;

&lt;p&gt;What would you do?&lt;/p&gt;

&lt;p&gt;Maybe something like this:&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="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;"Hello World"&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;"Hello World"&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;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ...97 more lines of pure suffering&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sure, you could copy–paste… but it still takes time, looks messy, and makes you question all your life choices.&lt;/p&gt;

&lt;p&gt;But if you know loops? → 3–4 lines and you’re done. Magic.&lt;/p&gt;

&lt;h2&gt;
  
  
  For Loop — When You Know Exactly How Many Times Mom Will Nag
&lt;/h2&gt;

&lt;p&gt;Real-life scenario: Morning time. Mom enters your room to wake you up.&lt;br&gt;
Think of a real-life situation.&lt;/p&gt;

&lt;p&gt;Your mom comes into your room every morning to wake you up.&lt;br&gt;
The first time? You don’t get up.&lt;br&gt;
Second? Nope.&lt;br&gt;
She keeps nagging until you finally leave the bed.&lt;/p&gt;

&lt;p&gt;After years of experience, you know this pattern:&lt;br&gt;
If she nags 10 times, the 11th time she will definitely lose her temper so you have to wake up.&lt;/p&gt;

&lt;p&gt;This kind of predictable repetition = perfect for a 'for loop'.&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;1&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;10&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="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s"&gt;". WAKE UPPP!!!!"&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;p&gt;This runs exactly 10 times. No more, no less.&lt;/p&gt;

&lt;p&gt;Now back to the lab task, printing “Hello World” 100 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;1&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;100&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;"Hello World"&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;p&gt;Boom. 100 times. Zero pain.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does This Black Magic Actually Work?
&lt;/h3&gt;

&lt;p&gt;Let’s break down the three parts inside the parentheses:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;int i = 1 → starting point (mom’s first nag)&lt;/li&gt;
&lt;li&gt;i &amp;lt;= 100 → condition: keep going as long as this is true&lt;/li&gt;
&lt;li&gt;i++ → after each loop, increase i by 1 (next nag)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Initialization → Condition → Increment/Decrement. &lt;/p&gt;

&lt;h2&gt;
  
  
  While Loop — When Mom Won’t Stop Until the Room Is Actually Clean
&lt;/h2&gt;

&lt;p&gt;This scenario is painfully accurate.&lt;/p&gt;

&lt;p&gt;Your mom will NEVER stop nagging until your room is cleaned, laundry folded, and your soul cleansed.&lt;/p&gt;

&lt;p&gt;This is where while loops shine, they run until a condition becomes true.&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;while&lt;/span&gt; &lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;roomIsClean&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;"Clean the room!!!!!!!!!"&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;p&gt;This loop continues until the room is clean.&lt;/p&gt;

&lt;h3&gt;
  
  
  Printing "Hello World" with a While Loop
&lt;/h3&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;count&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;
  
  
  What's happening here?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You start with a counter: count = 1&lt;/li&gt;
&lt;li&gt;The loop keeps running as long as count &amp;lt;= 100&lt;/li&gt;
&lt;li&gt;Each loop prints “Hello World”&lt;/li&gt;
&lt;li&gt;After every print, count increases by 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once count becomes 101, the condition becomes false → loop stops.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Beginner Mistake: Infinite Loops
&lt;/h2&gt;

&lt;p&gt;Be careful! If you forget to change the variable inside your loop, or your condition never becomes false, the loop will run forever.&lt;/p&gt;

&lt;p&gt;Example of an accidental infinite loop:&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;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// forgot count++&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your program will print forever and cry for help.&lt;br&gt;
So always check your:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;condition&lt;/li&gt;
&lt;li&gt;increment/decrement&lt;/li&gt;
&lt;li&gt;starting value&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When should you use which loop?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Loop Type&lt;/th&gt;
&lt;th&gt;When to Use It&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;for loop&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Use when you know exactly how many times the loop should run.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;while loop&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Use when you don't know the exact number of repetitions and the loop depends on a condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Loops make your life (and code) easier by letting you repeat actions without writing the same line again and again.&lt;/p&gt;

&lt;p&gt;Now you know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what a for loop is&lt;/li&gt;
&lt;li&gt;what a while loop is&lt;/li&gt;
&lt;li&gt;when to use each&lt;/li&gt;
&lt;li&gt;and what mistakes to avoid&lt;/li&gt;
&lt;/ul&gt;

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