Ruby Control Structures Venkat Subramaniam – svenkat@cs.uh.edu 1 Statements • Statements don’t need ; • You write one statement per line (like you normally do anyways) • If you want to put multiple statements on a line, you may use the optional ; Venkat Subramaniam – svenkat@cs.uh.edu 2
Conditions • if statement does not need { } • Short forms Venkat Subramaniam – svenkat@cs.uh.edu 3 Looping • while loop is similar to what you’re used to More concise form Venkat Subramaniam – svenkat@cs.uh.edu 4
For loops There are some other cool ways as well, but we’ll have to wait for that until we learn about blocks Venkat Subramaniam – svenkat@cs.uh.edu 5 Notice the Range • We used 1..10 and 1..val • Range is a first class citizen in Ruby • You can create range objects • It comes in handy in various situations… we’ll see these when discussing collections Venkat Subramaniam – svenkat@cs.uh.edu 6
Recommend
More recommend