<?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; if else statments</title>
	<atom:link href="http://talkbinary.com/tag/if-else-statments/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Advanced Control Flow &#8211; 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>
	</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-12 01:09:39 -->
