<?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: Lestari NS</title>
    <description>The latest articles on Forem by Lestari NS (@lestariningratna).</description>
    <link>https://forem.com/lestariningratna</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%2F131989%2Fa9956945-2728-46bc-8f0b-92cb8d544208.jpg</url>
      <title>Forem: Lestari NS</title>
      <link>https://forem.com/lestariningratna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lestariningratna"/>
    <language>en</language>
    <item>
      <title>Harnessing the Power of 'While' Loops: Flexible and Controlled Iteration in JavaScript.</title>
      <dc:creator>Lestari NS</dc:creator>
      <pubDate>Sun, 11 Jun 2023 15:20:28 +0000</pubDate>
      <link>https://forem.com/lestariningratna/harnessing-the-power-of-while-loops-flexible-and-controlled-iteration-in-javascript-3gam</link>
      <guid>https://forem.com/lestariningratna/harnessing-the-power-of-while-loops-flexible-and-controlled-iteration-in-javascript-3gam</guid>
      <description>&lt;p&gt;Loops are an essential component of any programming language, allowing developers to repeat a set of instructions multiple times. As we already know, there are several types of loops available, including the 'for' loop and the 'while' loop. In this article, we will explore the power of the 'while' loop in JavaScript and uncover situations where it excels compared to the 'for' loop.&lt;/p&gt;




&lt;h2&gt;
  
  
  while loop vs for loop
&lt;/h2&gt;

&lt;p&gt;The 'while' loop is a simple construct that repeats a block of code as long as a specified condition evaluates to true. Its syntax is straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (condition) {
  // code to be executed
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's take a look at code example from "&lt;a href="https://eloquentjavascript.net/"&gt;Eloquent Javascript&lt;/a&gt;"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number = 0;
while (number &amp;lt;= 12) {
  console.log(number);
  number = number + 2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In that example loop is started with number binding to track the progress of that program. Second statement is &lt;code&gt;while&lt;/code&gt; and then followed by expression inside parenthesis to check whether the condition is true or false. If the condition is true, the program will execute the code inside the loop block, adding 2 to previous value of variable &lt;code&gt;number&lt;/code&gt;. It will keep repeating as long as the result fulfill loop condition (return boolean value &lt;code&gt;true&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Compared to while loop, for loop is relatively shorter and clearer, because we can do initialization, checking condition and updating the state of the loop at the same time in one line.&lt;/p&gt;

&lt;p&gt;This is code example of for loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (let i = 0; i &amp;lt; 5; i++) {
  console.log("Count: " + i);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the 'for' loop starts with an initialization statement (&lt;code&gt;let i = 0&lt;/code&gt;) where we set the loop counter to 0. The condition (&lt;code&gt;i &amp;lt; 5&lt;/code&gt;) specifies that the loop should continue as long as the value of &lt;code&gt;i&lt;/code&gt; is less than 5. The increment statement (&lt;code&gt;i++&lt;/code&gt;) increments the value of &lt;code&gt;i&lt;/code&gt; by 1 after each iteration.&lt;/p&gt;

&lt;p&gt;Inside the loop, the code &lt;code&gt;console.log("Count: " + i)&lt;/code&gt; is executed, which logs the value of &lt;code&gt;i&lt;/code&gt; along with the string "Count:" to the console. The loop will run for five iterations, outputting the numbers 0 to 4.&lt;/p&gt;

&lt;p&gt;Both &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loop are pretty much similar. Most developers probably prefer &lt;code&gt;for&lt;/code&gt; loop because of the reason as mentioned before. However there are some situations when we would prefer to use &lt;code&gt;while&lt;/code&gt; loop rather than &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;




&lt;h2&gt;
  
  
  Particular scenarios where while loop is powerful
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop is often a better choice over the &lt;code&gt;for&lt;/code&gt; loop in the following scenarios:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Indeterminate loop conditions:
&lt;/h3&gt;

&lt;p&gt;When the number of iterations is not known beforehand or cannot &lt;br&gt;
   be easily determined, the &lt;code&gt;while&lt;/code&gt; loop provides a flexible &lt;br&gt;
   option. &lt;/p&gt;

&lt;p&gt;It allows you to continue looping as long as a certain &lt;br&gt;
   condition remains true, without the need for a predetermined &lt;br&gt;
   iteration count.&lt;/p&gt;

&lt;p&gt;This is useful when the loop termination depends on dynamic &lt;br&gt;
   factors or external inputs.&lt;/p&gt;

&lt;p&gt;For example when writing network applications, a &lt;code&gt;while&lt;/code&gt; loop &lt;br&gt;
   can be employed to continuously listen for incoming network &lt;br&gt;
   requests or messages. The loop continues running until a &lt;br&gt;
   specific condition is met, such as receiving a termination &lt;br&gt;
   signal or completing a particular task.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Continuous monitoring:
&lt;/h3&gt;

&lt;p&gt;If you need to continuously monitor a condition or event and &lt;br&gt;
   perform actions until a specific condition is met, the &lt;code&gt;while&lt;/code&gt; &lt;br&gt;
   loop is a suitable choice. &lt;/p&gt;

&lt;p&gt;It allows you to repeatedly check the condition and execute the &lt;br&gt;
   loop body until the desired state is achieved. &lt;/p&gt;

&lt;p&gt;For example, in systems that rely on sensor data, such as &lt;br&gt;
   environmental monitoring or industrial automation, a &lt;code&gt;while&lt;/code&gt; &lt;br&gt;
   loop can continuously monitor sensor inputs, detect changes or &lt;br&gt;
   anomalies, and trigger appropriate actions or alarms.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Dynamic loop control:
&lt;/h3&gt;

&lt;p&gt;In situations where the loop control is subject to change &lt;br&gt;
   during the loop execution, the &lt;code&gt;while&lt;/code&gt; loop is more flexible. &lt;/p&gt;

&lt;p&gt;You can modify the condition within the loop body or use &lt;br&gt;
   &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;continue&lt;/code&gt; statements to control the loop flow based &lt;br&gt;
   on certain conditions. &lt;/p&gt;

&lt;p&gt;For example when implementing search algorithms, such as depth- &lt;br&gt;
   first search or iterative deepening, a &lt;code&gt;while&lt;/code&gt; loop can provide &lt;br&gt;
   dynamic control. The loop can control the depth or limit of the &lt;br&gt;
   search, adjust search parameters based on intermediate results, &lt;br&gt;
   or terminate the search when a solution is found.&lt;/p&gt;




&lt;p&gt;In summary, the &lt;code&gt;while&lt;/code&gt; loop is preferable when you have indeterminate or dynamic loop conditions, continuous monitoring requirements, or dynamic loop control flow is desired. It offers more flexibility and adaptability compared to the &lt;code&gt;for&lt;/code&gt; loop, which is typically used for iterating over a known range of values or collections. When everything is said and done, it comes back to what problem we have to solve and how we utilize the best type of loop based on what we need to harness its power.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>learning</category>
      <category>coding</category>
    </item>
  </channel>
</rss>
