Skip to content

Instantly share code, notes, and snippets.

@Colt
Created April 6, 2014 17:40
Show Gist options
  • Save Colt/10009194 to your computer and use it in GitHub Desktop.
Save Colt/10009194 to your computer and use it in GitHub Desktop.

Control Flow

Objectives

  • If Statements
  • Each Loops
  • While Loops
  • Exercises!

Quick Review of Expressions

What's the difference?

x = 5
x == 5

We can make decisions! Add logic to our code

if condition
   something happens
 end

So let's look a super basic example

puts "Hello, what's your name? "
name = gets.chomp
puts "Hello, #{name}"
if name == 'Colt'
  puts "What a great name! "
end

Now let's add an else condition

puts "Hello, what's your name? "
name = gets.chomp
puts "Hello, #{name}"
if name == 'Colt'
  puts "What a great name! "
else
  puts "That's a pretty good name too!"
end

Elsif Statements

num = 20
average = 50

if num < average
  puts "#{num} is below average"
elsif num > average
  puts  "#{num} is above average"
else
  puts  "#{num} is exactly average"
end

Since ruby is all about being human readable. It also allows us to do one line control flow statements.

num = 10
max = 5

# Max is now 10
max = num if num > max

Also, since ruby is human readable, control flow statements generally have an opposite. For if, the opposite is unless

num = 1
min = 5

# Min is now 1
min = num unless num > min

Typically you use the control flow statements that sounds most natural in english.

Exercise

Write a calculator program that takes a number, an operation +,-,/,*, then another number. The program should be able to handle decimal numbers. Below is sample output:

Enter a number, a +,-,/,*, then another number.
7
/ 
2
Result: 3.5

Loops

Loops are essential to programming. They allow us to repeat an operation many times. Typically, execution of a loop lasts as long as a certain value holds true or as long as a set of data is being iterated over. Below is an example of printing the values from 0 to 10 done using many different methods. Note that 0..10 is a Range type, which was discussed earlier.

Times do loops

9.times do
  puts "HELLO"
end

For Loops

for i in 0..10
  puts "The value of i is now #{i}"
end
## Exercise: 99 bottles of beer
count = 99
while count > 0
  puts "#{count} bottles of beer on the wall"
  count = count -1
  puts "Take one down, pass it around #{count} bottles of beer on the wall"
end


Iteration using a block

(0..10).each do |i| puts "The value of i is now #{i}" end


A more condensed way to iterate using a block.

(0..10).each { |i| puts "The value of i is now #{i}" }


Iterating with a while loop

i = 0 num = 10

while i <= num do puts "The value of i is now #{i}" i += 1 end

## While Loops

command = ''

while command != 'bye' puts command command = gets.chomp end

puts 'Come again soon!'


Iterating until a value is true

i = 0 num = 10

until i > num puts "The value of i is now #{i}" i += 1 end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment