<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Man With Code &#187; when</title>
	<atom:link href="http://manwithcode.com/tag/when/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Thu, 09 Feb 2012 03:57:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Programming With Ruby Episode 9, Flow Control</title>
		<link>http://manwithcode.com/149/programming-with-ruby-episode-9-flow-control/</link>
		<comments>http://manwithcode.com/149/programming-with-ruby-episode-9-flow-control/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 10:24:19 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[case]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[else]]></category>
		<category><![CDATA[flow]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[until]]></category>
		<category><![CDATA[when]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=149</guid>
		<description><![CDATA[Part 1: Part 2: Covered in this episode: Code Blocks if, else, unless case, when while, until, for loops Transcript: Hello everybody and welcome to Programming With Ruby Episode 9, Flow Control. I&#8217;m Tyler and this video is brought to you by manwithcode.com So far all of the programs and code that I have shown [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Part 1:</strong><br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/6uMw60C7tyY&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/6uMw60C7tyY&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Part 2:</strong><br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/zpqByOutHVU&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/zpqByOutHVU&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered in this episode:</strong></p>
<ul>
<li>Code Blocks</li>
<li>if, else, unless</li>
<li>case, when</li>
<li>while, until, for loops</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello everybody and welcome to Programming With Ruby Episode 9, Flow<br />
Control. I&#8217;m Tyler and this video is brought to you by manwithcode.com</p>
<p>So far all of the programs and code that I have shown you has been<br />
completely linear. There has been no way to loop based on a condition<br />
or do something based on what the user inputs.</p>
<p>In this episode I will be showing you how to do all this, and I will<br />
also be explaining to you what a code block is, since I&#8217;ve been<br />
mentioning them in almost every episode previous to this.</p>
<p>This episode is REALLY LONG! Feel free to pause the video and think<br />
about what&#8217;s been said or try out code yourself.</p>
<p><strong>Code Blocks</strong></p>
<p>Throughout the previous videos, I&#8217;ve been telling you about code<br />
blocks, I&#8217;ve also been telling you I&#8217;ll teach you what they are in a<br />
later episode. That episode has finally come.</p>
<p>Code blocks look something like this:</p>
<pre class="brush: ruby; title: ; notranslate">
my_array.each do |item|
    puts item
end
</pre>
<p>or like this:</p>
<pre class="brush: ruby; title: ; notranslate">
my_array.each { |item| puts item }
</pre>
<p>Both of those actually do the same thing! The .each method takes a<br />
block as an argument. The block is what is between the do and end<br />
keywords, or the curly braces, depending on which format you use.</p>
<p>The item between the pipe characters (which is above the ENTER key) is<br />
the variable the .each method gives you (in this case the item in the<br />
array).</p>
<p>The rest of the code block is just normal Ruby code. Not so complicated, eh?</p>
<p>(Keep in mind that there are more metheds besides .each that take code blocks)</p>
<p><strong>If, Else, and Unless</strong></p>
<p>A basic if statement would look like this:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
if x &lt; 5
    # Do something
end
</pre>
<p>The &#8220;x &lt; 5&#8243; is the condition that has to be true for the code to<br />
run. If that condition is true, the code between the &#8220;if&#8221; and &#8220;end&#8221;<br />
keywords is run.</p>
<p>There are many different conditional operators you can use:</p>
<pre class="brush: ruby; title: ; notranslate">
==
&lt;
&gt;
&lt;=
&gt;=
!=
</pre>
<p>Just be sure to keep in mind that the &#8220;is equal&#8221; operator uses two<br />
equal signs, not one.</p>
<p>If you would like code that is run when the condition is false:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
if x &lt; 5
    # Do something if true
else
    # Do something if false
end
</pre>
<p>In a similar way you can execute code if the first condition is false<br />
but a second is true:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
if x &lt; 5
    # Do something if true
elsif x == 3
    # Do something if the first is false and this is true
else
    # Do something in all are false
end
</pre>
<p>In a different way you can only execute the code if two conditions are true:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
y = 2
if x == 3 and y == 2
    # Do something
end
</pre>
<p>You can also only execute code if one of any conditions are true:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
y = 4
if x == 3 or y == 2
    # Do something
end
</pre>
<p>There is also the evil twin brother of if, unless:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
unless x == 3
    # if x is 3, this code will not run
end
</pre>
<p>You can chain all of these together in almost any way you choose.</p>
<p><strong>Case, When</strong></p>
<p>Another way to evaluate conditions is using case, when:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 3
case x
    when 1 then
        # do something
    when 3 then
        # do something
    else
        # do something if none are true
end
</pre>
<p>You can&#8217;t do many complex conditionals, but it can be nicer than a<br />
long chain of if, else&#8217;s</p>
<p><strong>while, until, and for loops</strong></p>
<p>Similarly to the above if statements, while, until, and for loops will<br />
execute code based on a condition, but they will do it multiple times.</p>
<p>while:</p>
<pre class="brush: ruby; title: ; notranslate">
x = 1
while x &lt; 5
    x += 1
    puts x
end
</pre>
<p>Similar to the relationship between if, and unless. while has an evil<br />
sister, until</p>
<pre class="brush: ruby; title: ; notranslate">
x = 1
until x &gt; 5
    x += 1
    puts x
end
</pre>
<p>There are also for loops, which allow you to iterate over something</p>
<pre class="brush: ruby; title: ; notranslate">
foods = [&quot;ham&quot;, &quot;eggs&quot;, &quot;cheese&quot;]
for food in foods
    puts food += &quot; is yummy!&quot;
end
</pre>
<p>There is also .each, for&#8217;s evil stepchild:</p>
<pre class="brush: ruby; title: ; notranslate">
foods = [&quot;ham&amp;&quot;, &quot;eggs&quot;, &quot;cheese&quot;]
foods.each do |food|
    puts food += &quot; is yummy!&quot;
end
</pre>
<p><strong>Concrete Example</strong></p>
<p>A menu system</p>
<pre class="brush: ruby; title: ; notranslate">
input = &quot;&quot;
until input == 'quit'
    puts &quot;
Menu
(1) Hi!
(2) Credits
(3 or quit) Exit
&quot;
    input = gets.chomp!
    case input
        when &quot;1&quot; then
            puts &quot;Hi!&quot;
        when &quot;2&quot; then
            puts &quot;Written By: Tyler&quot;
        when &quot;3&quot; then
            input = 'quit'
        else
            puts &quot;Invalid input&quot;
    end
end
</pre>
<p><em>Lets break it down.</em></p>
<p>First we set the input variable to an empty string (so the second line<br />
dosen&#8217;t give us an error)</p>
<p>Then we use an until loop that quits when input is equal to &#8216;quit&#8217;</p>
<p>Next we print the menu (which is unindented so it doesn&#8217;t print to the<br />
screen indented)</p>
<p>After that we get input from the user, use .chomp! to remove the<br />
newline character created from pressing ENTER, and put the input into<br />
the input variable</p>
<p>Then we have a case statement, when &#8220;1&#8243; we print &#8220;Hi!&#8221;, when &#8220;2&#8243; we<br />
print who it was created by, when &#8220;3&#8243; we quit, otherwise we print out<br />
&#8220;Invalid input&#8221; to tell the user they entered something wrong.</p>
<p>This brings us to the end of the video.</p>
<p>If you like these videos please donate, because I&#8217;m doing this all for free</p>
<p>If you have any questions, comments, or suggestions, you can leave a<br />
comment on this page. Or you can email me at tyler@manwithcode.com</p>
<p>I covered a lot of material in this episode and I urge you to watch it<br />
again, go to the original post and look at the code (link is in the<br />
description) and write some code yourself.</p>
<p>Thank you very much for watching, goodbye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/149/programming-with-ruby-episode-9-flow-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

