<?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>Fri, 30 Jul 2010 18:22:57 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <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 } [...]]]></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>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>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><h3  class="related_post_title">Related Posts</h3><ul
class="related_post"><li><a
href="http://talkbinary.com/programming/c/basic-control-flow/" title="Basic Control Flow">Basic Control Flow</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-variables-and-data-types/" title="C++ Variables and Data Types">C++ Variables and Data Types</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-hello-world/" title="C++ Hello World">C++ Hello World</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-installation/" title="C++ Installation">C++ Installation</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion/" title="C++ Recursion">C++ Recursion</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-introduction/" title="C++ Introduction ">C++ Introduction </a> (2)</li><li><a
href="http://talkbinary.com/programming/c/recursion-examples/" title="C++ Recursion Examples">C++ Recursion Examples</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-logarithm/" title="C++ Recursion &#8211; Logarithm">C++ Recursion &#8211; Logarithm</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-palindrome/" title="C++ Recursion &#8211; Palindrome">C++ Recursion &#8211; Palindrome</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-greatest-common-divisor/" title="C++ Recursion &#8211; Greatest Common Divisor ">C++ Recursion &#8211; Greatest Common Divisor </a> (0)</li></ul>]]></content:encoded> <wfw:commentRss>http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/feed/</wfw:commentRss> <slash:comments>3</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, [...]]]></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 class="step">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 class="step">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 class="step">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 class="step">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><h3  class="related_post_title">Related Posts</h3><ul
class="related_post"><li><a
href="http://talkbinary.com/programming/c/basic-control-flow-continued/" title="Basic Control Flow Continued">Basic Control Flow Continued</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/advanced-control-flow-switch-statements/" title="Advanced Control Flow &#8211; Switch Statements">Advanced Control Flow &#8211; Switch Statements</a> (3)</li><li><a
href="http://talkbinary.com/programming/c/c-variables-and-data-types/" title="C++ Variables and Data Types">C++ Variables and Data Types</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-hello-world/" title="C++ Hello World">C++ Hello World</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-installation/" title="C++ Installation">C++ Installation</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion/" title="C++ Recursion">C++ Recursion</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-introduction/" title="C++ Introduction ">C++ Introduction </a> (2)</li><li><a
href="http://talkbinary.com/programming/c/recursion-examples/" title="C++ Recursion Examples">C++ Recursion Examples</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-logarithm/" title="C++ Recursion &#8211; Logarithm">C++ Recursion &#8211; Logarithm</a> (0)</li><li><a
href="http://talkbinary.com/programming/c/c-recursion-palindrome/" title="C++ Recursion &#8211; Palindrome">C++ Recursion &#8211; Palindrome</a> (0)</li></ul>]]></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/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 24/68 queries in 0.312 seconds using disk

Served from: talkbinary.com @ 2010-07-31 09:43:34 -->