<?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; loop</title>
	<atom:link href="http://talkbinary.com/tag/loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Advanced Control Flow &#8211; The Do While Loop</title>
		<link>http://talkbinary.com/programming/c/advanced-control-flow-the-do-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/advanced-control-flow-the-do-while-loop/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 18:02:59 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[do while]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=508</guid>
		<description><![CDATA[The Do While Loop is a loop that guarantees execution of the statements within the loop at least once. The structure of the Do While Loop Below is the structure: do { //statements } while ( condition(s) ) ; What is this useful for? In order to execute a set of commands at least once.]]></description>
			<content:encoded><![CDATA[<p>The Do While Loop is a loop that guarantees execution of the statements within the loop at least once. </p>
<h3 id="section-1">The structure of the Do While Loop</h3>
<p>Below is the structure:</p>
<pre lang="c++">do { 

//statements 

} while ( condition(s) ) ;</pre>
<p>What is this useful for? In order to execute a set of commands at least once. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/advanced-control-flow-the-do-while-loop/feed/</wfw:commentRss>
		<slash:comments>0</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>
		<item>
		<title>Advanced Control Flow &#8211; For Loop</title>
		<link>http://talkbinary.com/programming/c/advanced-control-flow-for-loop/</link>
		<comments>http://talkbinary.com/programming/c/advanced-control-flow-for-loop/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 02:44:23 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=178</guid>
		<description><![CDATA[Introducing a basic for loop with a detailed break down of every line of code that makes this loop. Practice problems are also provided.]]></description>
			<content:encoded><![CDATA[<p>Lets say we wanted to repeat an operation multiple times on a set of data. Do we really want to write the code for it multiple times? Of course not. This is where loops come into play.</p>
<h3 id="section-1">The Foor Loop</h3>
<p>How would you calculate the factorial of 1-10 without using loops? Below would be one of many solutions.</p>
<pre lang="c++">
int factorial = 1;
cout << "!1 = " << factorial << endl;
factorial = factorial * 2;
cout << "!2 = " << factorial << endl;
factorial = factorial * 3;
cout << "!3 = " << factorial << endl;
factorial = factorial * 4;
cout << "!4 = " << factorial << endl;
factorial = factorial * 5;
cout << "!5 = " << factorial << endl;
//Ok! That's a lot of work!</pre>
<p>Would print out </p>
<pre lang="c++">!1 = 1
!2 = 2
!3 = 6
!4 = 24
!5 = 120</pre>
<p><br/>Imagine doing this simple operation up to 100. <span id="more-178"></span></p>
<h3 id="section-2">Structure of the for loop</h3>
<p>Below is the structure of a for loop.</p>
<pre lang="c++">
for ( initialization of variable(s) ; condition ; assignment of different value to the previous value )
{
     statements to be executed
}</pre>
<p>Don't worry if you don't understand, below you'll see an example.<br />
<br/>Below is a "for loop" that will perform the same operation and can be easily manipulated to perform the previous operations from 1-10 and even to 100 with simple ease.</p>
<pre lang="c++">
int number = 10;
int factorial = 1;
for ( int i = 1; i <= number; i++ )
{
     //Multiplies the previous factorial value by the index value to calculate the next factorial
     factorial = factorial*i;
     cout << "!" << i << " = " << factorial << endl;
}
</pre>
<h3 id="section-3">Breaking down the For Loop</h3>
<p>Below is a detailed explanation of how the previous code works.</p>
<pre lang="c++">int number = 10; //We want to display !1 - !10 Change this number, to go even further!</pre>
<p><br/></p>
<pre lang="c++">int factorial = 1; //This will be the integer that keeps track of the previous factorial value to be able to find the next with ease</pre>
<p><br/></p>
<pre lang="c++">for ( int i = 1; i <= number; i++ ) //Below is the break down

//This variable pertains to this for loop and will reach out of scope once the loop finishes executing. This variable can be though of an index. Since we are finding the values of the factorial in increments of one, its obvious to start with 1.
int i = 1; 

//This is the condition in which this loop will execute until it is no longer true. Our number is 10, so while our index value is less than our number, this loop will execute.
i <= number 

//This increments our index value by 1 so we can cover all numbers from 1-10. We can also use statements such as (i += 2) , ( i-- ) , ( i *= 2 ) , and more.
i++
</pre>
<p>Let's break this down, this for loop executes these lines of code.</p>
<pre lang="c++">
int factorial = 1;
int i = 1;
factorial = factorial * i; // !1 = 1 * 1 = 1
i++; //i = 2;
factorial = factorial * i;// !2 = 2 * 1 = 2
i++; //i = 3;
factorial = factorial * i;// !3 = 3 * 2 = 6
//and so on...until our condition is met</pre>
<p><br/></p>
<h3 id="section-4">How to master For Loops </h3>
<p>With practice! Write down the steps taken. Try to change the values of the variables to see what happens.<br />
<br/><br />
<br/>Some examples to try out can be the following.<br />
<br/><b>Example 1: Counting from 1 to 100</b></p>
<pre lang="c++"> 1 2 3 4 5 6 7 8 9 10 11 ...</pre>
<p><br/><b>Example 2: Counting from 100 to 1</b></p>
<pre lang="c++"> 100 99 98 97...</pre>
<p><b>Example 3: The classic Triangle of Stars</b></p>
<pre lang="c++"> ****
***
**
*</pre>
<p>*Hint You will need to use two for loops instead of one. Can you figure out how? Need help? Visit the <a href="http://forums.talkbinary.com">Forums</a>. Registration is free, and you can ask your questions and get answers!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/advanced-control-flow-for-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-02-11 12:31:51 -->
