<?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; for loop</title>
	<atom:link href="http://talkbinary.com/tag/for-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>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-05-23 11:23:31 -->
