Skip to content

Latest commit

聽

History

History
64 lines (47 loc) 路 867 Bytes

06-control-flow.md

File metadata and controls

64 lines (47 loc) 路 867 Bytes

Control Flow

If / Elsif / Else

score = 85

if score >= 90
  puts "A"
elsif score >= 80
  puts "B"
else
  puts "C"
end

Postfix if

For one-liners, you can put the condition after the statement:

puts "big" if score >= 90
x = 42 if ready

Comparison & Logic

if x > 0 && x < 100
  puts "in range"
end

if name == "admin" || name == "root"
  puts "superuser"
end

if !done
  puts "still working"
end

Operators: ==, !=, <, >, <=, >=, &&, ||, !

|| and && return values

Like Ruby, || returns the first truthy value and && returns the last truthy value (or the first falsy one). This makes || great for defaults:

name = input || "anonymous"
config = load_config() || {}

While

i = 0
while i < 5
  puts i
  i += 1
end

Next: For Loops