<?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>Talk Binary &#187; while loop</title>
	<atom:link href="http://talkbinary.com/tag/while-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Wed, 15 Feb 2012 16:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>C++ While Loop</title>
		<link>http://talkbinary.com/programming/c/c-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-while-loop/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 16:00:55 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ While Loop]]></category>
		<category><![CDATA[counting program]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3155</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ If Statements which introduced us to the idea of control flow and conditions. Today, we introduce the while loop. The While Loop The while loop is another control flow structure that allows the programmer continuously execute a block of instructions until a specific condition is met. Below<a class="moretag" href="http://talkbinary.com/programming/c/c-while-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In our previous tutorial, we learned about <a href="http://talkbinary.com/programming/c/c-if-else-statements/">C++ If Statements</a> which introduced us to the idea of control flow and conditions. Today, we introduce the while loop. </p>
<h3 id="section-1">The While Loop</h3>
<p>The while loop is another control flow structure that allows the programmer continuously execute a block of instructions until a specific condition is met. Below is an example of a program that contains a while loop. The idea of the program is for the user to input a variable n, and the while loop will continuously print out and increase n until it is over 100. </p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/08/WhileLoop.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/WhileLoop.jpg" alt="While Loop" title="WhileLoop" width="422" height="545" class="aligncenter size-full wp-image-3166" /></a></center></p>
<p>Now that you understand the diagram, take a look at the syntax.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> condition <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
     <span style="color: #666666;">// Statements </span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>With the previous diagram, let&#8217;s make the program that increases and prints out n until it reaches 100. </p>
<p>In order to become a programmer, its essential that you learn how to create a program based on a specification. So its good to try it out on your own before you look at the solution below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #666666;">// Creating and initializing n</span>
<span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// User prompt</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Please enter a number to count to 100: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Beginning of while loop</span>
<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">100</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #666666;">// Printing out n</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Increasing n </span>
     n <span style="color: #000080;">=</span> n <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span> <span style="color: #666666;">// End of While Loop</span>
&nbsp;
<span style="color: #666666;">// Program continues</span></pre></td></tr></table></div>

<h3 id="section-2">Going further with While Loops</h3>
<p>Now that you learned how to create while loops, let&#8217;s have some more practice.</p>
<ol>
<li>Add to the previous program an if statement that determines if the user entered a negative number. If they did, output a message and set n to 0 so it only counts from a positive number to 100.</li>
<li>Make a while loop that prints a number from a user inputted n, to 0. If the number is below zero, let the user know, and set n to 100.</li>
<li>Using nested while loops, create a program that asks the user for a number from 0 &#8211; 10.
<p>Then print out n stars on one line and print out n number of lines. For example,</p>
<p>n = 3<br />
***<br />
***<br />
***<br />
n = 2<br />
**<br />
**</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-while-loop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advanced Control Flow &#8211; While Loop</title>
		<link>http://talkbinary.com/programming/c/advanced-control-flow-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/advanced-control-flow-while-loop/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 21:17:02 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[palindrome]]></category>
		<category><![CDATA[while]]></category>
		<category><![CDATA[while loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=179</guid>
		<description><![CDATA[Introducing the while loop with an example and several exercises. Meant for beginners. ]]></description>
			<content:encoded><![CDATA[<p>There is another type of loop similar to the for loop called the while loop. It executes instructions until the condition is met.</p>
<h3 id="section-1">Introducing the While Loop</h3>
<p>The structure is below.</p>
<pre lang="c++">while ( condition ) {

statements to be executed

}</pre>
<p>Let&#8217;s make a guessing game where the user guesses a number and the user continues with the program until he guesses the correct number.</p>
<pre lang="c++">int guess;
int numberToGuess = 27;
cin &gt;&gt; guess;

while ( guess != numberToGuess ) {
cout &lt;&lt; "Sorry! Wrong number.\nKeep Guessing!\n";
}

cout &lt;&lt; "Congratulations you guessed the correct number!\n";</pre>
<p><span id="more-179"></span></p>
<h3 id="section-2">How it the While Loop works</h3>
<p>It&#8217;s straightforward actually. It simply continues executing the statements until the condition is met. It&#8217;s very similar to the if statements. You can have multiple conditions to be satisfied in order for the program to break out of the while loop. An example is below.</p>
<pre lang="c++">int num1 = 25;
int num2 = 75;

int guess;

cout &lt;&lt; "Pick a number between our mystery two numbers!";

cin &gt;&gt; guess;

while ( num1 &lt; guess &amp;&amp; num2 &gt; guess  ) {
cin &gt;&gt; guess;
}

cout &lt;&lt; "Congratulations you picked the correct range for your number!\n";</pre>
<h3 id="section-3">Further Practice with While Loops</h3>
<p><strong>Exercise 1:</strong> Make the user guess a magic number. Tell them if their number was either below or above the one they guess.</p>
<p><strong>Exercise 2:</strong> Make the user guess a magic number. If they got it correct. Make them guess a second magic number. If they don&#8217;t get it right the first time, they start again!</p>
<p>*Hint: You can do it with 2 while loops.</p>
<p><strong>Challenge 1:</strong> Determine if a word is a palindrome. What is a palindrome? mom, racecar, dad&#8230;</p>
<p>*Hint: Something found <a href="http://talkbinary.com/2008/06/fundamental-data-type-string/">here</a> will help you out solve this problem!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/advanced-control-flow-while-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-05-24 16:33:32 -->
