Control Flow in Ruby

I Introduction to Control Flow

i. How It Works 1/17

Save and Submit

ii. If 2/17

my_num = 4
if my_num > 0 
    print "I'm happy!"
end

iii. Else 3/17

my_num = 4
if my_num > 0 
    print "I'm happy!"
else 
    print "I'm still happy"
end

iv. Elsif 4/17

my_num = 4
if my_num > 0 
    print "I'm happy!"
elsif my_num < 0
    print "Oh! negative"
else 
    print "I'm still happy"
end

v. ‘Unless 5/17

hungry = false

unless hungry
  puts "I'm writing Ruby programs!"
else
  puts "Time to eat!"
end

II Making Comparisons

i. Equal or Not? 6/17

is_true = 2 != 3

is_false = 2 == 3

ii. Less Than or Greater Than 7/17

test_1 = 17 > 16

test_2 = 21 < 30 test_3 = 9 >= 9

test_4 = -11 <= 4

iii. Practice Makes Perfect 8/17

# test_1 = 77 != 77
test_1 = false

# test_2 = -4 <= -4
test_2 = true

# test_3 = -44 < -33
test_3 = true

# test_4 = 100 == 1000
test_4 = false

III Boolean Operators

i. And 9/17

# boolean_1 = 77 < 78 && 77 < 77 boolean_1 = false # boolean_2 = true && 100 >= 100
boolean_2 = true
# boolean_3 = 2**3 == 8 && 3**2 == 9
boolean_3 = true

ii. Or 10/17

# boolean_1 = 2**3 != 3**2 || true
boolean_1 = true

# boolean_2 = false || -10 > -9
boolean_2 = false

# boolean_3 = false || false
boolean_3 = false

iii. Not 11/17

# boolean_1 = !true
boolean_1 = false

# boolean_2 = !true && !true
boolean_2 = false

# boolean_3 = !(700 / 10 == 70)
boolean_3 = false

iv. Combining Boolean Operators 12/17

# boolean_1 = (3 < 4 || false) && (false || true)
boolean_1 = true

# boolean_2 = !true && (!true || 100 != 5**2)
boolean_2 = false

# boolean_3 = true || !(true || false)
boolean_3 = true

IV Review
i. Nice Work! 13/17

Save and Submit

ii. If, Else, and Elsif 14/17

a = 1
b = 2

if a < b     print " Yes a is less than b" elsif b > a
    print "Wrong"
else
    print "No a is not qual to b"
end

iii. Unless 15/17

solved = true
unless solved
    print "Keep Solving"
else 
    print "Problem Solved!"
end

iv. Dare to Compare 16/17

# test_1 should be false
test_1 = 2 > 3

# test_2 = should be false
test_2 = 3 == 4

# test_3 = should be true
test_3 = 5 != 6

iv. Billions of Booleans 17/17

#First exercise is over.
=begin# test_1 should be true
test_1 = 2**3 == 9 || true

# test_2 = should be true
test_2 = 3**2 == 9 || false

# test_3 = should be false
test_3 = 2**3 == 9 || false
Advertisements

One thought on “Control Flow in Ruby

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s