<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>GeekScribes</title>
	
	<link>http://www.geekscribes.net/blog</link>
	<description>Bringing geekiness to the world</description>
	<lastBuildDate>Tue, 08 May 2012 20:14:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Geekscribes" /><feedburner:info uri="geekscribes" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><image><url>http://www.feedburner.com/fb/images/pub/fb_pwrd.gif</url></image><feedburner:emailServiceId>Geekscribes</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>SBMNet Transfer Woes</title>
		<link>http://feedproxy.google.com/~r/Geekscribes/~3/ibJnAspfVVM/</link>
		<comments>http://www.geekscribes.net/blog/2012/05/08/sbmnet-transfers-woes/#comments</comments>
		<pubDate>Tue, 08 May 2012 19:24:05 +0000</pubDate>
		<dc:creator>InF</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Mauritius]]></category>
		<category><![CDATA[Design Flaws]]></category>
		<category><![CDATA[Online Services]]></category>
		<category><![CDATA[SBM]]></category>

		<guid isPermaLink="false">http://www.geekscribes.net/blog/?p=1788</guid>
		<description><![CDATA[A friend of mine just sent me this screenshot of SBMNet, State Bank of Mauritius (SBM)&#8217;s online banking facility. She wanted to transfer some money from her account to a friend&#8217;s at another bank. She was then presented with the form below: SBM, that&#8217;s not how you design forms. Seriously, we don&#8217;t all have a [...]<p>This article comes from <a href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a href="http://www.geekscribes.net/blog/2012/05/08/sbmnet-transfers-woes/">SBMNet Transfer Woes</a></p>
]]></description>
			<content:encoded><![CDATA[<p>A friend of mine just sent me this screenshot of SBMNet, State Bank of Mauritius (SBM)&#8217;s online banking facility. She wanted to transfer some money from her account to a friend&#8217;s at another bank. She was then presented with the form below:</p>
<p style="text-align: center;"><a  href="http://www.geekscribes.net/blog/wp-content/uploads/2012/05/sbm-howitis.jpg" class="thickbox no_icon" rel="gallery-1788" title="SBM - Transfer Form"><img class="aligncenter size-large wp-image-1789" title="SBM - Transfer Form" src="http://www.geekscribes.net/blog/wp-content/uploads/2012/05/sbm-howitis-1024x582.jpg" alt="" width="630" height="358" /></a></p>
<p>SBM, that&#8217;s not how you design forms. Seriously, we don&#8217;t all have a degree in banking to know what your &#8220;Payee Id&#8221; is, what&#8217;s the difference between a Payee name and a Beneficiary Name and why you need four fields of Payment Details, intuitively called &#8220;Payment Details 1&#8243; to &#8220;Payment Details 4&#8243;. The sparse help messages are not helpful. At all.</p>
<p>Anyway, what&#8217;s Beneficiary Bank Address 2 and 3?! Come&#8217;on! It&#8217;s ridiculous! Fix your thing, before you can even claim to offer &#8220;online banking&#8221; on your adverts. It&#8217;s not useful.</p>
<p>Oh btw? The said friend? She&#8217;s decided to go in person to a bank branch to do the transfer. So much for your online services.</p>
<p>How it should be done? How about:</p>
<ul>
<li>Meaningful form names? Payment Details 1 is not helpful.</li>
<li>Contextual help when you hover on the forms?</li>
<li>Auto complete? Data-specific fields? Why should the user enter date? Can&#8217;t they choose from a dropdown?</li>
<li>Information overload! Use a couple of screens, with steps and a &#8220;next&#8221; button?</li>
<li>Or you know, just something like this</li>
</ul>
<p><img class="size-full wp-image-1790 aligncenter" title="SBM - form mockup" src="http://www.geekscribes.net/blog/wp-content/uploads/2012/05/sbm-howitshouldbe.jpg" alt="" width="360" height="234" /></p>
<p>Hopefully someone will see this and fix this, or at least suggest a better design proposal. :p</p>
<p>This article comes from <a  href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a  href="http://www.geekscribes.net/blog/2012/05/08/sbmnet-transfers-woes/">SBMNet Transfer Woes</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/sYmLRqrWKOshd3N8nts8-SUmBMw/0/da"><img src="http://feedads.g.doubleclick.net/~a/sYmLRqrWKOshd3N8nts8-SUmBMw/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/sYmLRqrWKOshd3N8nts8-SUmBMw/1/da"><img src="http://feedads.g.doubleclick.net/~a/sYmLRqrWKOshd3N8nts8-SUmBMw/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ibJnAspfVVM:DK7qmp8MqcI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ibJnAspfVVM:DK7qmp8MqcI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ibJnAspfVVM:DK7qmp8MqcI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ibJnAspfVVM:DK7qmp8MqcI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ibJnAspfVVM:DK7qmp8MqcI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ibJnAspfVVM:DK7qmp8MqcI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ibJnAspfVVM:DK7qmp8MqcI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ibJnAspfVVM:DK7qmp8MqcI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Geekscribes/~4/ibJnAspfVVM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekscribes.net/blog/2012/05/08/sbmnet-transfers-woes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekscribes.net/blog/2012/05/08/sbmnet-transfers-woes/</feedburner:origLink></item>
		<item>
		<title>Fundamentals of Programming: Part 7 – Loops</title>
		<link>http://feedproxy.google.com/~r/Geekscribes/~3/ovrysxl6Paw/</link>
		<comments>http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/#comments</comments>
		<pubDate>Sun, 08 Apr 2012 21:30:13 +0000</pubDate>
		<dc:creator>InF</dc:creator>
				<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech Posts]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.geekscribes.net/blog/?p=1777</guid>
		<description><![CDATA[Welcome to Part 7: Loops! I hope you have enjoyed the previous parts and if you haven&#8217;t checked them yet, you should! Here&#8217;s a link to Part 1! In this part, we consider another fundamental building blocks of programs: Loops. What are they? Simply, a method for telling your computer to do the same thing [...]<p>This article comes from <a href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a href="http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/">Fundamentals of Programming: Part 7 &#8211; Loops</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Welcome to Part 7: Loops! I hope you have enjoyed the previous parts and if you haven&#8217;t checked them yet, you should! Here&#8217;s a link to <a  title="Fundamentals of Programming: Part 1 – Introduction" href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/">Part 1</a>!</p>
<p>In this part, we consider another fundamental building blocks of programs: Loops. What are they? Simply, a method for telling your computer to do the same thing a bunch of times. Nothing too hard huh? Let&#8217;s get on with it!</p>
<p><span id="more-1777"></span></p>
<p>As I said, a loop is a cycle, where the ending point is also the starting point. I.e. when you reach the end, you actually find yourself at the start.</p>
<p>A loop in programming is pretty much the same: You tell your computer execute a set of instructions for a number of times or until some condition is met. Then stop repeating and proceed with other stuff.</p>
<p>Say you want to write a program that models a bunny. The bunny only knows how to jump. Now, you want that bunny to jump 5 times. How would you do that?</p>
<p>You can write it like this:</p>
<pre class="brush: plain; title: ; notranslate">
Jump
Jump
Jump
Jump
Jump
</pre>
<p>That is, you write your instructions 5 times. If you want 10 jumps, you write 10 jump instructions. If you want 100 jumps, you write 100 jumps instructions. If you want 10,000? Ugh&#8230; You see the problem? You don&#8217;t want to have to write that instruction 10,000 times! And that&#8217;s why you&#8217;d use a loop. Wouldn&#8217;t it be nice to just tell the program &#8220;do this 10,000 times&#8221;? Something like:</p>
<pre class="brush: plain; title: ; notranslate">
do 10,000 times{
	jump
}
</pre>
<p>That would be nice huh? Well, you just wrote a loop! It&#8217;s not exactly written like above, but the true ways of writing loops look just like that.</p>
<p>There are 3 main ways of writing loops, and we&#8217;ll go through each of them in turn, and I&#8217;ll tell you when to use them.</p>
<h2>For Loop</h2>
<p>The first way, but probably the weirdest way of writing a loop is the For loop. Sorry, but it really doesn&#8217;t look like the loop above! The other kinds of loops do look friendlier though! If I want to write that loop above as a For loop:</p>
<pre class="brush: plain; title: ; notranslate">
for (int i = 0; i &lt; 10000; i++){
    jump
}
</pre>
<p>WHAT IS THAT THING?!! It&#8217;s so hard to read! Lol&#8230; no it isn&#8217;t. Wait, we&#8217;ll go through it in parts.</p>
<p>&#8220;for&#8221; is perhaps the easiest thing to read. It&#8217;s a keyword indicating a For loop and in the end, you will come to just read it as &#8220;for 10,000 times&#8221;.</p>
<p style="padding-left: 30px;"><strong>(int i = 0; i < 10000; i++)</strong> ouch&#8230; But hey, do you see this consists of 3 parts? Let&#8217;s break it down, it&#8217;s not that hard!</p>
<p style="padding-left: 30px;"><strong>int i = 0;</strong> We are just saying, initialize variable i as an Integer, and set its value as zero to begin with. This is done only ONCE in the loop, i.e. the start!</p>
<p style="padding-left: 30px;"><strong>i < 10000;</strong> If you have read the <a  title="Fundamentals of Programming: Part 6 – Conditions" href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/">part on Conditions</a>, this is familiar to you now. It&#8217;s just a check to see if the value of i is LESS than 10000. This check is performed at EVERY RUN of the loop. Hence, we&#8217;ll check the value of i 10,000 times during execution.</p>
<p style="padding-left: 30px;"><strong>i++</strong> that&#8217;s new! It&#8217;s the short form of &#8220;i = i + 1&#8243;, i.e. add 1 to the current value of i. The incrementing is done ONCE every run, so we increment the value of i 10,001 times! What? Yep, 10,001 times! Remember, 0 to 9999 is 10,000 times! But see, the loop stops at 10,000 times right, so the value of i has to become 10,000 at some point. 0 to 10,000 is? 10,001!</p>
<p>So if we read the whole thing now:</p>
<p>for (int i = 0; i < 10000; i++) means, "do the following instructions for 10,000 times". The variable i is what we call a <strong>loop counter</strong>. It keeps track of how many times we have executed the loop so far, i.e. how many times we have run our instructions in a loop. The loop counter acts as our memory, so we remember how many times we&#8217;ve done that loop.</p>
<p>Why do we need 3 parts? We need a <strong>starting point</strong> (i = 0). We also need to know <strong>when to stop</strong> (i < 10000). Finally, we need to update our <strong>count</strong> of how many times we&#8217;ve run so far (i++). With those 3 elements together, we&#8217;re able to know where we start, how many times we looped and when we stop.</p>
<p>We start counting at zero and every time the loop executes, we increment the value of i (i++) once. We continue running the loop until i is no longer less than 10,000 i.e. i = 10,000 stops the loop. After those checks and whatnot, our bunny jumps! Then we restart, check the value of i, increment it, bunny jumps, check value of i, increments it, bunny jumps etc&#8230; until 10,000.</p>
<h2>While Loop</h2>
<p>So that was one way of doing looping. We can also use a While loop. This one looks simpler, but beware, it has a trap!</p>
<pre class="brush: plain; title: ; notranslate">
while (i &lt; 10000){
    jump
}
</pre>
<p>As before, &#8220;while&#8221; is a keyword indicating that a loop should be done.</p>
<p>(i < 10000) is a check, so we know when to stop. So we read "while i is less than 10,000, do these instructions"</p>
<p>Have you noticed the trap yet? It may not be that obvious, so compare with the For loop above. Tip: i++</p>
<p>The incrementing of the counter i is missing! What happens if you do that? You have a bunny that never stops jumping! An infinite loop!</p>
<p>Since you're never incrementing the value of i, the check will always return true since every time we compare, i is still zero. The loop never stops and in the end, you have a very tired bunny.</p>
<p>You have to be really careful about not missing the incrementer if you're using While loops. If you're not counting, or doing some other kind of check, you need to make sure there's a stopping condition for the loop. Otherwise, you will in most cases, have a non-responding program to deal with while it's stuck in the loop.</p>
<p>Infinite loops do have their uses though. For example, you can keep repeating your program to read an unknown number of inputs from the user, until they enter "quit" for example. Since you don't know when to stop, you just run infinite number of times and implement a manual stop condition. We'll see about that in the ending section of this part!</p>
<p>Here's a correctly written While loop that behaves as we expect:</p>
<pre class="brush: plain; title: ; notranslate">
int i = 0
while (i &lt; 10000){
    jump
    i++
}
</pre>
<p>But why do you ever need a While loop? A For loop works perfectly well for this task! It happens that you're performing actions that don't rely on a counter. Or you don't really know when you have to stop. A For loop runs for a <strong>finite number</strong> of times. A While loop can run for as long as you want it to, maybe depending on some external condition or some user action. Here's an example:</p>
<pre class="brush: plain; title: ; notranslate">
while ( isUserPresent() ){
    jump
}
</pre>
<p>In this loop, we're using a function isUserPresent. You can ignore it for now. Just know that it'll return true if the user is currently sitting at their desk. It returns false if the user is not present. Functions come in the next part.</p>
<p>This loop will have our bunny jump if the user is there to see it. If the user leaves, the check becomes "false" and the loop stops. The bunny then stops jumping and the loop ends.</p>
<p>As you can see, this While loop depends on an external condition that's not related to counting. You don't know for how long the user will be at their desk and so in effect, you're executing the loop until the user leaves, potentially executing an infinite number of times (user never leaves). You can't easily do this with a For loop, since it's finite.</p>
<p>A While loop gives you more flexibility to control when to start, how many times to execute and when to stop. Or if you wish, you can continue as long as you want (infinite loop).</p>
<h2>Do While Loop</h2>
<p>A third way of writing loops is the Do While loop, also called Repeat Until loop. This is not present in all languages, but can be quite useful in some conditions. Let's compare with a While loop.</p>
<pre class="brush: plain; title: ; notranslate">
int i = 10000
while (i &lt; 10000){
    jump
    i++
}
</pre>
<p>What happens in this case? Check the initial value of i (10,000)? while (i < 10000) never executes because i is NOT less than 10,000. It is EQUAL to 10,000! So your loop never runs!</p>
<p>There are cases when you want the loop to run at least once before the check is made. Say for example, you want the bunny to jump at least once, even if there may not be any user. You would not be able to do this with the While loop without extensive checking and tricking the program. But you can very easily do this with a Do While loop:</p>
<pre class="brush: plain; title: ; notranslate">
do{
    jump
}while (isUserPresent());
</pre>
<p>Note how we shifted the check AFTER the instructions. So no matter what, we'll still execute the "jump" instruction at least once before we reach the first check for user presence.</p>
<p>This kind of loop is less frequently used as compared to the above two, but am including it just so you know it exists. One realistic scenario where you might want to use a Do While loop is to check that the user's input is what you expect, and if it's not, keep asking for an input until you get what you want. Here is a non-language-specific example:</p>
<pre class="brush: plain; title: ; notranslate">
do{
    Output &quot;Say Y or N: &quot;
    Get user input
}while (input != &quot;Y&quot; || input != &quot;N&quot;);
</pre>
<p>I chose to give a non-language-specific example to keep it as simple as possible.</p>
<p>This block of code will execute at least once and ask the user to either type "Y" or "N". We then read what the user typed. Finally, we check if the input was either "Y" or "N". If the value input was neither of these, we basically drop into an infinite loop until we get what we want. That is, we keep asking the user until they say either "Y" or "N" and then stop the loop.</p>
<p>This illustrates an example of where you need to get the input first, then decide what to do. You could have used a While loop too:</p>
<pre class="brush: plain; title: ; notranslate">
Output &quot;Say Y or N: &quot;
Get user input
while (input != &quot;Y&quot; || input != &quot;N&quot;){
    Output &quot;Say Y or N: &quot;
    read input
}
</pre>
<p>Notice how you're repeating the instructions? That's something you want to avoid in programming. Stick to the Don't Repeat Yourself (DRY) principle in programming. The Do While is a simpler alternative here.</p>
<h2>Break and Continue</h2>
<p>Before we end, there are two important keywords that you need to know. If you read <a  href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/" title="Fundamentals of Programming: Part 6 – Conditions">Part 6 on Conditions</a>, you've already seen "break" used inside a Switch block.</p>
<p>"break" tells your program to "break out" of the structure it current is in. It's mainly used in loops, where you want to manually stop the loop, especially if you're using an infinite loop and want to manually stop at some point.</p>
<p>"continue" tells your loop to ignore the rest of the instructions in the loop and restart with the next cycle of the loop.</p>
<p>Let's see examples of these keywords:</p>
<pre class="brush: plain; title: ; notranslate">
int i = 1
while (true){

    jump
    i++

    if (i == 10000){
        break
    }

}
</pre>
<p>The loop above is a complex way of just stopping when i is 10,000. We're using an infinite While loop. Notice while (true)? Well, the check will always return true since we're saying it's "true"! There is no stopping condition there. Instead, we use an If condition to do the check, and "break" the loop when we want. So when i reaches 10,000, we break out of the loop and stop it. Fairly stupid way to do it, but it's just to illustrate "break".</p>
<pre class="brush: plain; title: ; notranslate">
do{
    output &quot;Say Y or N: &quot;
    read input

    if (input == &quot;N&quot;){
        continue
    }

    output &quot;Hello!&quot;
}while(true);
</pre>
<p>The loop above is again, an infinite loop. We want the user to type "Y" or "N". If they type "N", we don't greet them since "continue" is activated and the loop restarts, skipping the output line. If they say "Y", the If condition doesn't trigger and we say "Hello!" to them.</p>
<p>P.s. Normally, you don't compare Strings like that. You don't do input == "N" for e.g. but would use some function. We'll see about that later.</p>
<p>That's all for this part on loops. Remember, they just allow you to tell the computer to do a set of actions multiple times. There are multiple ways of writing them, and you'll use different ways depending on what you want to achieve. For finite loops where you know how many times you want to execute, use For. For more flexibility, use While. If you want to run at least once before checking, use Do While.</p>
<p>I hope you found this part interesting. If you have questions, please ask in the comments section below. Suggestions welcome!</p>
<h3>In the same series:</h3><ul class="eg-series-posts" ><li class="eg-series-posts-item current"><span class="eg-series-post-title">Fundamentals of Programming: Part 7 - Loops</span> <span class="eg-series-post-this">(This post)</span></li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/" title="Fundamentals of Programming: Part 6 - Conditions">Fundamentals of Programming: Part 6 - Conditions</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/" title="Fundamentals of Programming: Part 5 - Data Types">Fundamentals of Programming: Part 5 - Data Types</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/" title="Fundamentals of Programming: Part 4 - Variables and Arrays">Fundamentals of Programming: Part 4 - Variables and Arrays</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/07/fundamentals-of-programming-part-3-style-indentation-and-comments/" title="Fundamentals of Programming: Part 3 - Style, Indentation and Comments">Fundamentals of Programming: Part 3 - Style, Indentation and Comments</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/05/fundamentals-of-programming-part-2-pseudo-code-and-batch-jobs/" title="Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs">Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/" title="Fundamentals of Programming: Part 1 - Introduction">Fundamentals of Programming: Part 1 - Introduction</a></span> </li></ul>
<p>This article comes from <a  href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a  href="http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/">Fundamentals of Programming: Part 7 &#8211; Loops</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/K-xENtEaCifWvg3RpXqqWvFHuT4/0/da"><img src="http://feedads.g.doubleclick.net/~a/K-xENtEaCifWvg3RpXqqWvFHuT4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/K-xENtEaCifWvg3RpXqqWvFHuT4/1/da"><img src="http://feedads.g.doubleclick.net/~a/K-xENtEaCifWvg3RpXqqWvFHuT4/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ovrysxl6Paw:qA3gjbNZr1A:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ovrysxl6Paw:qA3gjbNZr1A:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ovrysxl6Paw:qA3gjbNZr1A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ovrysxl6Paw:qA3gjbNZr1A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ovrysxl6Paw:qA3gjbNZr1A:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ovrysxl6Paw:qA3gjbNZr1A:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=ovrysxl6Paw:qA3gjbNZr1A:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=ovrysxl6Paw:qA3gjbNZr1A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Geekscribes/~4/ovrysxl6Paw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/</feedburner:origLink></item>
		<item>
		<title>Fundamentals of Programming: Part 6 – Conditions</title>
		<link>http://feedproxy.google.com/~r/Geekscribes/~3/tZ46x-KzUN8/</link>
		<comments>http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 18:41:31 +0000</pubDate>
		<dc:creator>InF</dc:creator>
				<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech Posts]]></category>
		<category><![CDATA[Unsorted]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.geekscribes.net/blog/?p=1768</guid>
		<description><![CDATA[Welcome again! In this series, we go on to the first of the essential building blocks of programming: conditions. This is the first part of the series that starts to deal with blocks of code, as opposed to just independent lines. So without delay, let&#8217;s start. In this long series, we&#8217;ll cover condition blocks using [...]<p>This article comes from <a href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/">Fundamentals of Programming: Part 6 &#8211; Conditions</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Welcome again! In this series, we go on to the first of the essential building blocks of programming: conditions. This is the first part of the series that starts to deal with blocks of code, as opposed to just independent lines. So without delay, let&#8217;s start.</p>
<p>In this long series, we&#8217;ll cover condition blocks using if-else and if-else if-else. We cover checks using AND, or and NOT (! symbol). We finally end with the switch statement. I doubt you&#8217;ll be able to grasp everything in one go as a beginner, so go slow, maybe a section at a time, try to understand how it works before moving on.</p>
<p><span id="more-1768"></span></p>
<h2>What are conditions?</h2>
<p>It happens that you need your program to take decisions based on checks and conditions. That is, you want your program to perform a set of actions in case A, but perform another set of actions in case B. That&#8217;s where conditions come in. It allows you to implement <strong>decision points</strong> in your programs.</p>
<p>Associated with a condition is a <strong>check</strong>. Usually, you will be checking what the value of a variable is. Then based on this value, you will branch out and do different things.</p>
<p>Conditions start with the keyword <strong>if</strong>, followed by a <strong>check</strong>. There may be another section, called <strong>else</strong> which gets executed if the condition check turns out to be false. Within these two sections, there can be other conditions too. These are called <strong>nested conditions</strong>.</p>
<p>Let&#8217;s see a blank, simple condition block:</p>
<pre class="brush: plain; title: ; notranslate">
if (condition){
	do this
}
else{
	do that
}
</pre>
<p>This is perhaps, your first serious encounter with a block of code, so let&#8217;s devote some time to how it is structured.</p>
<p>First notice the { and } braces. These indicate where a block starts and ends. I.e. they tell which statements are part of which blocks. Think of those as boundaries. Everything within the first set of { } is for the if-block and everything in the second { } is for the else-block. Not all languages use braces to separate blocks like these; Python for e.g. uses indentation to indicate blocks. So you just indent lines under an if and these lines are considered under the control of the if-block. Other languages use keywords, such as&nbsp; &#8220;end if&#8221;. It&#8217;s all programming-language dependent, so when programming, you will use the method your language imposes.</p>
<p>Just for clarity, here is this same example, but in those different ways I just mentioned:</p>
<pre class="brush: plain; title: ; notranslate">

if (condition):
	do this
else:
	do that

if (condition)
	do this
else
	do that
end if
</pre>
<p>So that&#8217;s structure. Let&#8217;s focus on the if line: if (condition). The condition is the check which will determine which block gets executed. You will normally decide what to do based on a variables&#8217; values. This is an actual example:</p>
<pre class="brush: plain; title: ; notranslate">

if (x &gt; 0){
	output &quot;X is positive&quot;
}
else (x &lt; 0){
	output &quot;X is negative&quot;
}
</pre>
<p>Here, we want to check if x is positive or negative, and tell the user what it is. Let&#8217;s consider three cases:</p>
<ul>
<li><span style="text-decoration: underline;">x is 2</span>: The if statement&#8217;s condition will see what value x has. It sees 2. Is 2 greater than 0? True, so we go into the if-block and execute the output line. The user sees &#8220;X is positive&#8221;. After executing that, we DO NOT execute the else part. The else-block is only executed if the condition was not matched. So after outputting the line, we finish.</li>
<li><span style="text-decoration: underline;">x is -1</span>: The if statement&#8217;s condition checks the value of x. It sees -1. Is -1 greater than 0? False. We skip over to the else part since we didn&#8217;t get a match. The if-block is NOT executed. We go inside the else-block and execute the output line. The user sees &#8220;X is negative&#8221;. After this, we finish.</li>
<li><span style="text-decoration: underline;">x is 0</span>: The if statement&#8217;s condition is checked. Is 0 &gt; 0? False! (It&#8217;s not greater! It&#8217;s greater or equal, but we&#8217;re only checking greater here). Since the if-block condition failed, we go to the else part and output &#8220;X is negative&#8221;. That&#8217;s probably not what we wanted. Did we really want to tell the user that zero is negative? (Oh by the way, in computing, you can have positive-zero and negative-zero!)</li>
</ul>
<p>You can see that we&#8217;re missing a condition here, for the case where x is zero. There are multiple ways to do that, one is not available in all languages. Let&#8217;s see 3 ways:</p>
<p>First:</p>
<pre class="brush: plain; title: ; notranslate">

if (x &gt; 0){
	output &quot;X is positive&quot;
}

if (x &lt; 0){
	output &quot;X is negative&quot;
}

if (x == 0){
	output &quot;X is zero&quot;
}
</pre>
<p>This works, but normally, you don&#8217;t want to do that because it increases the number of lines of code unnecessarily. Programmers like compact code and you may get a few stares if you do that while working as a programmer. Do note that you can have if-blocks on their own too. You don&#8217;t always need an else-part.</p>
<p>Second, nested blocks or if-ception if you want. If inside if:</p>
<pre class="brush: plain; title: ; notranslate">

if (x &gt; 0){
	output &quot;X is positive&quot;
}
else{

	if (x &lt; 0){
		output &quot;X is negative&quot;
	}
	else{
		output &quot;X is zero&quot;
	}

}
</pre>
<p>Can you understand what we did here? We check if x is positive. If it is not, we do the else-block. In there, we find another condition check, so we again check the if part. This time, is x negative? If not, we do the else. Well, if the number is not positive (first if) and not negative (first if in else-block), we guess that it has to be zero (else in else-block).</p>
<p>Third, else if:</p>
<pre class="brush: plain; title: ; notranslate">

if (x &gt; 0){
	output &quot;X is positive&quot;
}
else if (x &lt; 0){
	output &quot;X is negative&quot;
}
else{
	output &quot;X is zero&quot;
}
</pre>
<p>This way is similar to the nested-block one (second) but it&#8217;s just shorter to write. Programmers like shorter-to-write. It just has an &#8220;else if&#8221; keyword which is additional condition checks if the first if fails. So here, we check the first if to see if X is positive. Nope? Check the second if (else if). Is x negative? Nope. We go to the else block then and output &#8220;X is zero&#8221;.</p>
<p>There is a short way for checking the values of booleans:</p>
<pre class="brush: plain; title: ; notranslate">

if (isSunny)
	go out
else
	stay indoors
</pre>
<p>isSunny is a boolean variable. For boolean variables. you don&#8217;t need to write if (isSunny == true) or if (isSunny == false). You just write if (isSunny) and it&#8217;s sufficient to get the condition block working. It&#8217;s just a short, recommended way to check for boolean variables&#8217; values.</p>
<p>Oh to check if it&#8217;s false? if (!isSunny). ! operator reverses the value, so true becomes false, false becomes true. This check is frankly, weird but heavily used once you understand it. Prepare for weirdness!</p>
<p>Two cases happen. We want to execute the if statements only if isSunny is TRUE! </p>
<p>For simplicity, read !isSunny as NOT-is-sunny or is-NOT-sunny if you wish.</p>
<ul>
<li><span style="text-decoration: underline;">isSunny is true</span>: So if (!isSunny) will reverse the value, hence we get if (false). The if-block is skipped.</li>
<li><span style="text-decoration: underline;">isSunny is false</span>: if (!isSunny) will flip the value again, and we get if (true) this time. The if-block is now executed. That&#8217;s what we wanted. Execute only when the boolean is false.</li>
</ul>
<p>Did you notice the lack of { and } in this example? It&#8217;s not a mistake. If your condition blocks if and else have only a single line within, you can leave out the brackets, again for making programs shorter. Personally, I just put them in, single-line or not. Just makes things clearer for me. But what you do is your decision! <img src='http://www.geekscribes.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<h2>Multiple condition checks</h2>
<p>What happens if you want to check for multiple conditions at once before deciding whether to go into a block? For that, there is AND and OR operators. They are usually represented using symbols &#038;&#038; for AND and || for OR. Those are two ampersands and two &#8220;pipe&#8221; symbols. Look for it on your keyboard. It&#8217;s usually to the left of &#8220;Enter&#8221; or the Z key.</p>
<p>Quick recap: For AND, all conditions must be true for the whole check to be true. If any condition fails, we have false. For OR, at least one condition has to be true for the whole check to be true.</p>
<p>Here you have 2 examples:</p>
<pre class="brush: plain; title: ; notranslate">

if ( isSunny &amp;&amp; (day == &quot;Saturday&quot;) ){
	go out
	buy groceries
	meet friends
}
else{
	stay indoors
}
</pre>
<p>The if-condition now has the &#038;&#038; (AND) symbol, so we&#8217;re checking for 2 conditions here. First, isSunny has to be true AND it has to be saturday. If it&#8217;s not sunny or it&#8217;s not saturday, we go to else. If it&#8217;s both sunny and saturday, then we go to the if-block.</p>
<p>You will also notice that the different conditions can be put in their own brackets. These brackets help with ordering which operations are performed first. You&#8217;ve seen that at work in the previous post. The second example shows this:</p>
<pre class="brush: plain; title: ; notranslate">
if ( isFree || (day == &quot;Saturday&quot; &amp;&amp; isSunny) ){
	go out
	buy groceries
	meet friends
}
else{
	stay indoors
}
</pre>
<p>Here we see the OR and AND both at work. Due to brackets being used, we consider the second check as a compound check. So if I&#8217;m free OR if it&#8217;s both saturday AND it&#8217;s sunny, then I go out, buy groceries and meet friends.</p>
<p>If isFree alone is true, it&#8217;s sufficient to do the if-block i.e. go out. But if I&#8217;m not free, the second part has to be true. Thus if I&#8217;m not free, but it&#8217;s both saturday AND sunny, then I go out.</p>
<p>Those AND and OR conditions take some getting used to, so don&#8217;t worry too much if you don&#8217;t completely understand them here. You will, with practice.</p>
<h2>To summarize:</h2>
<p>Conditions allow you to take decisions in your programs. You will check values of variables and if these are true or false, you take different actions. There are multiple ways to write conditional blocks and the one you choose depends on the programming language you&#8217;re writing in. You can have if checks nested in other if or else blocks for extended checks or you can use else if constructs if the language supports it. If you want to check for multiple conditions, use AND and OR.</p>
<p>Just before we end, what happens if you have lots of checks to perform. Well, you can use lots of if-blocks and face the anger of other programmers! Or you can use the switch statement, also known as the select statement.</p>
<h2>Switch statement</h2>
<p>The switch statement (or select statement) is really language-dependent. Some languages allow you to do comparisons, others just pure equality. Others vary the way the blocks are ordered. It&#8217;s just varied. Java for example, has only introduced switch equality checks for strings recently (Java 7) 15 years after the feature was requested. I&#8217;ll stick to one language-independent version here just to illustrate how it works. This one only supports equality check on numbers.</p>
<p>Here goes:</p>
<pre class="brush: plain; title: ; notranslate">
switch (dayOfWeek){

	case 1:
		output &quot;Monday&quot;
		break
	case 2:
		output &quot;Tuesday&quot;
		break
	case 3:
		output &quot;Wednesday&quot;
		break
	case 4:
		output &quot;Thursday&quot;
		break
	case 5:
		output &quot;Friday&quot;
		break
	case 6:
		output &quot;Saturday&quot;
		break
	case 7:
		output &quot;Sunday&quot;
		break
	default:
		output &quot;Wrong day!&quot;
		break
}
</pre>
<p>Ooooh that was long! Basically, we tell the switch statement which variable we want to examine, so here &#8220;dayOfWeek&#8221;. The various &#8220;case&#8221; lines represent possible values for the dayOfWeek variable.</p>
<p>Notice, it only supports equality checks! You can&#8217;t for example check if dayOfWeek > 6 in the switch statement!</p>
<p>So we check the value of dayOfWeek. Then jump to the appropriate &#8220;case&#8221; line and execute its statements. Say dayOfWeek is 3. We check case 1, which is equivalent to if (dayOfWeek == 1). Nope. Case 2, if (dayOfWeek == 2). Nope. Case 3, if (dayOfWeek == 3)? Yes. We then go into it and execute output &#8220;Wednesday&#8221;. Then break? What is break?!</p>
<p>&#8220;break&#8221; is a keyword you will use in the future series on looping. It tells your program to &#8220;break out&#8221; of the block it&#8217;s currently in. Hence, when we hit the &#8220;break&#8221; statement, we go directly to the closing bracket and we finish.</p>
<p>That &#8220;break&#8221; keyword in all those case statements is critically important. If you don&#8217;t have them, you will &#8220;fall through&#8221; the other case statements and execute everything, irrespective of the cases&#8217; values being checked, until a &#8220;break&#8221; is encountered. I.e. you may get unexpected results! Try it for fun and see someday.</p>
<p>You can use that as &#8220;feature&#8221; too if you wish:</p>
<pre class="brush: plain; title: ; notranslate">
switch (x){

	case 0:
		do something
	case 1:
	case 2:
	case 3:
		do that too
		break
	case 5:
		do other
		break
	default:
		do something else
		break
}
</pre>
<p>In this case, if a zero is encountered, the switch statement will &#8220;do something&#8221;. Since there is no break, it&#8217;ll also &#8220;fall through&#8221; 1,2,3 and &#8220;do that too&#8221; and finally break. This could be useful in situations where you want zero to execute one statement in addition to what 1,2,3 normally do. Maybe zero is a slightly special case or something.</p>
<p>If either 1, 2 or 3 is encountered, they all lead to &#8220;do that too&#8221; and break&#8221;. I.e. 1,2,3 are the same thing and lead to the same action being performed.</p>
<p>Finally &#8220;default&#8221; represents a case where nothing was matched. Think of it as a last-resort case that&#8217;s useful to handle errors like invalid inputs from users.</p>
<p>Phew! That was a long part, eh! You now know how to make your program take decisions! I recommend that you re-read this part if you don&#8217;t understand it at one go. It&#8217;s quite a mouthful to chew at once. I hope you got something out of it. As usual, post your questions and suggestions in the comment section, and see you in the next part.</p>
<p style="font-size: 0.7em">Note: Technically, you don&#8217;t compare strings&#8217; values directly like day == &#8220;Saturday&#8221;. You would use a function or method such as equals(), like day.equals(&#8220;Saturday&#8221;). I left those out for simplicity here as these come in a future part. Also, I tried to keep using pseudo-code here, so there are no semi-colons on statements. Just so you know! <img src='http://www.geekscribes.net/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<h3>In the same series:</h3><ul class="eg-series-posts" ><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/" title="Fundamentals of Programming: Part 7 - Loops">Fundamentals of Programming: Part 7 - Loops</a></span> </li><li class="eg-series-posts-item current"><span class="eg-series-post-title">Fundamentals of Programming: Part 6 - Conditions</span> <span class="eg-series-post-this">(This post)</span></li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/" title="Fundamentals of Programming: Part 5 - Data Types">Fundamentals of Programming: Part 5 - Data Types</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/" title="Fundamentals of Programming: Part 4 - Variables and Arrays">Fundamentals of Programming: Part 4 - Variables and Arrays</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/07/fundamentals-of-programming-part-3-style-indentation-and-comments/" title="Fundamentals of Programming: Part 3 - Style, Indentation and Comments">Fundamentals of Programming: Part 3 - Style, Indentation and Comments</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/05/fundamentals-of-programming-part-2-pseudo-code-and-batch-jobs/" title="Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs">Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/" title="Fundamentals of Programming: Part 1 - Introduction">Fundamentals of Programming: Part 1 - Introduction</a></span> </li></ul>
<p>This article comes from <a  href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a  href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/">Fundamentals of Programming: Part 6 &#8211; Conditions</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/oj2uzvOXZDpA7Hq-RW-aIOXvdp4/0/da"><img src="http://feedads.g.doubleclick.net/~a/oj2uzvOXZDpA7Hq-RW-aIOXvdp4/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/oj2uzvOXZDpA7Hq-RW-aIOXvdp4/1/da"><img src="http://feedads.g.doubleclick.net/~a/oj2uzvOXZDpA7Hq-RW-aIOXvdp4/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Geekscribes?a=tZ46x-KzUN8:hmRRjjlfR5c:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=tZ46x-KzUN8:hmRRjjlfR5c:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=tZ46x-KzUN8:hmRRjjlfR5c:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=tZ46x-KzUN8:hmRRjjlfR5c:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=tZ46x-KzUN8:hmRRjjlfR5c:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=tZ46x-KzUN8:hmRRjjlfR5c:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=tZ46x-KzUN8:hmRRjjlfR5c:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=tZ46x-KzUN8:hmRRjjlfR5c:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Geekscribes/~4/tZ46x-KzUN8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/</feedburner:origLink></item>
		<item>
		<title>Fundamentals of Programming: Part 5 – Data Types</title>
		<link>http://feedproxy.google.com/~r/Geekscribes/~3/elHhXUINH4Y/</link>
		<comments>http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 19:18:16 +0000</pubDate>
		<dc:creator>InF</dc:creator>
				<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech Posts]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.geekscribes.net/blog/?p=1762</guid>
		<description><![CDATA[Hello! Continuing with the Fundamentals of Programming series, we will now look at what Data Types are. If you have not read the earlier series, now would be a good time to start. You can click here go to Part 1. What are Data Types? Well, they are a way for you to tell the [...]<p>This article comes from <a href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a href="http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/">Fundamentals of Programming: Part 5 &#8211; Data Types</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Hello! Continuing with the Fundamentals of Programming series, we will now look at what Data Types are. If you have not read the earlier series, now would be a good time to start. You can click here go to <a  title="Fundamentals of Programming: Part 1 - Introduction" href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/">Part 1</a>.</p>
<p>What are Data Types? Well, they are a way for you to tell the computer what kind of data you are referring to. In English, you have two basic kinds of data types: numbers and letters. To a computer, those would be &#8220;char&#8221; and &#8220;integer&#8221;. Char is short for &#8220;character&#8221; and integer means a whole number, without fractions or decimal points.</p>
<p>Unlike humans, computers are not so intelligent (yet!) and need to be told exactly what kind of data they are dealing with. For us, numbers are &#8220;3&#8243; and &#8220;3.24&#8243;. For a computer, you&#8217;d need to specify &#8220;integer&#8221; for whole numbers, &#8220;float&#8221; or &#8220;double&#8221; for decimal numbers and even specify the sign! Talk about smart!</p>
<p><span id="more-1762"></span></p>
<h2>Just how many data types are there?!</h2>
<p>Lots! And they have specific uses too. We have to start somewhere, so let&#8217;s start with numbers.</p>
<ul>
<li><strong>Integers:</strong> They are <strong>whole numbers</strong>. E.g. &#8220;3&#8243;, &#8220;210&#8243; and even &#8220;4521&#8243; are all integers. They are represented as 2 bytes (16 bits) in most programming languages. They can be signed (positive/negative) or unsigned.</li>
</ul>
<ul>
<li><strong>Float:</strong> These are &#8220;floating point&#8221; numbers. Fancy term for &#8220;<strong>decimal numbers</strong>&#8221; or fractions if you want. E.g. &#8220;2.35&#8243;, &#8220;30.16&#8243; are floating point numbers. Floating point numbers are actually stored in a quite complex, as a sign bit, an exponent, a fraction and a bias. You don&#8217;t need to know all that, but if you <a  title="IEEE Float Representation" href="http://steve.hollasch.net/cgindex/coding/ieeefloat.html" target="_blank">want to</a>&#8230; Floats are stored on 4 bytes.</li>
</ul>
<ul>
<li><strong>Double:</strong> For a rather obscure reason, floating point numbers are not precise enough. Computers have troubles dealing with the fractional part and after a number of digits after the decimal, you lose precision. So for <strong>high-precision decimal numbers</strong>, you use double. It&#8217;s stored on a massive 8 bytes for high-precision numbers e.g. used in scientific data.</li>
</ul>
<p>Various languages define other data types e.g. Date or Currency, but the 3 above are almost universal.</p>
<p>What about text? Here it&#8217;s easier.</p>
<ul>
<li><strong>Char:</strong> This represents a <strong>single character</strong>. Note, characters include letters, numbers and symbols, but you can&#8217;t make calculations using char. They&#8217;re the literal representation of the number. For e.g. &#8216;n&#8217;, &#8216;N&#8217; and &#8217;7&#8242; are chars. Note the single quotes.</li>
</ul>
<ul>
<li><strong>String:</strong> Basically, you can think of them as an array of chars. They are just <strong>sequences of characters</strong>. E.g. &#8220;Hello&#8221; is a string. Note, double quotes.</li>
</ul>
<p>Normally, to avoid confusion, chars are represented inside single quotes while string are within double quotes.</p>
<p>How would you represent true or false?</p>
<ul>
<li><strong>Booleans:</strong> It&#8217;s a <strong>&#8220;true&#8221; or &#8220;false&#8221;</strong> value that can only be one at a time. Programs don&#8217;t have a separate data type for that. They normally just represent true as &#8220;1&#8243; and false as &#8220;0&#8243;.</li>
</ul>
<p>There are a few other data types you may want to know of, although per-se, they are not necessarily &#8220;primitive&#8221; data types:</p>
<ul>
<li><strong>Enumerations:</strong> Sometimes you want data that only comes from within a list of values which you define (or enumerate). For example, &#8220;Days of the week&#8221; would be &#8220;Monday, Tuesday, Wednesday&#8230; Sunday&#8221;. You can then make checks to see if some value falls within your list.</li>
</ul>
<ul>
<li><strong>Array:</strong> This is a sequence of other data types. As I stated before, arrays have data types in most languages i.e. they are typed. You can such as, have arrays of integers, floats, or chars &#8211; strings basically.</li>
</ul>
<ul>
<li><strong>Map:</strong> This is a abstract type that allows you to store a number of key-value pairs as mappings. For e.g. you can map Strings to Integers or Integers to Strings or even Strings to Strings. For e.g. &#8220;John &#8211; Doe&#8221; would be a mapping of &#8220;Forename-Surname&#8221;. The key is &#8220;John&#8221; and the value is &#8220;Doe&#8221;. I could ask the map for &#8220;John&#8221; and it would give me the corresponding value, which is &#8220;Doe&#8221;, if I was looking for surnames.</li>
</ul>
<ul>
<li><strong>Set:</strong> A set can be thought of as an array that does not allow duplicates. You cannot have a set like [2][3][2] for example. If you tried that, it&#8217;d just store it as [2][3] and ignore duplicates, in most cases. It could also complain about duplicates in some languages.</li>
</ul>
<ul>
<li><strong>Queue:</strong> This structure is like an array, but you read from one side and put new things from the other. It&#8217;s like a queue in real life: people join at the end of the queue and leave from the head. It is a First-In-First-Out (FIFO) structure.</li>
</ul>
<ul>
<li><strong>Stack:</strong> Data is read and written to from the same end. Think of a pile of plates. The last one you put is the first one you will take off the pile. It&#8217;s is a Last-In-Last-Out (LILO) structure. Or First-In-Last-Out (FILO) if you want.</li>
</ul>
<ul>
<li><strong>Pointer:</strong> A pointer points to a memory location. For example, you can have a pointer that references the memory location where an integer is stored for e.g. Or have a pointer to indicate at which point you have reached in an array.</li>
</ul>
<p>Why are there all these data types? Why do you need them at all? That&#8217;s because computers represent data and use them differently. You cannot do calculations with strings directly. You cannot addition two booleans. The different types are useful when you want to do type-checking, for example, the user has not input letters when you&#8217;re trying to do calculations with numbers. Or you are trying to put letters in an array for floats for example. The compiler in strict languages will complain bitterly and prevent you from committing mistakes, so your programs break less often.</p>
<h2></h2>
<h2>Operators</h2>
<p>There are different operators you can use on different data types. Here are the most commonly used operators:</p>
<ul>
<li>= :Not equal to! Yep! Not Mathematics! This sign means &#8220;<strong>assignment</strong>&#8220;. It is used to assign values to variables!</li>
</ul>
<ul>
<li>== :This one is what you know as &#8220;<strong>equal to</strong>&#8220;. It compares equality and returns &#8220;true&#8221; or &#8220;false&#8221; depending on what it finds.</li>
</ul>
<ul>
<li>=== :Some languages have this sign. It compares <strong>both values and data types</strong>. If both are same, then &#8220;true&#8221; is returned, otherwise &#8220;false&#8221;.</li>
</ul>
<ul>
<li>+ :Normally represents <strong>addition</strong>. I.e. the sum of two numbers. In some languages however, it can also represent joining two strings. For e.g. &#8220;2+2&#8243; will give &#8220;4&#8243;. But, &#8220;Hello&#8221; + &#8220;World&#8221; will give &#8220;HelloWorld&#8221;.</li>
</ul>
<ul>
<li>- :<strong>Subtraction</strong>. Simple. &#8220;5-3&#8243; will give &#8220;2&#8243;.</li>
</ul>
<ul>
<li>* :Unlike Mathematics, where x represents <strong>multiplication</strong>, in programming, you use asterisk. &#8220;2*2&#8243; gives &#8220;4&#8243;.</li>
</ul>
<ul>
<li>/ :This one is <strong>division</strong>. &#8220;4 / 2&#8243; gives &#8220;2&#8243;.</li>
</ul>
<ul>
<li>% :<strong>Modulo</strong> operator. It gives the remainder of a division operation. E.g. &#8220;5 % 2&#8243; would give 1. It is useful to check if a number is even or odd. Even number % 2 would give zero. Odd numbers give 1.</li>
</ul>
<ul>
<li>.  :That&#8217;s a dot. Some language use it to join (<strong>concatenate</strong>) two strings, instead of + to avoid confusion. So, &#8220;Hello&#8221; . &#8220;World&#8221; gives &#8220;HelloWorld&#8221;.</li>
</ul>
<p>Below are comparison operators. They return true or false (boolean data type) depending on what the condition is.</p>
<ul>
<li>&gt; :Greater than sign. Used in comparisons.</li>
</ul>
<ul>
<li>&gt;= :Greater or equal to sign.</li>
</ul>
<ul>
<li>&lt; :Less than sign</li>
</ul>
<ul>
<li>&lt;= :Less than or equal to sign.</li>
</ul>
<ul>
<li>! :Represents &#8220;<strong>not</strong>&#8220;. I.e. it inverses other operations. For example !(x&gt;2) is &#8220;not x greater than 2&#8243; or &#8220;x not greater than 2&#8243; which is equivalent to saying (x&lt;=2), or &#8220;x is less or equal 2&#8243;. E.g. != is not-equal. Careful here, != is not equal. It&#8217;s not !==. Strange, but that&#8217;s how it is!</li>
</ul>
<ul>
<li>AND: The boolean AND operation. Please see below.</li>
</ul>
<ul>
<li>OR: The boolean OR operation. Again, please see below.</li>
</ul>
<p>The last 2 operators are mainly used with Boolean data types. You use them to make a series of checks to see what the values are. For AND, all the conditions must be true for the final result to be true. If a single condition is false, the net result is false. For OR, at least ONE condition must be true for the end result to be true. If any is false, you don&#8217;t care. It&#8217;s still true. If none are true, then you get false.</p>
<p>Here are some examples of comparison operators and their boolean outputs:</p>
<ul>
<li>2 &gt; 3  :False</li>
</ul>
<ul>
<li>3 &gt; 2  :True</li>
</ul>
<ul>
<li>2 &gt;= 2  :True</li>
</ul>
<ul>
<li>2 == 2  :True</li>
</ul>
<ul>
<li>1 != 2  :True (1 is not equal to 2? Yep)</li>
</ul>
<ul>
<li>2 != 2  :False (2 is not equal 2? It is. Hence, false)</li>
</ul>
<p>Here are boolean AND and OR comparisons:</p>
<ul>
<li>(2 &gt; 3) OR (2 == 2)   :True since (2==2 is true)</li>
</ul>
<ul>
<li>(2 &gt; 3) AND (2 == 2)  :False. All conditions must be true and (2 &gt; 3) isn&#8217;t true.</li>
</ul>
<ul>
<li>(1 == 1) OR ( <em>(2&gt;3) AND (4&gt;3)</em> )  :True. This one is tough! First, you check the inner-brackets. (2&gt;3) is false, and without needing to check the rest, you know AND will now return false. (1==1) OR (false) is how the expression looks now. So (1==1) OR (false) returns TRUE since (1==1) is true, while the other is false, so you don&#8217;t care.</li>
</ul>
<ul>
<li>(2&gt;3) AND ( <em>(1==1) OR (2==2)</em> )  :False. Why? Because even if the inner OR returns true for both, the first part of AND, (2&gt;3) is false, so the whole thing becomes false.</li>
</ul>
<p>I understand that the above is a bit hard to understand for beginners. Do try a few other examples on your own or re-read this section if you are having troubles. Don&#8217;t worry too much if you don&#8217;t understand it at first. It comes together with practice and familiarity with programming languages.</p>
<p>Here are the truth tables for AND and OR, if you are interested:</p>
<p>AND: Only true if ALL conditions are true.</p>
<p>True-True = True<br />
True-False = False<br />
False-True = False<br />
False-False = False</p>
<p>OR: True if AT LEAST ONE condition is true.</p>
<p>True-True = True<br />
True-False = True<br />
False-True = True<br />
False-False = False</p>
<p>I end this post on Data Types and Operators here. I hope it was not too hard to understand and you now have a fairly good grasp of what data types do and why they are needed. If you have questions, please ask in the comments section below. Other programmers&#8217; views are welcome too. See you in the next series!</p>
<h3>In the same series:</h3><ul class="eg-series-posts" ><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/" title="Fundamentals of Programming: Part 7 - Loops">Fundamentals of Programming: Part 7 - Loops</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/" title="Fundamentals of Programming: Part 6 - Conditions">Fundamentals of Programming: Part 6 - Conditions</a></span> </li><li class="eg-series-posts-item current"><span class="eg-series-post-title">Fundamentals of Programming: Part 5 - Data Types</span> <span class="eg-series-post-this">(This post)</span></li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/" title="Fundamentals of Programming: Part 4 - Variables and Arrays">Fundamentals of Programming: Part 4 - Variables and Arrays</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/07/fundamentals-of-programming-part-3-style-indentation-and-comments/" title="Fundamentals of Programming: Part 3 - Style, Indentation and Comments">Fundamentals of Programming: Part 3 - Style, Indentation and Comments</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/05/fundamentals-of-programming-part-2-pseudo-code-and-batch-jobs/" title="Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs">Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/" title="Fundamentals of Programming: Part 1 - Introduction">Fundamentals of Programming: Part 1 - Introduction</a></span> </li></ul>
<p>This article comes from <a  href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a  href="http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/">Fundamentals of Programming: Part 5 &#8211; Data Types</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/DNAFiVO8edKLFxomfuklnbKYSic/0/da"><img src="http://feedads.g.doubleclick.net/~a/DNAFiVO8edKLFxomfuklnbKYSic/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/DNAFiVO8edKLFxomfuklnbKYSic/1/da"><img src="http://feedads.g.doubleclick.net/~a/DNAFiVO8edKLFxomfuklnbKYSic/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Geekscribes?a=elHhXUINH4Y:_qpuy7q7qlI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=elHhXUINH4Y:_qpuy7q7qlI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=elHhXUINH4Y:_qpuy7q7qlI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=elHhXUINH4Y:_qpuy7q7qlI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=elHhXUINH4Y:_qpuy7q7qlI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=elHhXUINH4Y:_qpuy7q7qlI:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=elHhXUINH4Y:_qpuy7q7qlI:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=elHhXUINH4Y:_qpuy7q7qlI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Geekscribes/~4/elHhXUINH4Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/</feedburner:origLink></item>
		<item>
		<title>Fundamentals of Programming: Part 4 – Variables and Arrays</title>
		<link>http://feedproxy.google.com/~r/Geekscribes/~3/QX82fuftCnc/</link>
		<comments>http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 18:11:02 +0000</pubDate>
		<dc:creator>InF</dc:creator>
				<category><![CDATA[Fundamentals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tech Posts]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[IT]]></category>
		<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.geekscribes.net/blog/?p=1748</guid>
		<description><![CDATA[Welcome to Part 4 where we will talk about variables and arrays are. If you&#8217;ve read the previous 3 parts, you would have noticed that I&#8217;ve used a few variables already, like A, B, Tc and Tf for example. In this section, we go deeper into what variables are, how to use them and finally, [...]<p>This article comes from <a href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a href="http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/">Fundamentals of Programming: Part 4 &#8211; Variables and Arrays</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Welcome to Part 4 where we will talk about variables and arrays are. If you&#8217;ve read the previous 3 parts, you would have noticed that I&#8217;ve used a few variables already, like A, B, Tc and Tf for example. In this section, we go deeper into what variables are, how to use them and finally, how to cope with situations where you have lots of variables to use, or don&#8217;t really know how many you need in fact!</p>
<p>So let&#8217;s begin this fairly short part, shall we?</p>
<p><span id="more-1748"></span></p>
<h2>Variables</h2>
<p>If you&#8217;ve studied Mathematics before, you should already know what variables are. You must have encountered questions that involved X and Y. For example, &#8220;X apples cost 10. How many do 2X apples cost?&#8221; or much more complex questions involving &#8220;dy/dx&#8221; etc.</p>
<p>Variables in Maths, as the name suggests, represents a piece of information that can change i.e. it is variable. It acts as a place-holder for various values and you can plug-in different values in a variable and it will change  the result at the end.</p>
<p>For example, &#8220;X + 2&#8243;. If I say, X = 1. The result is 3. If now X = 8, the result is 10. If X = 20, the result is 22. The result will change depending on the value you give to the variable X.</p>
<p>Similarly in programming, you need those &#8220;placeholders&#8221;. For example, if you ask the user to input two numbers, you need somewhere to store those. You&#8217;d put them in two variables, which you can use later. You don&#8217;t really care which values the user inputs. Whatever they will input, they will go in two variables you define, say A and B.</p>
<p>Then in your calculation, you can use the inputs directly, without caring about their values. Variables can be used at all stages of a program. They can store user inputs, store intermediate results during processing or store results of processing before output. They are an essential component of any program.</p>
<p>What to add them? A+B. Multiply them? A*B. Divide? A/B. Did you notice something? The symbols are a bit different from normal mathematics. Let&#8217;s check that for a moment:</p>
<p>Addition is +. Subtraction is -. Those two are same as in Maths. However, multiplication is represented by an asterisk, *. Division is represented by a slash. Power is represented by ^. And depending on context, % can either represent percentage, or more often, modulo (the remainder of a division operation).</p>
<p>Back to our variables. So just to make it clear, variables are placeholders that will eventually store values or results of operations. You name variables and can then access their values by this name. Do notice, YOU name variables so be sure to use <strong>meaningful names</strong> for your variables! This is really important if you don&#8217;t want to get lost in your program. Unless in very specific cases, don&#8217;t use names like A, B or X. Instead, use names like &#8220;firstinput&#8221;, &#8220;second input&#8221; and &#8220;result&#8221;. It makes this clearer when you&#8217;re reading code. This is called &#8220;<strong>self-documenting</strong>&#8221; code, code which is clear just by reading it.</p>
<p>Here&#8217;s an example where variables are used:</p>
<pre class="brush: plain; title: ; notranslate">

Define variables: firstnum, secondnum

Input two numbers: firstnum, secondnum

resultadd = firstnum + secondnum

resultsub = firstnum - secondnum

resultmult = firstnum * secondnum

resultdiv = firstnum / secondnum

Output resultadd, resultsub, resultmult, resultdiv
</pre>
<p>We made use of 6 variables, two numbers and four to store the result of four operations. We then output them to the user.</p>
<p>Check the first line. We used a line: define. Before you use variables, you have to define them, i.e create them. It&#8217;s like that in most programming languages: you <strong>define variables before you can use them</strong> otherwise the compiler will complain bitterly! Not all languages force this, but it&#8217;s good-practice to define all your variables before you use them. Normally you will also specify the <strong>data type</strong> of the variable too, but we&#8217;ll see that in the next part.</p>
<p>Do you notice something? The variable names are rather hard to understand. &#8220;resultmult&#8221;? &#8220;secondnum&#8221;? There are conventions for variable formats, and most programming languages consider variables to be case-sensitive. That is, if you write a variable name all in caps and the same variable all in lower-case, they&#8217;d be two different variables. &#8220;NAME&#8221;, &#8220;name&#8221;, &#8220;Name&#8221;, &#8220;nAmE&#8221; would all be different variables. Therefore, there should be conventions to make sure everyone is writing the same way.</p>
<p>The two most commonly used ways are: <strong>camel-casing</strong> and <strong>underscore</strong>:</p>
<p>Here&#8217;s how you name variables for camel-casing: the first letter of the first word is lowercase. All other words have their first letter as uppercase. There are no spaces. E.g. &#8220;thisIsAVariable&#8221;, &#8220;anotherVariable&#8221;. If there is a single word, it starts with just a lowercase e.g. &#8220;variable&#8221;.</p>
<p>If you&#8217;re using underscore, you use all lowercase letters and separate multi-words with underscores. Thus, &#8220;this_is_a_variable&#8221;, &#8220;another_variable&#8221;, &#8220;variable&#8221;.</p>
<p>Whichever you use, stick to it. Normally, most people use camel-casing since it takes less space and is generally easier to type than having to reach the underscore button. But again, your choice unless imposed by a project.</p>
<p>So just to summarize, variables are placeholders for data needed by your program. Name them correcly to make sure it&#8217;s clear what they&#8217;re storing. Stick to conventions.</p>
<h2>Arrays</h2>
<p>Now, arrays. What are they? You can think of them as many variables together. Why would you need that? Well, what if I wanted the user to input 100 numbers? Would you create 100 variables, like &#8220;firstNum&#8221;, &#8220;secondNum&#8221;, &#8220;thirdNum&#8221; etc up to &#8220;hundredthNum&#8221;? Ok good luck if you want to try that. How about adding them all? Will you go re-writing each one?</p>
<p>Nah, you need arrays. Think of arrays as a series of variables stuck together, they&#8217;re reachable under a single name.</p>
<p>You can visualize an array like this [ ] [ ] [ ] [ ] [ ]. This would represent an array with 5 slots, and each of those slots can be thought of as a variable in itself. Like variables, arrays also have names and you reference to a particular slot inside an array by number. Array slots usually start from zero up to whatever you define.</p>
<p>So for example, if I create an array of 5 slots, called myArray, its slots will be o, 1, 2, 3, 4. For a total of 5 slots. If I wanted to get the 3rd slot of the array? I&#8217;ll reference it like this: myArray[2]. Huh, 2? But you said 3rd slot! It&#8217;s correct! Remember, you <strong>start from zero</strong>! The nth slot is referred to as n-1. Confusing huh? Don&#8217;t worry, you&#8217;ll get used to it. We are all confused at the start. (Arrays are <strong>zero-indexed</strong>.)</p>
<p>So if we wanted to re-write that program above, but instead use arrays for the two numbers input and arrays for the results too, it&#8217;ll be like this:</p>
<pre class="brush: plain; title: ; notranslate">

Define arrays: myNum[2], result[4]

Input two numbers: myNum[0], myNum[1]

result[0] = myNum[0] + myNum[1]

result[1] = myNum[0] - myNum[1]

result[2] = myNum[0] * myNum[1]

result[3] = myNum[0] / myNum[1]

Output result[0], result[1], result[2], result[3]
</pre>
<p>Whoa, that&#8217;s a lot to write. Copy-paste is your friend! No really, it is. Use it! That&#8217;s what I did.</p>
<p>Check the 1st line. We defined our two arrays. The numbers in this case represent the size of the array. That is, we said, we want two arrays, one having 2 slots and one having 4. Just like variables, you will need to define your arrays and their size before you can start using them.</p>
<p>Of course, there are situations when you don&#8217;t really how know big an array you need. Languages provide various mechanisms to cover this and we won&#8217;t do that here since this is a &#8220;fundamentals&#8221; series. But if you do want more information, go look for &#8220;<a  title="Dynamic Array - Wikipedia" href="http://en.wikipedia.org/wiki/Dynamic_array" target="_blank">dynamic arrays</a>&#8221; and &#8220;arraylists&#8221; as a start.</p>
<p>If you want to go into arrays, Inception style, you can have arrays inside arrays&#8230; Yes! Arrays inside arrays inside arrays inside arrays inside arrays too if you want, although, that would be a bit too much. Normally, we only use arrays-in-arrays to represent tables. Conceptually, it&#8217;d look like this:</p>
<pre class="brush: plain; title: ; notranslate">

       0     1    2              0     1     2
0 [  [ a ]  [ ]  [ ]  ]   1 [  [ b ]  [ ]  [ c ]  ]

or if that helps, vertically:

       0     1     2

0 [  [ a ]  [ ]  [   ]  ]

1 [  [ b ]  [ ]  [ c ]  ]
</pre>
<p>How would you go about referencing the various slots? Well, two brackets. Let&#8217;s say this array is called &#8220;myArray&#8221;.</p>
<p>To reference &#8220;a&#8221; you reference the outer array first, then the inner one. Thus:</p>
<p>a would be: myArray[0][0]</p>
<p>b would be: myArray[1][0]</p>
<p>c would be: myArray[1][2]</p>
<p>What you just saw above is an array of size 3 inside an array of size 2.To define it, you&#8217;d call: myArray[2][3]. Thus, an array of size 2, with an array of size 3 inside. Want more? myArray[2][3][4]. An array of size 2, containing an array of size 3, containing an array of size 4. That&#8217;d be a 3-dimensional table. Too complex for this series! Those are called <strong>multi-dimensional arrays</strong> and are pretty useful to store, as you can guess, tabular data. E.g. students and their corresponding marks. It&#8217;ll be something like this, for simplicity:</p>
<pre class="brush: plain; title: ; notranslate">

[  [ &quot;John&quot; ]  [ 84 ]  ]

[  [ &quot;Ann&quot; ]   [ 89 ]   ]
</pre>
<p>But above, you see how arrays can be used to store inputs and results for output. They&#8217;re not hard to understand and use once you&#8217;ve done it a couple of times.</p>
<p>As you can guess, <strong>arrays have data types</strong> too, and you will need to specify it before you can put any data inside. Some languages restrict arrays so that an array can only contain a single data type. Others don&#8217;t care and you can stuff whatever you want in an array. We&#8217;ll get to the basics of that in the next part on data types.</p>
<p>Careful when using arrays. If you reference a slot that doesn&#8217;t exist or has no data yet, you might either get an error or get junk data. So check where you&#8217;re reading from or writing to to avoid weirdness.</p>
<p>Finally, loops are particularly useful when working with large arrays but this will be for another part!</p>
<p>I guess that&#8217;s it for this part on Variables and Arrays. If you have questions, suggestion etc, just ask in the comments section below. Hope this section was informational for you, and see you next time.</p>
<h3>In the same series:</h3><ul class="eg-series-posts" ><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/04/09/fundamentals-of-programming-part-7-loops/" title="Fundamentals of Programming: Part 7 - Loops">Fundamentals of Programming: Part 7 - Loops</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/03/13/fundamentals-of-programming-part-6-conditions/" title="Fundamentals of Programming: Part 6 - Conditions">Fundamentals of Programming: Part 6 - Conditions</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/29/fundamentals-of-programming-part-5-data-types/" title="Fundamentals of Programming: Part 5 - Data Types">Fundamentals of Programming: Part 5 - Data Types</a></span> </li><li class="eg-series-posts-item current"><span class="eg-series-post-title">Fundamentals of Programming: Part 4 - Variables and Arrays</span> <span class="eg-series-post-this">(This post)</span></li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/07/fundamentals-of-programming-part-3-style-indentation-and-comments/" title="Fundamentals of Programming: Part 3 - Style, Indentation and Comments">Fundamentals of Programming: Part 3 - Style, Indentation and Comments</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/05/fundamentals-of-programming-part-2-pseudo-code-and-batch-jobs/" title="Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs">Fundamentals of Programming: Part 2 - Pseudo Code and Batch Jobs</a></span> </li><li class="eg-series-posts-item "><span class="eg-series-post-title"><a  href="http://www.geekscribes.net/blog/2012/02/04/fundamentals-of-programming-part-1-introduction/" title="Fundamentals of Programming: Part 1 - Introduction">Fundamentals of Programming: Part 1 - Introduction</a></span> </li></ul>
<p>This article comes from <a  href="http://www.geekscribes.net/blog">GeekScribes</a><br/><br/><a  href="http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/">Fundamentals of Programming: Part 4 &#8211; Variables and Arrays</a></p>

<p><a href="http://feedads.g.doubleclick.net/~a/wyK9L3wXkf63CFAO4fvrb5BLAwA/0/da"><img src="http://feedads.g.doubleclick.net/~a/wyK9L3wXkf63CFAO4fvrb5BLAwA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/wyK9L3wXkf63CFAO4fvrb5BLAwA/1/da"><img src="http://feedads.g.doubleclick.net/~a/wyK9L3wXkf63CFAO4fvrb5BLAwA/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/Geekscribes?a=QX82fuftCnc:NraMQRhVBGY:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=QX82fuftCnc:NraMQRhVBGY:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=QX82fuftCnc:NraMQRhVBGY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=QX82fuftCnc:NraMQRhVBGY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=QX82fuftCnc:NraMQRhVBGY:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=QX82fuftCnc:NraMQRhVBGY:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/Geekscribes?i=QX82fuftCnc:NraMQRhVBGY:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/Geekscribes?a=QX82fuftCnc:NraMQRhVBGY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/Geekscribes?d=yIl2AUoC8zA" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/Geekscribes/~4/QX82fuftCnc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.geekscribes.net/blog/2012/02/12/fundamentals-of-programming-part-4-variables-and-arrays/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 2.337 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-13 10:25:59 -->

