<?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; C++</title>
	<atom:link href="http://talkbinary.com/tag/c/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>C++ &#8211; Scope of Variables</title>
		<link>http://talkbinary.com/programming/c/c-scope-of-variables/</link>
		<comments>http://talkbinary.com/programming/c/c-scope-of-variables/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 17:23:24 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[c++ global variables]]></category>
		<category><![CDATA[c++ local variables]]></category>
		<category><![CDATA[c++ scope of variables]]></category>
		<category><![CDATA[scope of variables]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3601</guid>
		<description><![CDATA[The scope of a variable determines the block of instructions in which a variable may be accessed. To understand this topic, we&#8217;ll start by discussing local variables. Local Variables Local variables may be accessed within the block of instructions in which they were declared. For example, when we write a main function, all variables declared<a class="moretag" href="http://talkbinary.com/programming/c/c-scope-of-variables/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>The scope of a variable determines the block of instructions in which a variable may be accessed. To understand this topic, we&#8217;ll start by discussing local variables. </p>
<h3 id="section-1">Local Variables</h3>
<p>Local variables may be accessed within the block of instructions in which they were declared. For example, when we write a main function, all variables declared in that block of code may be accessed any place after they have been initialized.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">int</span> a, b<span style="color: #008080;">;</span>
    string message <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;The sum of a and b is: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> a <span style="color: #000080;">&gt;&gt;</span> b<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> message <span style="color: #000080;">&lt;&lt;</span> a <span style="color: #000040;">+</span> b <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-3601"></span><br />
Variables declared within a block of instructions such as an if-else statement or loop may only be used local to that block as well.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> res <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">int</span> a <span style="color: #000080;">=</span> i<span style="color: #000040;">*</span>i<span style="color: #008080;">;</span>
     res <span style="color: #000040;">+</span><span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> res <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>In the example above, variable <strong>a</strong> may only be accessed within the for loop. </p>
<p>As you may now know, variables declared within functions are also only local to that function.</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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #0000ff;">int</span> factorial <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
          factorial <span style="color: #000040;">*</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> n<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> fac <span style="color: #000080;">=</span> factorial<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Factorial of n: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> fac <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-2">Global Variables</h3>
<p>Global variables on the other hand may be accessed within every scope of the program. The use of global variables is usually discouraged since they may be modified from anywhere. They may be used to avoid having to pass in a variable that remains continuous throughout the program.</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
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> bonus <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">bool</span> flag <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// This function increases salary and adds the bonus</span>
<span style="color: #666666;">// if the flag is true</span>
<span style="color: #0000ff;">int</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> flag <span style="color: #008000;">&#41;</span> 
        <span style="color: #0000ff;">return</span> n <span style="color: #000040;">+</span> bonus<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span> 
        <span style="color: #0000ff;">return</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// This function increases the bonus</span>
<span style="color: #0000ff;">void</span> increaseBonus<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    bonus <span style="color: #000040;">+</span><span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// This function changes the flag </span>
<span style="color: #0000ff;">void</span> setFlag<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span> n<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    flag <span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The bonus is currently: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> bonus <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> mySalary <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
    mySalary <span style="color: #000040;">+</span><span style="color: #000080;">=</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    setFlag<span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>flag<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    mySalary <span style="color: #000040;">+</span><span style="color: #000080;">=</span> increaseSalary<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;My Salary is now: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> mySalary <span style="color: #000080;">&lt;&lt;</span> endl
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<blockquote><h4 id="section-3">Only use global variables when absolutely needed</h4>
<p>Remember, only use global variables if you absolutely need to. It is very cumbersome to debug a program with global variables if several functions are accessing and modifying their values. A good way to use them is possibly for a flag to run auxiliary functions to help decode a program if it its true.</p></blockquote>
<h3 id="section-4">Going further</h3>
<p>Now that you understand how the scope of variables work, try answering the following questions.</p>
<ol>
<li>Can two different functions declare variables with the same names?</li>
<li>If you declare a variable within the <strong>if</strong> clause, may you access it in the <strong>else</strong> clause?</li>
<li>If you declare a variable <strong>count</strong> in an if statement, can you access it within a nested if statement within that original if statement?</li>
<li>What is the output up the previous global variable example? What is the value of <strong>mySalary?</strong></li>
<li>Write a program with a global boolean flag that determines whether to display auxiliary <strong>cout</strong> statements to help debug your code. (Example: Print out factorial at every iteration.)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-scope-of-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ For Loop</title>
		<link>http://talkbinary.com/programming/c/c-for-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-for-loop/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 16:00:08 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ For Loop]]></category>
		<category><![CDATA[C++ While Loop]]></category>
		<category><![CDATA[For Loop Break]]></category>
		<category><![CDATA[For Loop Example]]></category>
		<category><![CDATA[For Loop Factorial]]></category>
		<category><![CDATA[For Loop Summation]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3170</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ While Loops. Today, we&#8217;ll learn about for loops. For loops are different than while loops due to the way they function. This allows one to choose what type of loop to use depending on the problem. For example, while loops are useful for executing statements until a<a class="moretag" href="http://talkbinary.com/programming/c/c-for-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In our previous tutorial, we learned about C++ While Loops. Today, we&#8217;ll learn about for loops. </p>
<p>For loops are different than while loops due to the way they function. This allows one to choose what type of loop to use depending on the problem. For example, while loops are useful for executing statements until a condition is met and a for loop is useful when you know how many iterations you must complete before you get your result. </p>
<p>A for loop works as follows:</p>
<ol>
<li>The variable(s) is initialized</li>
<li>The condition is checked. If true, execute statements, else terminate for loop</li>
<li>Execute statements within for loop</li>
<li>Update variables and return to step B</li>
</ol>
<p><span id="more-3170"></span><br />
<center><div id="attachment_3176" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/ForLoop.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/ForLoop-300x219.jpg" alt="For Loop" title="ForLoop" width="300" height="219" class="size-medium wp-image-3176" /></a><p class="wp-caption-text">Diagram of how a for loop works</p></div></center></p>
<p>The syntax is below:</p>

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

<h3 id="section-1">For Loop Example</h3>
<p>In our previous tutorial, we learned how to count forwards and backwards. In this tutorial, we&#8217;ll do something more interesting. We&#8217;ll compute the summation from 0 to <em>n</em>, n being user input. </p>
<p>To compute the summation, we simply add up all numbers starting from 0 to <em>n</em>. For example, the sum of 5 would be 0 + 1 + 2 + 3 + 4 + 5 = 15. Taking programming aside, the way we&#8217;d solve this step by step would be to have a running <em>sum</em> which we add to until we reach our desired <em>n</em>. Try programming this before you see 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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> n <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>  <span style="color: #666666;">// User input</span>
&nbsp;
<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Our running sum</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Sum: &quot;</span><span style="color: #008080;">;</span>
&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;">// For loop declaration</span>
<span style="color: #666666;">// We use the variable i to denote an iteration</span>
<span style="color: #666666;">// In this example, we execute the following loop</span>
<span style="color: #666666;">// if i is less than or equal to the user input </span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> i<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>For example, if <em>n</em> was 3, the following loop would execute in the following manner.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">0</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000040;">+</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span>
sum <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span>
&nbsp;
i <span style="color: #000080;">=</span> <span style="color: #0000dd;">4</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">4</span> <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">3</span>
<span style="color: #666666;">// End of For Loop</span></pre></div></div>

<h3 id="section-2">For loops advanced</h3>
<p>Some things to know about for loops:</p>
<ul>
<li>You don&#8217;t have to declare a variable within the initialization block of a for loop.<br />
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> i<span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> n<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span></pre></div></div>

</blockquote>
</li>
<li>You may have multiple initializations, conditions, and updates if you use a comma in between them.<br />
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, j <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> j<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span></pre></div></div>

</blockquote>
</li>
<li>You can break out of a for loop using the following statement:<br />
<blockquote>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">break</span><span style="color: #008080;">;</span></pre></div></div>

</blockquote>
<p>This is useful for when you encounter a special case in your loop using if statements.</li>
<li>Likewise you may use <em>continue;</em> statement.</li>
</ul>
<h3 id="section-3">Going further with For Loops</h3>
<p>Now that you understand for loops, why not try the following?</p>
<ol>
<li>Write the previous for loop using a while loop. This will make you understand when to use a for loop</li>
<li>Write a for loop that computes the factorial of <em>n</em>.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the factorial would be fac(3) = 1 x 2 x 3 = 6;</p></blockquote>
</li>
<li>Using two for loops (not nested), create a program that draws a triangle.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the appropriate triangle would be </p>
<p>*<br />
**<br />
***<br />
**<br />
*</p></blockquote>
</li>
<li>Using a nested for loop, create a program that will draw a right triangle.<br />
<blockquote><p>For example, if <em>n</em> is 3, then the appropriate triangle would be</p>
<p>*<br />
**<br />
***</p></blockquote>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-for-loop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>C++ If Else Statements</title>
		<link>http://talkbinary.com/programming/c/c-if-else-statements/</link>
		<comments>http://talkbinary.com/programming/c/c-if-else-statements/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 00:07:22 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[boolean operators]]></category>
		<category><![CDATA[c++ if statement]]></category>
		<category><![CDATA[comparison operators]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[if else statement]]></category>
		<category><![CDATA[if statement]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3058</guid>
		<description><![CDATA[In our previous tutorial, we learned about C++ input and output to make our programs more interactive. Well, today we are going to learn how to use If Then Else statements to allow us to control the flow of the program. This will allow us to execute code based on a given condition. Below is<a class="moretag" href="http://talkbinary.com/programming/c/c-if-else-statements/">&#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-input-and-output-cin-cout/">C++ input and outpu</a>t to make our programs more interactive. Well, today we are going to learn how to use If Then Else statements to allow us to control the flow of the program. This will allow us to execute code based on a given condition. Below is a simple example. </p>
<p><center><div id="attachment_3099" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExamples.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExamples-300x206.jpg" alt="" title="IfStatementExamples" width="300" height="206" class="size-medium wp-image-3099" /></a><p class="wp-caption-text">Example of Control Flow </p></div></center></p>
<h3 id="section-1">Boolean Comparison Operators</h3>
<p>Before we teach you how to write these statements, we&#8217;ll show you the boolean comparison operators you may use for creating conditions. These condition statements will return either TRUE or FALSE which are useful in <em>if statements</em>.<br />
<span id="more-3058"></span></p>
<blockquote><table width="80%">
<tr>
<td><strong>Operator </strong></td>
<td><strong>Example</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr>
<td>></td>
<td>a > b, a > 2, 4 > 2</td>
<td>Greater than</td>
</tr>
<tr>
<td>>=</td>
<td>a >=b, a >= 2, 4 >= 2</td>
<td>Greater than or equal to</td>
</tr>
<tr>
<td><</td>
<td>a < b, a < 2, 4 < 2</td>
<td>Less than</td>
</tr>
<tr>
<td><=</td>
<td>a <= b, a <=2, 4 <= 2</td>
<td>Less than or equal to</td>
</tr>
<tr>
<td>==</td>
<td>a == b, 2 == a</td>
<td>Equal to</td>
</tr>
<tr>
<td>!=</td>
<td>a != b, 2 != a</td>
<td>Does Not Equal to</td>
</tr>
</table>
</blockquote>
<p>You may use these boolean operators to compare data types of similar type. You may compare any of the <a href="http://talkbinary.com/programming/c/c-variables-and-data-types/">C++ Data Types</a> with each others whether they are literals such as 2, 3, &#8220;Hello&#8221;, or variables. If you compare mixed data types you will get different behavior. </p>
<p>Below are some examples of conditions. If they are literals, determine the conditions truth value.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> number
&nbsp;
<span style="color: #FF0000;">&quot;Hello&quot;</span> <span style="color: #000080;">==</span> <span style="color: #FF0000;">&quot;HELLO&quot;</span>
&nbsp;
<span style="color: #FF0000;">'a'</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'A'</span>
&nbsp;
<span style="color:#800080;">2.0</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">2</span></pre></div></div>

<p>You may also use the <strong>&#038;&#038;</strong> (and) and <strong>||</strong> (or) connectives in order to have multiple conditions. Below is an everyday example and after that some C++ examples.</p>
<p><center><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExample2.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatementExample2-300x300.jpg" alt="Examples for both &amp;&amp; and ||" title="IfStatementExample2" width="300" height="300" class="aligncenter size-medium wp-image-3098" /></a></center></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #666666;">// Returns true if both the 1st and 2nd conditions are true</span>
<span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> a <span style="color: #000040;">&amp;&amp;</span> b <span style="color: #000080;">&gt;</span> c
&nbsp;
<span style="color: #666666;">// Returns true if either the 1st or 2nd conditions are true</span>
<span style="color: #0000dd;">2</span> <span style="color: #000080;">&lt;</span> a <span style="color: #000040;">||</span> b <span style="color: #000080;">&gt;</span> c</pre></div></div>

<p>Finally, you may also use the <strong>!</strong> connective to make a condition have the opposite truth value. An example is below:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> a, b<span style="color: #008080;">;</span>
a <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
b <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// The following would return TRUE</span>
a <span style="color: #000080;">&lt;</span> b 
&nbsp;
<span style="color: #666666;">// The following would return FALSE because the ! </span>
<span style="color: #666666;">// negates the truth value between the parenthesis</span>
<span style="color: #000040;">!</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">&lt;</span> b <span style="color: #008000;">&#41;</span></pre></div></div>

<h3 id="section-2">C++ If Statement</h3>
<p>We will now start by showing you the syntax for writing an if statement.</p>
<blockquote><pre>
if ( condition ) { 

statements ...

}</pre>
</blockquote>
<p>Now, we&#8217;ll actually put it to some use. In our previous tutorial we learned about user input which will help us make things a lot more interesting. Let&#8217;s say we wanted to create a program that asks the user to guess a number and we&#8217;ll simply reply if it was the correct one. We&#8217;d do it in the following manner.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> magicNumber <span style="color: #000080;">=</span> <span style="color: #0000dd;">27</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> guess<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Guess my magic number: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the code above, the statement inside the if clause would execute if the condition were true. Therefore, if the user chooses our magic number <strong>27</strong>, our program will congratulate the user.</p>
<p><center><div id="attachment_3080" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfStatement-300x206.jpg" alt="" title="IfStatement" width="300" height="206" class="size-medium wp-image-3080" /></a><p class="wp-caption-text">If Statement Diagram</p></div></center></p>
<h3 id="section-3">C++ If Else Statement</h3>
<p>Well, what if they didn&#8217;t? We would want to give them another message saying it was incorrect. Your first guess might be making another if statement but we can use the <em>else</em> statement. </p>
<blockquote><pre>
if ( condition ) { 

statements ...

} else { 

statements ...

}</pre>
</blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// .. Previous code</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Incorrect!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In the previous code, if the user guess the number correctly, the program would congratulate the user. If the user did not guess the number correctly, it would display Incorrect!</p>
<p><center><div id="attachment_3079" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfElseStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfElseStatement-300x209.jpg" alt="" title="IfElseStatement" width="300" height="209" class="size-medium wp-image-3079" /></a><p class="wp-caption-text">If Else Statement</p></div></center></p>
<h3 id="section-4">C++ If Else If Else Statement</h3>
<p>Now, what if we wanted to actually let them know if their guess was too high or too low if it was incorrect? We can then use the <em>else if</em> statements. </p>
<blockquote><pre>
if ( condition ) { 

statements ...

} else if ( condition ) { 

statements ...

} else { 

statements ...

}</pre>
</blockquote>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// .. Previous code</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! Correct guess!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> magicNumber <span style="color: #000080;">&gt;</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your guess is too low!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Your guess is too high!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><center><div id="attachment_3078" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/IfElseIfElseStatement.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/IfElseIfElseStatement-300x256.jpg" alt="" title="IfElseIfElseStatement" width="300" height="256" class="size-medium wp-image-3078" /></a><p class="wp-caption-text">If Else Else If Statements</p></div></center></p>
<h3 id="section-5">Nesting If Statements</h3>
<p>You may also have nest if statements within if statements. An example is 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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
number <span style="color: #000080;">=</span> <span style="color: #0000dd;">25</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> number <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Now try guessing the second number!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #666666;">// Guess a second number</span>
     number <span style="color: #000080;">=</span> <span style="color: #0000dd;">56</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> guess<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> number <span style="color: #000080;">==</span> guess <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
         <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Congratulations! You guess two numbers correctly!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
         <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Try next time!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Incorrect!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-6">Overview of If Statements</h3>
<p>Some things to know about if statements are mentioned below.</p>
<ol>
<li>Remember, the <em>else</em> statement is optional and you may have zero, one, or more <em>else if</em> statements. </li>
<li>For good practice always indent or more formally known as tab your code when its within an <em>if</em> statement. That way it&#8217;s easy to follow. Imagine going over nested if statements that don&#8217;t have tabs? You would have a hard time figuring out which statements belong where.

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">&gt;</span> b <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> c <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> 
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;No!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> 
<span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span> 
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;What?&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

</li>
<li>If you only have one statement within an <em>if statement</em> you may omit the {}&#8217;s. For good practice, just keep them. It&#8217;s easier for someone else to go back to read and add to your code.

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">==</span> b <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> 
&nbsp;
<span style="color: #666666;">// Is the same as</span>
&nbsp;
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> a <span style="color: #000080;">==</span> b <span style="color: #008000;">&#41;</span> 
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Yes!&quot;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

</li>
</ol>
<h3 id="section-7">Going further with If Statement</h3>
<p>Now that you know the basics of If Statements, why not do the following?</p>
<ol>
<li>Create an if statement that contains a condition that is always true.</li>
<li>Are &#8220;Hello&#8221; and &#8220;HELLO&#8221; equivalent? What about the char &#8216;a&#8217; and a string &#8220;a&#8221;?</li>
<li>Create a 3rd level deep if statement. The previous example shown was nested 2x.</li>
</ol>
<p>Keep patient, in our next tutorial we will learn about for loops and then while loops. Once we get a good handle of control flow, we can make some interesting programs.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-if-else-statements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Input and Output ( cin, cout )</title>
		<link>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/</link>
		<comments>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 07:54:20 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ cin]]></category>
		<category><![CDATA[C++ cout]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[standard input]]></category>
		<category><![CDATA[standard output]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3022</guid>
		<description><![CDATA[In our previous tutorial, we learned about the C++ String datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal. C++ Standard Output<a class="moretag" href="http://talkbinary.com/programming/c/c-input-and-output-cin-cout/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In our previous tutorial, we learned about the <a href="http://talkbinary.com/programming/c/c-string/">C++ String</a> datatype. Until know, our C++ programs haven&#8217;t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal.</p>
<h3 id="section-1">C++ Standard Output</h3>
<p>If you recall, we have been using the <em>cout</em> object to display information on to the screen. Below, I&#8217;ll describe various ways to output strings, the contents of variables, expressions, and more.</p>
<h4 id="section-2">Basic output with endl</h4>
<p>Below is a simple example of printing out the string &#8220;Hello, World!&#8221; to the terminal accompanied by a newline.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We first print out Hello World </span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We then add the following to create a new line</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><center><div id="attachment_3028" class="wp-caption alignnone" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/08/gvimandterminal-300x143.jpg" alt="" title="gvimandterminal" width="300" height="143" class="size-medium wp-image-3028" /></a><p class="wp-caption-text">The previous example is demonstrated above.</p></div></center></p>
<h4 id="section-3">Basic output with variables</h4>
<p>Now, we are going to display the contents of a variable. Notice how we can output more than one item with a single <em>cout</em> object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> num <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-4">Basic output with math expressions</h4>
<p>We can also output arithmetic expressions. This by the way, is something you will hardly ever use but its good to know anyways since you&#8217;ll always want to save the result of an expression in a variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">5</span> <span style="color: #000040;">*</span> <span style="color: #0000dd;">4</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">3</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h4 id="section-5">Basic output with boolean expressions</h4>
<p>Finally, we will demonstrate a simple example with a boolean expression. This will not print out <em>true</em> or <em>false</em>. It will print out either a <em>1</em> for true, or <em>0</em> for false. You also need the parenthesis, else the < operator will give you an error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span>  <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-6">C++ Standard Input</h3>
<p>For standard output we used the object <em>cout</em> and for standard input, we will use the object <em>cin</em>. Before we start with examples, there is are a couple of things you must know. </p>
<blockquote><ul>
<li>User input is usually done through the keyboard and the terminal. Your program will halt at each user input and keep executing when the user enters a value and then presses enter</li>
<li>The <em>cin</em> object will read in the type of object you tell it to. Therefore, if you wish to have a char, it will read a char. Same with an integer, float, and a string. If you mix things up, you will receive undesired behavior</li>
<li>The user might provide you with invalid values and therefore require you to do error checking. We will leave this for a later topic.</li>
</ul>
</blockquote>
<h4 id="section-7">Reading in a value</h4>
<p>Below is an example of reading in a single and multiple values. Try it by changing the object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// We declare and initialize the variable num to -1</span>
<span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We prompt the user for input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> num<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We declare three strings</span>
string a, b, c<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// We read three strings from input</span>
<span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> a <span style="color: #000080;">&gt;&gt;</span> b <span style="color: #000080;">&gt;&gt;</span> c<span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-8">Going further with input and output</h3>
<p>Now that you understand the basics of input and output why not do the following?</p>
<ol>
<li>Prompt the user for his First Name, Last Name, Age, City, and State in the following fashion.</p>
<pre lang="">
Enter your first name:
Enter your last name:
Enter your age:
Enter your city:
Enter your state:</pre>
<p><br/><br />
Then, output the results in the following fashion. </p>
<pre lang="">
Hello <strong>firstname lastname</strong>!
I see you are <strong>age</strong> years old and live in <strong>city</strong>, <strong>state</strong>.</pre>
</li>
<li>Now try declaring a variable without initializing it. Then output it. What happened?</li>
<li>Try reading in an int value, but give it a string. Output the variable and what happens?</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-input-and-output-cin-cout/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ Variables and Data Types</title>
		<link>http://talkbinary.com/programming/c/c-variables-and-data-types/</link>
		<comments>http://talkbinary.com/programming/c/c-variables-and-data-types/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 18:22:57 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ bool]]></category>
		<category><![CDATA[C++ char]]></category>
		<category><![CDATA[C++ data types]]></category>
		<category><![CDATA[C++ double]]></category>
		<category><![CDATA[C++ float]]></category>
		<category><![CDATA[C++ identifiers]]></category>
		<category><![CDATA[C++ int]]></category>
		<category><![CDATA[C++ variables]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2912</guid>
		<description><![CDATA[In our previous C++ Hello World tutorial, we learned how to output &#8220;Hello World&#8221; to the terminal. Now, we are going to learn how to create variables that contain different types of information in memory so we can manipulate these variables throughout our program. C++ Variables Variables are created through the use of identifiers. An<a class="moretag" href="http://talkbinary.com/programming/c/c-variables-and-data-types/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In our previous <a href="http://talkbinary.com/programming/c/c-hello-world/">C++ Hello World</a> tutorial, we learned how to output &#8220;Hello World&#8221; to the terminal. Now, we are going to learn how to create variables that contain different types of information in memory so we can manipulate these variables throughout our program.</p>
<h3 id="section-1">C++ Variables</h3>
<p>Variables are created through the use of identifiers. An <em>identifier</em> is a sequence of one or more digits, letters, or the underscore character. It also may not be a reserved word, have two consecutive underscores, or begin with a digit. A reserved word is simply a keyword that is reserved by the language such as: int, double, string.</p>
<p>Below, is an example of the creation and manipulation of variables.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Below we are creating two different variables of type int</span>
<span style="color: #0000ff;">int</span> num <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> num2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Below we are creating a variable of type in that stores the sum of the </span>
<span style="color: #666666;">// previous two variables.</span>
<span style="color: #0000ff;">int</span> sum <span style="color: #000080;">=</span> num <span style="color: #000040;">+</span> num2<span style="color: #008080;">;</span></pre></td></tr></table></div>

<p><span id="more-2912"></span></p>
<h3 id="section-2">C++ Fundamental Data Types</h3>
<p>For now, we will discuss a couple of the fundamental data types the C++ language has to offer. Later, we&#8217;ll learn how to create our own through the use of classes.</p>
<table id="hor-minimalist-a">
<thead>
<tr>
<th scope="col">Variable</th>
<th scope="col">Example</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>int</td>
<td>1, 2, 10, 999, -562</td>
<td>A positive or negative integer value</td>
</tr>
<tr>
<td>double</td>
<td>1, 1.2, -1.2</td>
<td>A floating-point variable</td>
</tr>
<tr>
<td>bool</td>
<td>true, false</td>
<td>Either true or false, useful for conditions</td>
</tr>
<tr>
<td>char</td>
<td>a, b, 2, 1</td>
<td>A single character</td>
</tr>
</tbody>
</table>
<h3 id="section-3">Declaring variables</h3>
<p>In order to declare a variable, you need to specify the type of variable, identifier, and initialize it if you wish. Below are a couple of examples.</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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Declaring and initializing variables differently</span>
<span style="color: #0000ff;">int</span> num1 <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> num2<span style="color: #008080;">;</span>
num2 <span style="color: #000080;">=</span> <span style="color:#800080;">2.5</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Declaring a list of variables</span>
<span style="color: #0000ff;">int</span> a, b, c, d<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Setting a boolean value</span>
<span style="color: #0000ff;">bool</span> val <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// When you declare a character, you need to use the ''s</span>
<span style="color: #0000ff;">char</span> e <span style="color: #000080;">=</span> <span style="color: #FF0000;">'b'</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Some things to take into consideration are below:</p>
<ul>
<li>Uninitialized variables is a bad practice. If you forget, and try to use them later throughout your program they will contain garbage and will lead to undefined behavior for your program</li>
</ul>
<h3 id="section-4">Example of C++ Program with Variables</h3>
<p>Below is a simple example of a program that uses what we just learned. Try it out for yourself and try new things as well.</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
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
&nbsp;
     <span style="color: #666666;">//Outputs to the terminal, followed by a newline character</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Welcome!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Declaring some variables and initializing them</span>
     <span style="color: #0000ff;">int</span> a, b, c<span style="color: #008080;">;</span>
     a <span style="color: #000080;">=</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
     b <span style="color: #000080;">=</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
     c <span style="color: #000080;">=</span> a <span style="color: #000040;">+</span> b<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Outputting c to the terminal as well as a new line</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;The sum of a and b is: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> c <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<h3 id="section-5">Further practice</h3>
<p>You need further practice? Don&#8217;t worry, in the next few tutorials, we&#8217;ll learn how to use the different variable types you learned today. Just as long as you understand the material in this tutorial you are doing fine. Have any questions? Post them below.</p>
<p>In the next tutorial, we&#8217;ll be learning about global and constant variables as well as the scope of variables.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-variables-and-data-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>C++ Hello World</title>
		<link>http://talkbinary.com/programming/c/c-hello-world/</link>
		<comments>http://talkbinary.com/programming/c/c-hello-world/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 15:40:29 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Basic Structure of C++ Program]]></category>
		<category><![CDATA[C++ Hello World]]></category>
		<category><![CDATA[C++ Program]]></category>
		<category><![CDATA[hello world]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2867</guid>
		<description><![CDATA[Hello World in C++ The Hello World program is introduced to new programmers as its the simplest program to write in many languages (except GUIs). The Hello World program we&#8217;ll be writing in C++ will display &#8220;Hello World!&#8221; to the terminal. By now you must have your C++ programming environment set up, if not check<a class="moretag" href="http://talkbinary.com/programming/c/c-hello-world/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">Hello World in C++</h3>
<p>The <strong>Hello World</strong> program is introduced to new programmers as its the simplest program to write in many languages (except GUIs). The Hello World program we&#8217;ll be writing in C++ will display &#8220;Hello World!&#8221; to the terminal.</p>
<p>By now you must have your C++ programming environment set up, if not check out <a href="http://talkbinary.com/programming/c/c-installation/">C++ Installation</a>.</p>
<h3 id="section-2">Writing your first program</h3>
<p>The easiest way to learn how to program is by demonstrating a simple example. Simply compile the following code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #666666;">// Will display Hello World to the terminal</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello World!&quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-2867"></span></p>
<h3 id="section-3">The basic structure of a C++ program</h3>
<p>So if you were successful, you should have <strong>Hello World</strong> displayed in your terminal. Below, I&#8217;ll go ahead and describe the basic structure of a C++ program. </p>
<p>The following tells the compiler to include the following library since we&#8217;ll be using some of its functionality in our program.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span></pre></div></div>

<p>We&#8217;ll save namespaces for a later tutorial as its a more advanced feature. For now, just know that functionality found in iostream is declared under a namespace <em>std</em>. This will be something that will be found in most of your programs.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span></pre></div></div>

<p>The following is a functional declaration which we&#8217;ll describe later. The purpose of the main function is that it points to where your C++ program will start executing always. The {}&#8217;s simply encapsulate the body of our program.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>The following are known as comments. They are useful as notes to the programmer and others who wish to edit the code later on. Their purpose is to describe the functionality of a certain block of code. Best practice is to have clear and concise comments to let the reader know what to expect.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Will display Hello World to the terminal</span></pre></div></div>

<p>The following is known as a C++ statement as it terminated with a semicolon (;). The following <em>cout</em> statement belongs to the <em><iostream> library </em>and the <em>std namespace</em>. The <em>cout</em> statement is feeding the string &#8220;Hello World!&#8221; to standard output in which our case is usually a terminal.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello World!&quot;</span><span style="color: #008080;">;</span></pre></div></div>

<p>As mentioned before, our main function which expects an <em>int</em> value in return. The 0 denotes that the program terminated successfully.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span></pre></div></div>

<h3 id="section-4">Overview of C++ program structure</h3>
<p>Described above is the most basic of C++ programs. For the next couple of tutorials, we&#8217;ll be using the following format. Make sure you understand the basic gist as its imperative you understand the structure of a C++ program.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++ Installation</title>
		<link>http://talkbinary.com/programming/c/c-installation/</link>
		<comments>http://talkbinary.com/programming/c/c-installation/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 17:30:43 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Compiler]]></category>
		<category><![CDATA[C++ IDE]]></category>
		<category><![CDATA[C++ Installation]]></category>
		<category><![CDATA[Programming in C++]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2557</guid>
		<description><![CDATA[What do you need? You have two options. You can write your code, compile, and run it using an Integrated Development Environment (IDE), or you can write your code in your favorite editor and compile it within a terminal. In order to receive the benefits of both options, try them both to see what you<a class="moretag" href="http://talkbinary.com/programming/c/c-installation/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h3 id="section-1">What do you need?</h3>
<p>You have two options. You can write your code, compile, and run it using an <strong>Integrated Development Environment</strong> (IDE), or you can write your code in your favorite editor and compile it within a terminal. In order to receive the benefits of both options, try them both to see what you like using the most.</p>
<p>You might even want to try programming in a different operating system such as Ubuntu. To figure out how to do that make sure to check out <a href="http://talkbinary.com/linux/3-methods-for-installing-ubuntu-using-windows/">3 Methods for Installing Ubuntu using Windows</a>.</p>
<h3 id="section-2">Should I use an IDE or a plain &#8216;ol editor and compiler?</h3>
<p>Personally, using an IDE is the easiest option. An IDE is a software environment that provides tools and resources to make programming for you easier. An IDE usually contains a source code editor, compiler. and a debugger. It also may have many neat features like auto-complete, syntax highlighting, and can point you to the exact line where the syntax or compile time errors were found. The only downside is that downloading an IDE with a compiler can be a pretty large download (>~75mb).<br />
<span id="more-2557"></span><br />
<center><div id="attachment_2833" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/ide.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/ide-300x237.jpg" alt="" title="ide" width="300" height="237" class="size-medium wp-image-2833" /></a><p class="wp-caption-text">Using Eclipse (IDE)</p></div></center></p>
<p>The other option is to write code in your favorite editor and compile it within a terminal (~10mb to download). This is the path many students in universities are taught in their introductory courses. I learned this way and sometimes prefer it over using an IDE. This method is sometimes more simpler since you don&#8217;t need to create an entire project in an IDE to simply write some code out. </p>
<p><center><div id="attachment_2832" class="wp-caption aligncenter" style="width: 310px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/gvimandterminal.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/gvimandterminal-300x143.jpg" alt="" title="gvimandterminal" width="300" height="143" class="size-medium wp-image-2832" /></a><p class="wp-caption-text">Using gvim and the terminal</p></div> </center></p>
<h3 id="section-3">Getting started with C++ and an IDE</h3>
<p>You can get started writing C++ with an IDE on any platform such as Windows, Linux, and Mac. Below are a list of IDEs you may be interested in. They are ordered in my preference of usage. Make sure you download a version with a compiler. </p>
<p><a href="http://www.eclipse.org/downloads/">Eclipse</a> &#8211; Windows/Mac/Linux<br />
<a href="http://www.codeblocks.org/downloads/26">CodeBlocks</a> &#8211; Windows/Mac/Linux<br />
<a href="http://netbeans.org/">Netbeans</a> &#8211; Windows/Mac/Linux</p>
<p>Once you download the IDE, simply create the default C++ project. That should start you with the famous &#8220;Hello World&#8221; written for you. All you need to do is hit Build &#038; Run and you should see the output in the terminal window like the picture of the IDE above.</p>
<h3 id="section-4">Getting started with C++ with a compiler, terminal, and source editor</h3>
<p>1. The first step is to download your compiler. </p>
<p><strong>Mac</strong> &#8211; Correct me if I&#8217;m wrong, but you need to download <a href="http://developer.apple.com/technologies/xcode.html">Xcode</a>.<br />
<strong>Linux</strong> &#8211; Using the Synaptic Package Manager or the equivalent, download <em>build-essential</em>.</p>
<p>2. Open up your favorite text editor. For now gedit (Linux) or TextEdit(Mac) is fine. Now copy and paste the following code. Make sure you save it as main.cpp (cpp is the common extension of C++ files).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>3. Open up your terminal, and navigate to <strong>main.cpp</strong>&#8216;s location. To navigate within a terminal you can use the following commands.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">//Change Directory to specified folder</span>
cd foldername
&nbsp;
<span style="color: #666666;">//Go up one directory </span>
cd .. 
&nbsp;
<span style="color: #666666;">// List contents of directory</span>
ls</pre></div></div>

<p>4. To compile your program, simply type in the following.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">g<span style="color: #000040;">++</span> main.<span style="color: #007788;">cpp</span></pre></div></div>

<p>5. Now that your program is compiled, run it with the following.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">.<span style="color: #000040;">/</span>a.<span style="color: #007788;">out</span></pre></div></div>

<p><center><div id="attachment_2840" class="wp-caption aligncenter" style="width: 333px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/terminalnavigation.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/terminalnavigation.jpg" alt="" title="terminalnavigation" width="323" height="318" class="size-full wp-image-2840" /></a><p class="wp-caption-text">Shows steps 4 - 6 </p></div></center></p>
<p>6. If done correctly, you should see a &#8220;Hello Word&#8221; outputted to the terminal!</p>
<h3 id="section-5">Overview of C++ Installation</h3>
<p>Did you have any problems with any of the steps? Let me know and I&#8217;ll address your issue. I am here to help. Once you learn once, you&#8217;ll be compiling and running your C++ programs left and right. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-installation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Recursion</title>
		<link>http://talkbinary.com/programming/c/c-recursion/</link>
		<comments>http://talkbinary.com/programming/c/c-recursion/#comments</comments>
		<pubDate>Tue, 20 Jul 2010 16:54:41 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[activation records]]></category>
		<category><![CDATA[Base Cases]]></category>
		<category><![CDATA[c++ recursion]]></category>
		<category><![CDATA[Infinite Recursion]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[recursive function]]></category>
		<category><![CDATA[Runtime stack]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2813</guid>
		<description><![CDATA[Recursion in C++ is a function with a set of rules designed to reduce a problem into a smaller one called a base case. This may be referred to as the divide and conquer method since the idea is to solve a complex problem by solving the smaller instances of it. A classic example is<a class="moretag" href="http://talkbinary.com/programming/c/c-recursion/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Recursion in C++ is a function with a set of rules designed to reduce a problem into a smaller one called a base case. This may be referred to as the <em>divide and conquer</em> method since the idea is to solve a complex problem by solving the smaller instances of it. A classic example is the factorial function, which we&#8217;ll discuss later. </p>
<h3 id="section-1">Recursive function that displays numbers</h3>
<p>For our first example, let&#8217;s create a recursive function that displays a number. In order to do this, we create a function that prints a number to output, then we call itself.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Recursive call</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunately, the previous example displays behavior known as <em>infinite recursion</em> on any call. Depending on the environment you are programming in, you might receive a <strong>segmentation fault</strong>. This happens because each <em>activation record</em> of a function is pushed onto the run-time stack. When you have a function that runs indefinitely, you are bound to run out of space.<br />
<span id="more-2813"></span><br />
<center><div id="attachment_2822" class="wp-caption aligncenter" style="width: 144px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/RecursionInfinite.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/RecursionInfinite-134x300.jpg" alt="" title="RecursionInfinite" width="134" height="300" class="size-medium wp-image-2822" /></a><p class="wp-caption-text">Example of Infinite Recursion</p></div></center></p>
<h3 id="section-2">Base Cases in Recursion</h3>
<p>In order to eliminate <em>infinite recursion</em> we introduce a <em>base case</em>. A <strong>base case</strong> is a condition that determines when to terminate a recursive function. For our previous example, we&#8217;ll introduce the following base case and also make sure we are reducing our input into a smaller case each call (otherwise, we&#8217;d never reach our base case).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// Base case</span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> 
          <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Recursive call that reduces input into a smaller case</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> 
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Below is an example of the output of executing such a function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">printnum<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">5</span> <span style="color: #0000dd;">4</span> <span style="color: #0000dd;">3</span> <span style="color: #0000dd;">2</span> <span style="color: #0000dd;">1</span></pre></div></div>

<p><center><div id="attachment_2823" class="wp-caption aligncenter" style="width: 301px"><a href="http://talkbinary.com/wp-content/uploads/2010/07/RecursionPrintNum.jpg"><img src="http://talkbinary.com/wp-content/uploads/2010/07/RecursionPrintNum-291x300.jpg" alt="" title="RecursionPrintNum" width="291" height="300" class="size-medium wp-image-2823" /></a><p class="wp-caption-text">Example of printnum(3) and output to the console</p></div></center></p>
<p>Now, what happens if we execute <em>printnum(-2)</em>? We will never reach our base case and thus receive <em>infinite recursion</em>. In order to avoid this behavior, its essential that we create robust base cases that allow our recursive functions to terminate. </p>
<p>The following is the modified version of our recursive function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> printnum<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// Improved base case </span>
     <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> n <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> 
          <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span>
     <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> n <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #666666;">// Recursive call that reduces input into a smaller case</span>
     printnum<span style="color: #008000;">&#40;</span>n<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
     <span style="color: #0000ff;">return</span><span style="color: #008080;">;</span> 
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<h3 id="section-3">Things to remember about C++ Recursion</h3>
<p>Below are some things that you should take into consideration when solving recursive problems in C++.</p>
<blockquote><ul>
<li>Recursion in C++ is a very difficult subject. It takes dedication in order to understand and master the subject.</li>
<li>The best way to solve recursive functions is to write down the problem on a piece of paper and identify how you are going to break down the problem into a smaller instance and when to terminate (base cases).</li>
<li>A good way to visualize recursion is to draw the <em>activation records</em> by drawing the run time stack of a simple recursive call</li>
</ul>
</blockquote>
<h3 id="section-4">Going further with C++ Recursion</h3>
<p>Now that you know the basic concept, the best way to learn recursion is by solving problems.</p>
<p>Next: <a href="http://talkbinary.com/programming/c/recursion-examples/">C++ Recursion Examples</a></p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-recursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ Introduction</title>
		<link>http://talkbinary.com/programming/c/c-introduction/</link>
		<comments>http://talkbinary.com/programming/c/c-introduction/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 05:40:34 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++ examples]]></category>
		<category><![CDATA[C++ Introduction]]></category>
		<category><![CDATA[C++ Programming]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=2543</guid>
		<description><![CDATA[What You Should Already Know Absolutely nothing. This tutorial is assuming you have no prior programming knowledge. Therefore, it would be serve as an excellent resource for people interested in learning to program for the very first time, or even people wanting to brush up on some skills. This tutorial is aimed at setting up<a class="moretag" href="http://talkbinary.com/programming/c/c-introduction/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<h2 id="section-1">What You Should Already Know</h2>
<p>Absolutely nothing. This tutorial is assuming you have no prior programming knowledge. Therefore, it would be serve as an excellent resource for people interested in learning to program for the very first time, or even people wanting to brush up on some skills. This tutorial is aimed at setting up your programming environment and teaching you the skills necessary to master beginner up to advanced topics.</p>
<h2 id="section-2">What is C++?</h2>
<p>C++ is a very popular general purpose programming language. It is taught at major universities and is widely used in the software industry. It may be used for network application, applications, embedded software, video games, and many other purposes. </p>
<blockquote><h3 id="section-3">Examples of C++ Programs</h3>
<p>Let this help convince you. According to this page on <a href="http://www2.research.att.com/~bs/applications.html">C++ Applications</a> there is a wide variety of programs written in C++. Below are a few examples.</p>
<ul>
<li>Adobe Systems such as Photoshop, Image Ready, Illustrator, Acrobat, and more.</li>
<li>Amazon.com uses it for its software for large-scale e-commerce</li>
<li>Apple uses it for their Finder, iPod user interface, and more</li>
<li>Google uses it for its web search engine, Chrome browser, and more</li>
<li>Intel</li>
<li>IBM</li>
<li>HP</li>
<li>and the list goes on&#8230;</li>
</ul>
<p>You see? C++ is a very popular language so its nice to know.</p></blockquote>
<p><span id="more-2543"></span></p>
<h2 id="section-4">Why C++?</h2>
<p>First of all, it&#8217;s fairly easy anyone can learn. Best of all, you&#8217;ll be learning about object orientated programming, basic control flow, data structures, functions, classes, recursion, and a wide variety of other topics. The skills you will develop along the way will make it easy for you to transition from one language to another later on as well. So don&#8217;t worry if you think you won&#8217;t put C++ to great use.  You can, and worst case if you don&#8217;t, you can take your new found knowledge and simply learn how to do what you know, but in a different language. </p>
<h2 id="section-5">Structure of tutorial</h2>
<p>The following tutorials will mainly consist of short tutorials on the various subjects. That way you can learn at your own pace and don&#8217;t get overwhelmed. There will be plenty of examples you could try out in order to learn the language. </p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-introduction/feed/</wfw:commentRss>
		<slash:comments>4</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-10 15:36:14 -->
