CONDITIONALS    Sarah Allen
TRUTH• Truth: Everything is true except for:  – false  – nil• Therefore  – 0 is true  – “” is true
CONDITIONALS: IFif age > 17  puts “can vote”                    Statement Modifiers:end                 do_something if x =...
CONDITIONALS: IFif age >= 21    puts “can drink”elsif age > 17    puts “can vote”else    puts “too young”end
CHECKING FOR FALSEif !(name == “superman”) …if not (name == “superman”)  …
UNLESS• “unless” provides us with another way of checking if a  condition is false:  unless human     status = "superhero"...
CASEcase superhero      when "superman"            city = "metropolis"      when "batman"            city = "gotham_city" ...
CASE REFACTORINGcity = case superhero      when "superman"           "metropolis"      when "batman"           "gotham_cit...
CASEcase num      when 1          puts "one"      when 2..5          puts "that is       small"      else          puts "p...
CASEcase name      when "Sarah"          puts "awesome"      when /Mr.*/          puts "formal guy"      else          put...
CASEcase args    when String      puts "Got a String"    when Array      puts "Got an Array”    when Hash      puts "Got a...
Upcoming SlideShare
Loading in …5
×

Conditionals

618 views
605 views

Published on

Conditionals

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
618
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • Unlike some languages with the 0 and empty string\n! Binds more tightly than than the “not” keyword so you do need parentheses for example 1, but don’t need parentheses for example 2\n
  • Conditionals are key to being able to make decisions in a program\n\nleft looks like every other language\n\nparentheses are optional in ruby \nmake sure to do ==, = is an assignment, == is a conditional test\n\nExplain puts\n\nright is a little different…people in ruby don’t like to type…english readable\nA statement modifier lets you move control structures at the end of an expression.\n
  • Conditionals are key to being able to make decisions in a program\n\nleft looks like every other language\n\nparentheses are optional in ruby \nmake sure to do ==, = is an assignment, == is a conditional test\n\nExplain puts\n\nright is a little different…people in ruby don’t like to type…english readable\nA statement modifier lets you move control structures at the end of an expression.\n
  • \n
  • Unless can be awkward, especially with else. Usually you’ll want to use if for conditionals with else clauses.\n Occasionally unless is more readable:\n unless something is nil\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Conditionals

    1. 1. CONDITIONALS Sarah Allen
    2. 2. TRUTH• Truth: Everything is true except for: – false – nil• Therefore – 0 is true – “” is true
    3. 3. CONDITIONALS: IFif age > 17 puts “can vote” Statement Modifiers:end do_something if x == 4 Other Syntax: if x == 4 then do_something endif age > 17 puts “can vote”else puts “attends school”end
    4. 4. CONDITIONALS: IFif age >= 21 puts “can drink”elsif age > 17 puts “can vote”else puts “too young”end
    5. 5. CHECKING FOR FALSEif !(name == “superman”) …if not (name == “superman”) …
    6. 6. UNLESS• “unless” provides us with another way of checking if a condition is false: unless human status = "superhero" end status = "superhero" unless human
    7. 7. CASEcase superhero when "superman" city = "metropolis" when "batman" city = "gotham_city" else city = "central_city"end
    8. 8. CASE REFACTORINGcity = case superhero when "superman" "metropolis" when "batman" "gotham_city" else "central_city"end
    9. 9. CASEcase num when 1 puts "one" when 2..5 puts "that is small" else puts "pretty big"end
    10. 10. CASEcase name when "Sarah" puts "awesome" when /Mr.*/ puts "formal guy" else puts "whatever"end
    11. 11. CASEcase args when String puts "Got a String" when Array puts "Got an Array” when Hash puts "Got a Hash"end

    ×