<?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; bool</title>
	<atom:link href="http://talkbinary.com/tag/bool/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>Basic Control Flow Continued</title>
		<link>http://talkbinary.com/programming/c/basic-control-flow-continued/</link>
		<comments>http://talkbinary.com/programming/c/basic-control-flow-continued/#comments</comments>
		<pubDate>Thu, 26 Jun 2008 01:14:38 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[bool]]></category>
		<category><![CDATA[else]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[if-else]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=135</guid>
		<description><![CDATA[By this time you should be familiar with the basics behind If and Else if statements. Below you&#8217;ll see more examples and more ways to use Control Flow in your programming. 1. You&#8217;ll learn more advanced ways of manipulating control flow in your program. 2. You&#8217;ll also learn how to declare and initialize boolean variables.<a class="moretag" href="http://talkbinary.com/programming/c/basic-control-flow-continued/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>By this time you should be familiar with the basics behind If and Else if statements. Below you&#8217;ll see more examples and more ways to use Control Flow in your programming.<br/><br />
1. You&#8217;ll learn more advanced ways of manipulating control flow in your program.<br />
2. You&#8217;ll also learn how to declare and initialize boolean variables.<br />
<br/></p>
<h3 id="section-1">Boolean Variables</h3>
<p>Boolean variables are another built in data type in C++ that allow you to assign it either a TRUE, or FALSE value. What is this useful for? Let&#8217;s see below. (Declaring and assigning a value to a bool value is really similar to int, double, and strings).<br />
<br/><span id="more-135"></span></p>
<pre lang="c++">
bool success = FALSE;
int age;
cin >> age;

//If user entered a value between 0 and 120 we can assume they entered a correct value. If not, SUCCESS remains FALSE.
if ( age < 120 &#038;&#038; age > 0 ) success = TRUE; 

//If user either put a negative number or above 120 then we "assume" they lied about their age and terminate the program.
if ( !success ) return 0; 

cout << "Based on your age we can determine..." << endl;
//Do more stuff
</pre>
<p>In other words, if the user did satisfy our age requirement, we can continue with our program.<br/><br/></p>
<h3 id="section-2">Explanation:</h3>
<p>You may have also noticed I used the following:</p>
<pre lang="c++">if ( !success )</pre>
<p>This simply means if success is NOT TRUE (In other words, FALSE), the following return would execute thus terminating the program. <br/><br/></p>
<pre lang="c++">if ( success ) //Simple means, if SUCCESS is TRUE</pre>
<p><br/></p>
<p>If you also noticed, I included the following:</p>
<pre lang="c++">if ( age < 120 &#038;&#038; age > 0 )</pre>
<p></br>In other words, you may have more than one condition in your if statements. <br/><br />
You may use <strong>and</strong> or <strong>&#038;&#038;</strong> as well as <strong>or</strong> or <strong>||</strong>. <br/><br />
When you concatenate conditions with the <strong>AND</strong> operator it means in order for the if statement to execute, ALL conditions must be true. For <strong>OR</strong> only one condition must be met.<br />
<br/></p>
<h3 id="section-3">More advanced examples</h3>
<p>Try figuring out how this following code would work. Try it yourself. It's the best way to learn! Fill in the cases of course with code of your desire.<br />
<br/></p>
<pre lang="c++">
bool success = false; //Remember to always initialize bool variables
int num1, num2, num3;

//Some lines that modify the previous variables.

if ( success ) {
	if ( (num1 <= num2) || num3 > 100 ) {
	     //Case 1
	}
	else if ( num1 == num3 ) {
	     //Case 2
	}
	else {
	     //Case 3
	}
}
else { return 0; } 

//Since success, keep doing other code.
</pre>
<p><br/></p>
<h3 id="section-4">What now?</h3>
<p>Keep practicing! If you have any more questions feel free to ask below or join the forums and ask the community!</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/basic-control-flow-continued/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 12:27:05 -->
