<?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; control flow</title>
	<atom:link href="http://talkbinary.com/tag/control-flow/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++ Do While Loop</title>
		<link>http://talkbinary.com/programming/c/c-do-while-loop/</link>
		<comments>http://talkbinary.com/programming/c/c-do-while-loop/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 16:00:38 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ Do While Loop]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[Control Structure]]></category>
		<category><![CDATA[Do While Loop]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=3223</guid>
		<description><![CDATA[The Do While Loop is different from the While Loop for one reason only. The condition is checked after executing the statements. Therefore, this loop guarantees that the statements will be executed at least once. The syntax for the Do While Loop is below: do &#123; &#160; // Statements ... &#160; &#125; while &#40; condition<a class="moretag" href="http://talkbinary.com/programming/c/c-do-while-loop/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>The Do While Loop is different from the <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loop</a> for one reason only. The condition is checked after executing the statements. Therefore, this loop guarantees that the statements will be executed at least once. </p>
<p><center><div id="attachment_3224" class="wp-caption alignnone" style="width: 412px"><img src="http://talkbinary.com/wp-content/uploads/2010/08/DoWhile.jpg" alt="Do While Loop" title="DoWhile" width="402" height="377" class="size-full wp-image-3224" /><p class="wp-caption-text">Do While Loop</p></div></center></p>
<p>The syntax for the Do While Loop is below:</p>

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

<h3 id="section-1">Do While Example</h3>
<p>An example of a do while loop is below.</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;">string msg<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">do</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;Enter a word or 'exit' to quit: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> msg<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> msg <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;exit&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Do While Loops aren&#8217;t used as extensively as <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loops</a> and <a href="http://talkbinary.com/programming/c/c-for-loop/">For Loops</a> in practice due to their nature. They are only used for specific cases where the programmer needs the block of statements to be executed at least once. </p>
<h3 id="section-2">Going further with Do While Loops</h3>
<p>Now that you understand Do While Loops, <a href="http://talkbinary.com/programming/c/c-while-loop/">While Loops</a>, and <a href="http://talkbinary.com/programming/c/c-for-loop/">For Loops</a> why not try the following?</p>
<ol>
<li>Create a factorial program that uses a while, do while, and for loop. Do the same for summation. This should make you fully understand all loops and when to use them.</li>
</ol>
</li>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/c-do-while-loop/feed/</wfw:commentRss>
		<slash:comments>3</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>Advanced Control Flow &#8211; Switch Statements</title>
		<link>http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/</link>
		<comments>http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 18:35:48 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[advanced control flow]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[if else statments]]></category>
		<category><![CDATA[switch statements]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=728</guid>
		<description><![CDATA[A switch statement allows you to control the flow of your program similar to if and else statements, but can be easier for the user to understand. The Switch Statement The structure of a switch statement is the following: switch(expression) { case : statements break; case : statements break; //More cases... default : statements }<a class="moretag" href="http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>A switch statement allows you to control the flow of your program similar to if and else statements, but can be easier for the user to understand. </p>
<h3 id="section-1">The Switch Statement</h3>
<p>The structure of a switch statement is the following: </p>
<pre lang="c++"> switch(expression) {
    case <constant_expression>  :
    statements
    break;

    case <constant_expression> :
    statements
    break;

    //More cases...

    default :
    statements
}</pre>
<p><span id="more-728"></span><br />
The case to be executed is determined based on the value of the constant_expression. In other words, you may have an int, char, etc&#8230;They must be constants. The <strong>break</strong> statement once encountered breaks out of the switch statement. In other words, if you don&#8217;t include the break statement in one of your cases, it will simply execute the following cases until it reaches a break or the end of switch statement. The <strong>default</strong> case is simply all other cases not covered. </p>
<p>An example would be&#8230;</p>
<pre lang="c++"> cin >> number;
switch(number) {
     case 1:
     cout << "One\n";
     break;
     case 2:
     case 3:
     cout << "My favorite numbers...\n";
     break;
     default:
     cout << "What!?\n";
}</pre>
<p>In other words, if the user chooses 1, they will encounter case 1, if they choose 2 or 3, they receive "My favorite..." statement, else they receive "What!?".</p>
<h3 id="section-2">Another example comparing an if else statement with a switch statement</h3>
<p>Imagine a banking program in which the teller gives the program an integer value to perform a different action. Each action determines the reward the user receives. </p>
<p><strong>1</strong> - Gold Reward... Receives $100 reward plus all the other rewards<br />
<strong>2</strong> - Silver Reward... Received $50 reward plus the Bronze Reward<br />
<strong>3</strong> - Bronze Reward... Received $25 reward<br />
<strong>*</strong> - Deduct $100 from their account</p>
<p>*Denotes any other value</p>
<p>How would this look like with a if and else statement?</p>
<pre lang="c++">cin >> action;

if ( action == 1 ) {
     //Give Gold Award
     //Give Silver Reward
     //Give Bronze Reward }
else if ( action == 2 ) {
     //Give Silver Reward
     //Give Bronze Reward }
else if ( action == 3 ) {
     //Give Bronze Reward }
else {
     //Deduct $100 from their account }
}</pre>
<p>What about with a switch statement?</p>
<pre lang="c++"> cin >> action;

switch(action) {
     case 1:
     //Give Gold Reward
     case 2:
     //Give Silver Reward
     case 3:
     //Give Bronze Reward
     break;
     default:
     //Deduct $100 from their account
}</pre>
<p>In other words, once action satisfies one case, it keeps executing the statements until it reaches the <strong>break</strong> statement which breaks out of the switch statement. Wouldn't this be much easier to for a reader to understand? Most likely in certain cases.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Basic Control Flow</title>
		<link>http://talkbinary.com/programming/c/basic-control-flow/</link>
		<comments>http://talkbinary.com/programming/c/basic-control-flow/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 06:55:10 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[control flow]]></category>
		<category><![CDATA[else]]></category>
		<category><![CDATA[if]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=134</guid>
		<description><![CDATA[In the following tutorial, you&#8217;ll learn how to use if, and if-else statements. They are useful for deciding on what lines of code to execute based on values of previous variables in your program. The If Statement An example is as follows int x; if ( condition ) x = 50; //If condition returns TRUE,<a class="moretag" href="http://talkbinary.com/programming/c/basic-control-flow/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>In the following tutorial, you&#8217;ll learn how to use if, and if-else statements. They are useful for deciding on what lines of code to execute based on values of previous variables in your program.</p>
<p><br/></p>
<h3 id="section-1">The If Statement</h3>
<p>An example is as follows</p>
<pre lang="c++">
int x;
if ( condition ) x = 50; //If condition returns TRUE, assign 50 to X.
</pre>
<p>In other words, if the condition you provide returns a boolean value ( is a value that returns either TRUE or FALSE ) TRUE, then the following statements enclosed with {}&#8217;s are executed (no need if only one statement follows the if statement). <span id="more-134"></span><br />
<br/><br />
You may use the following operands in your conditions&#8230;<br />
== &#8211; EQUAL TO<br />
!=  &#8211; NOT EQUAL TO<br />
<   - LESS THAN<br />
<= - LESS THAN OR EQUAL TO<br />
>   &#8211;  GREATER THAN<br />
>= &#8211;  GREATER THAN OR EQUAL TO<br />
<br/>Here is an actual example.</p>
<pre lang="c++">
int number;
cin >> number;
if ( number == 50 ) {
cout << "Congratulations! You picked the right number!" << endl;
}
</pre>
<p>In other words, if the user typed in 50, the if statement will be true and print the following line to the screen.<br />
<br/></p>
<h3 id="section-2">The If and Else</h3>
<p>Let's say we want to output something if it was false.</p>
<pre lang="c++">
int number;
cin >> number;
if ( number == 50 ) {
cout << "Congratulations! You picked the right number!" << endl;
}
else {
cout << "Sorry! You were wrong!" << endl; //Second Statement
}</pre>
<p>Then, if the user didn't type in 50, it would output the second statement.<br />
<br/></p>
<h3 id="section-3">The If, Else-If, and Else</h3>
<p>What if we wanted to check other conditions as well? </p>
<pre lang="c++">
int number;
cin >> number;
if ( number == 50 ) {
cout << "Congratulations! You picked the right number!" << endl;
}
else if ( number > 50 ) {
cout << "Sorry! You were over!" << endl; //New Statement
}
else {
cout << "Sorry! You were way off!" << endl;
}</pre>
<p>In other words, this one has a different output if the user entered a number larger than 50! If the user didn't choose 50, or a number larger than fifty, then the last else statement would be executed.<br />
<br/><br />
Keep in mind that you may add multiple else if statements! Just remember to have good coding style in order for any future reader to understand what statements pertain to what if condition. The way my code is written is a really good way of styling your code as its easy to read and follow!<br />
<br/></p>
<h3 id="section-4">So what now?</h3>
<p>If you have trouble don't be afraid to post below any questions, comments, or concerns!<br />
<br/>In the next tutorial expect to learn how to use If, Else If, and Else statements in more detail!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/basic-control-flow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 20:55:30 -->
