
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>while loop Archives - TECHNIG</title>
	<atom:link href="https://www.technig.com/tag/while-loop/feed/" rel="self" type="application/rss+xml" />
	<link>https://168.138.42.164/tag/while-loop/amp/</link>
	<description>Gateway for IT Experts and Tech Geeks</description>
	<lastBuildDate>Fri, 09 Sep 2016 05:15:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>

<image>
	<url>https://www.technig.com/wp-content/uploads/2020/04/32x32.png</url>
	<title>while loop Archives - TECHNIG</title>
	<link>https://168.138.42.164/tag/while-loop/amp/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">162720667</site>	<item>
		<title>Repetition (Looping) Control Structures in C++</title>
		<link>https://www.technig.com/repetition-looping-control-structures/</link>
					<comments>https://www.technig.com/repetition-looping-control-structures/#respond</comments>
		
		<dc:creator><![CDATA[Raza Bayani]]></dc:creator>
		<pubDate>Fri, 09 Sep 2016 05:15:32 +0000</pubDate>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Control Structure]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[while loop]]></category>
		<guid isPermaLink="false">https://www.technig.com/?p=8213</guid>

					<description><![CDATA[<div style="margin-bottom:20px;"></div>
<p>In programming languages &#8211; while coding a program, sometimes it is necessary to repeat a set of statements several times (Looping Control Structures ). A way to repeat statements is to type the same statements in the program over and over. For example, if you want to repeat some statements 100 times, you type the same statements [&#8230;]</p>
<p>The post <a href="https://www.technig.com/repetition-looping-control-structures/">Repetition (Looping) Control Structures in C++</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div style="margin-bottom:20px;"></div><p>In programming languages &#8211; while coding a program, sometimes it is necessary to repeat a set of statements several times (Looping Control Structures ). A way to repeat statements is to type the same statements in the program over and over. For example, if you want to repeat some statements 100 times, you type the same statements 100 times in the program. However, this solution of repeating statements is impractical, if not impossible. Fortunately, there is a better way to repeat a set of statements.</p>
<p>As noted earlier, C++ has three repetition, or looping control structures that allow you to repeat a set of statements until certain conditions are met.</p>
<p><strong>Note</strong>: The variable that controls the loop is called <strong>loop control variable (LCV)</strong></p>
<h1><span style="color: #3366ff;"><b><i>While </i></b></span><b>Looping Control Structures</b></h1>
<p>The first loop that we’re going to discuss about is <strong>while</strong> loop. The general form of <strong>while</strong> loop is:</p>
<pre class="lang:default decode:true ">while (expression)
      statement
</pre>
<p>&nbsp;</p>
<p>In C++, <em><span style="color: #3366ff;">while</span> </em>is a reserved word. Of course, the statement can be either a simple or compound statement. The expression acts as a decision maker and is usually a logical expression. The statement is called the body of the loop. Note that the parentheses around the expression are part of the syntax.</p>
<figure id="attachment_8214" aria-describedby="caption-attachment-8214" style="width: 800px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" class="wp-image-8214" src="https://www.technig.com/wp-content/uploads/2016/09/while-1024x563.jpg" alt="While Loop" width="800" height="440" /><figcaption id="caption-attachment-8214" class="wp-caption-text">while loop &#8211; Looping Control Structures</figcaption></figure>
<pre class="lang:default decode:true ">#include &lt;iostream&gt;
using namespace std;
int main()
{
   int X = 0; 			        //Line 1
  while (x &lt;= 20) 		        //Line 2
       {
       cout &lt;&lt; x &lt;&lt; " "; 		//Line 3
        x = x + 5;			 //Line 4
       }

return 0;
</pre>
<p>In <span style="color: #339966;">Line 1</span>, the variable <strong>x</strong> is set to <strong>0</strong>. The <strong>expression</strong> in the <span style="color: #3366ff;">while</span> statement (in <span style="color: #339966;">Line 2</span>), <strong>x &lt;</strong>= 20, is evaluated. Because the expression <strong>x &lt;</strong>= 20 evaluates to<span style="color: #3366ff;"> true</span>, the body of the <span style="color: #3366ff;">while</span> loop executes next. The body of the <span style="color: #3366ff;">while </span>loop consists of the statements in <span style="color: #339966;">Lines 3 and 4</span>. The statement in Line 3 outputs the value of <strong>x</strong>, which is <strong>0</strong>. The statement in<span style="color: #339966;"> Line 4</span> changes the value of <strong>x</strong> to <strong>5</strong>. After executing the statements in<span style="color: #339966;"> Lines 3 and 4</span>, the <strong>expression</strong> in the <span style="color: #3366ff;">while</span> loop (<span style="color: #339966;">Line 2</span>) is evaluated again. Because <strong>x</strong> is <strong>5</strong>, the expression <strong>x &lt;</strong>= 20 evaluates to<span style="color: #3366ff;"> true</span> and the body of the <span style="color: #3366ff;">while</span> loop executes again. This process of evaluating the <strong>expression</strong> and executing the body of the <span style="color: #3366ff;">while</span> loop continues until the <strong>expression</strong>, <strong>x &lt;</strong>= 20 (in <span style="color: #339966;">Line 2</span>), no longer evaluates to <span style="color: #3366ff;">true</span>.</p>
<p><a href="https://www.technig.com/wp-content/uploads/2016/09/whileex.jpg"><img decoding="async" class="size-full wp-image-8215" src="https://www.technig.com/wp-content/uploads/2016/09/whileex.jpg" alt="&quot;&lt;yoastmark" /></a></p>
<p>Kinds of <strong><em>while</em></strong> loops are: counter-controlled, flag-controlled, sentinel-controlled, EOF-controlled <strong><em>while</em> </strong>loops and etc.</p>
<p>&nbsp;</p>
<h1><span style="color: #3366ff;"><b><i>for</i> </b></span><b><span style="color: windowtext;">Repetition (Looping) Structure</span></b></h1>
<p>The general form of <strong><em>for </em></strong>loop is:</p>
<pre class="lang:default decode:true">for (initial statement; loop condition; update statement)
      statement
</pre>
<p>The <strong>initial statement</strong>, <strong>loop condition</strong>, and <strong>update statement</strong> (called<span style="color: #3366ff;"> <em>for</em></span> loop control statements) enclosed within the parentheses control the body (<strong>statement</strong>) of the <span style="color: #3366ff;"><em>for</em></span> statement.</p>
<figure id="attachment_8216" aria-describedby="caption-attachment-8216" style="width: 1265px" class="wp-caption aligncenter"><a href="https://www.technig.com/wp-content/uploads/2016/09/For.jpg"><img decoding="async" class="size-full wp-image-8216" src="https://www.technig.com/wp-content/uploads/2016/09/For.jpg" alt="For Loop" width="1265" height="716" /></a><figcaption id="caption-attachment-8216" class="wp-caption-text">For Loop</figcaption></figure>
<p>The <span style="color: #3366ff;">for</span> loop executes as follows:</p>
<ol>
<li>The <strong>initial statement</strong> executes.</li>
<li>The <strong>loop condition</strong> is evaluated. If the <strong>loop condition</strong> evaluates to <span style="color: #3366ff;">true</span>:
<ol>
<li>Execute the <span style="color: #3366ff;">for</span> loop <strong>statement</strong>.</li>
<li>Execute the <strong>update statement</strong> (the third expression in the parentheses).</li>
</ol>
</li>
<li>Repeat Step 2 until the loop condition evaluates to <span style="color: #3366ff;">false</span>.</li>
</ol>
<p>The <strong>initial statement</strong> usually initializes a variable (called the <span style="color: #3366ff;">for </span><strong>loop control</strong>, or <span style="color: #3366ff;">for</span> <strong>indexed</strong>, variable).</p>
<p>In C++, <span style="color: #3366ff;"><strong><em>for</em></strong> </span>is a reserved word.</p>
<p>Concentrate on the example bellow:</p>
<pre class="lang:default decode:true">#include &lt;iostream&gt;
using namespace std;
int main()
{
  int x;
  for (x = 0; x &lt; 10 ; x++)
    {  
     cout &lt;&lt; x &lt;&lt; " ";
     }
cout &lt;&lt; endl;

return 0;
}
</pre>
<p>The <strong>initial statement</strong>, <strong>x = 0;</strong>, initializes the <span style="color: #3366ff;">int</span> variable <strong>x</strong> to <strong>0</strong>. Next, the loop condition, <strong>x&lt;10</strong>, is evaluated. Because <strong>0&lt;</strong>10 is <span style="color: #3366ff;">true</span>, the print statement executes and outputs 0. The <strong>update statement</strong>, <strong>x++</strong>, then executes, which sets the value of <strong>x</strong> to <strong>1</strong>. Once again, the <strong>loop condition</strong> is evaluated, which is still <span style="color: #3366ff;">true</span>, and so on. When <strong>x</strong> becomes <strong>10</strong>, the <strong>loop condition</strong> evaluates to <span style="color: #3366ff;">false</span>, the <span style="color: #3366ff;">for</span> loop terminates, and the statement following the <span style="color: #3366ff;">for</span> loop executes.</p>
<figure id="attachment_8217" aria-describedby="caption-attachment-8217" style="width: 373px" class="wp-caption aligncenter"><a href="https://www.technig.com/wp-content/uploads/2016/09/forex.jpg"><img decoding="async" class="size-full wp-image-8217" src="https://www.technig.com/wp-content/uploads/2016/09/forex.jpg" alt="For Control Structure" width="373" height="241" /></a><figcaption id="caption-attachment-8217" class="wp-caption-text">For Loop Example</figcaption></figure>
<h1><span style="color: #3366ff;"><b><i>do…while</i> </b></span><b><span style="color: windowtext;">Repetition (Looping) Structure</span></b></h1>
<p>This section describes the third type of looping or repetition structure, called a <span style="color: #3366ff;">do&#8230;while</span> loop. The general form of a<span style="color: #3366ff;"> do&#8230;while</span> statement is as follows:</p>
<pre class="lang:default decode:true ">do
       statement
while (expression);
</pre>
<figure id="attachment_8218" aria-describedby="caption-attachment-8218" style="width: 700px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" class="wp-image-8218" src="https://www.technig.com/wp-content/uploads/2016/09/Do-while.jpg" alt="Repetition" width="700" height="525" /><figcaption id="caption-attachment-8218" class="wp-caption-text">do-while Loop</figcaption></figure>
<p>The <strong>statement</strong> executes first, and then the <strong>expression</strong> is evaluated. If the <strong>expression</strong> evaluates to <span style="color: #3366ff;">true</span>, the <strong>statement</strong> executes again. As long as the <strong>expression</strong> in a <span style="color: #3366ff;">do&#8230;while</span> statement is <span style="color: #3366ff;">true</span>, the <strong>statement</strong> executes. To avoid an infinite loop, you must, once again, make sure that the loop body contains a statement that ultimately makes the <strong>expression</strong> <span style="color: #3366ff;">false</span> and assures that it exits properly.</p>
<pre class="lang:default decode:true">#include &lt;iostream&gt;
using namespace std;
int main()
{
  x = 0;
  do {
      cout &lt;&lt; x &lt;&lt; " ";
      x = x + 5;
     }while (x &lt;= 20);
return 0;
}
</pre>
<p>The output of the <strong><em>code</em></strong> is:</p>
<p><span style="background-color: #333333; color: #ffffff;"><strong>0  5  10  15  20</strong></span></p>
<p>After 20 output, the statement:</p>
<p>x = x +5;</p>
<p>changes the value of <strong>x</strong> to <strong>25</strong> and so <strong>x &lt;</strong>=20 becomes <span style="color: #3366ff;">false</span>, which halts the loop.</p>
<ul style="list-style-type: disc;">
<li>In a <span style="color: #3366ff;">while</span> and <span style="color: #3366ff;">for</span> loop, the loop condition is evaluated before executing the body of the loop. Therefore, <span style="color: #3366ff;">while </span>and <span style="color: #3366ff;">for</span> loops are called <strong>pretest</strong> loops. On the other hand, the loop condition in a <span style="color: #3366ff;">do&#8230;while</span> loop is evaluated after executing the body of the loop. Therefore, <span style="color: #3366ff;">do&#8230;while</span> loops are called <b>post-test</b> loops. Because the <span style="color: #3366ff;">while</span> and <span style="color: #3366ff;">for</span> loops both have entry conditions, these loops may never activate. The <span style="color: #3366ff;">do&#8230;while</span> loop, on the other hand, has an exit condition and therefore always executes the statement at least once. Looping Control Structures</li>
</ul>
<p>&#8220;The end of Looping Control Structures in C++.</p>
<p>The post <a href="https://www.technig.com/repetition-looping-control-structures/">Repetition (Looping) Control Structures in C++</a> appeared first on <a href="https://www.technig.com">TECHNIG</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.technig.com/repetition-looping-control-structures/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">8213</post-id>	</item>
	</channel>
</rss>
