<?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: 221910303043</title>
    <description>The latest articles on DEV Community by 221910303043 (@221910303043).</description>
    <link>https://dev.to/221910303043</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%2F540951%2F85ccbf9a-09e4-4f20-92e6-b487df30d077.png</url>
      <title>DEV Community: 221910303043</title>
      <link>https://dev.to/221910303043</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/221910303043"/>
    <language>en</language>
    <item>
      <title>Loops in C</title>
      <dc:creator>221910303043</dc:creator>
      <pubDate>Tue, 15 Dec 2020 17:24:27 +0000</pubDate>
      <link>https://dev.to/221910303043/loops-in-c-lfj</link>
      <guid>https://dev.to/221910303043/loops-in-c-lfj</guid>
      <description>&lt;p&gt;WHAT ARE LOOPS?&lt;br&gt;
The computer has an ability to perform a set of instructions repeatedly . This involves repeating some portion of the program a specific number of times or infinite times until the basic condition is satisfied. These kind of repetitive structures or statements are called loops.&lt;/p&gt;

&lt;p&gt;METHODS IN LOOPS&lt;br&gt;
1.For statement&lt;br&gt;
2.While statement&lt;br&gt;
3.Do-While statement&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;WHILE LOOPS:
A while loop in C programming repeatedly executes a target 
statement as long as a given condition is true
Until the given condition is true, while loop in repeatedly 
executes a target statement.
When the condition becomes false, the program control passes to 
the line immediately following the loop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SYNTAX OF WHILE LOOP:&lt;/p&gt;

&lt;p&gt;// while(condition) {&lt;br&gt;
   statement(s);&lt;br&gt;
}&lt;br&gt;
1.Statement(s): may a block of statements.&lt;br&gt;
2.Condition: can be any expression.&lt;br&gt;
EXAMPLE:&lt;/p&gt;

&lt;p&gt;// Print numbers from 1 to 5&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;int main()&lt;br&gt;
{&lt;br&gt;
    int i = 1;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while (i &amp;lt;= 5)
{
    printf("%d\n", i);
    ++i;
}

return 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;FOR LOOP:&lt;br&gt;
Used in repetition control structure.&lt;br&gt;
Used when a loop that needs to execute a specific number of times.&lt;/p&gt;

&lt;p&gt;SYNTAX OF FOR LOOP:&lt;/p&gt;

&lt;p&gt;// for ( initialization; condition; increment ) {&lt;br&gt;
   statement(s);&lt;br&gt;
}&lt;br&gt;
FLOW OF CONTROL IN FORLOOP:&lt;/p&gt;

&lt;p&gt;1.INITIALIZATION:&lt;br&gt;
  It is the first step to be executed and it can be executed only &lt;br&gt;
  once.&lt;br&gt;
  Initialization allows you to declare and initialize any loop &lt;br&gt;
  control variables.&lt;br&gt;
2.CONDITION:&lt;br&gt;
  Condition is the second step in ‘for’ loop where it can be &lt;br&gt;
  evaluated.&lt;br&gt;
3.INCREMENT:&lt;br&gt;
  Increment is the third step after condition . Flow of control &lt;br&gt;
  jumps back to the increment statement after the body of ‘for’ &lt;br&gt;
  loop executes.&lt;br&gt;
  Loop control variables can be updated using this statement.&lt;br&gt;
  This statement can also be left blank.&lt;br&gt;
EXAMPLE:&lt;br&gt;
// Print numbers from 1 to 10&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;int main() {&lt;br&gt;
  int i;&lt;/p&gt;

&lt;p&gt;for (i = 1; i &amp;lt; 11; ++i)&lt;br&gt;
  {&lt;br&gt;
    printf("%d ", i);&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;DO-WHILE LOOP:&lt;br&gt;
The Do-While loop is similar to the while loop with one important difference. The body of do-while loop is executed at least once. Only then, the test expression is evaluated.&lt;/p&gt;

&lt;p&gt;SYNTAX OF DO-WHILE :&lt;br&gt;
do&lt;br&gt;
{&lt;br&gt;
   // statements inside the body of the loop&lt;br&gt;
}&lt;br&gt;
while (testExpression);&lt;br&gt;
 EXAMPLE:&lt;br&gt;
// Program to add numbers until the user enters zero&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;int main()&lt;br&gt;
{&lt;br&gt;
    double number, sum = 0;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// the body of the loop is executed at least once
do
{
    printf("Enter a number: ");
    scanf("%lf", &amp;amp;number);
    sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;The major difference between While and do-while :&lt;/p&gt;

&lt;p&gt;WHILE&lt;br&gt;
1.It tests the condition before executing any of the statements &lt;br&gt;
  within the while loop.&lt;/p&gt;

&lt;p&gt;2.It doesn’t execute its statements if the condition fails for the &lt;br&gt;
  first time.&lt;/p&gt;

&lt;p&gt;DO - WHILE&lt;br&gt;
1.It tests the condition after having the executed the statements &lt;br&gt;
  with in the loops.&lt;/p&gt;

&lt;p&gt;2.It executes the statements at least once, even if the condition &lt;br&gt;
  fails.&lt;/p&gt;

</description>
      <category>loops</category>
      <category>while</category>
      <category>for</category>
      <category>differences</category>
    </item>
  </channel>
</rss>
