﻿ Section 14.4.  Flow-Control Statements

Free JavaScript Editor     JavaScript Debugger ﻿

Main Page

### 14.4. Flow-Control Statements

In any type of nontrivial program, flow control is possibly the most important factor in programming. Without some kind of flow control in programming languages, computers would essentially be very expensive desktop ornaments. Come to think of it, when you got past the forwarding of every e-mail received each day to his team, I once had a manager whose computer was a very expensive desktop ornament. He actually once forwarded the same message 14 times before realizing that he had somehow been added to his address list for the team. But I'm wandering, so let's get back to flow control, starting with conditions.

#### 14.4.1. Conditions

In your average run-of-the-mill language, there is the if statement, and that is pretty much all there is to it. Ruby has an if that looks something like this:

```if x == 1
b = 2
end
```

Pretty easy. Let's add a layer of complexity with an else:

```if x == 1
b = 2
else
b = 3
end
```

In Ruby, it is also possible to take it to a higher degree of complexity by using the elsif statement:

```if x == 1
b = 2
elsif x == 2
b = 4
else
b = 3
end
```

Before I forget, for the purpose of clarity, Ruby permits the addition of a then to the if statement:

```if x == 1 then
b = 2
end
```

Remember all the way back to Chapter 4, "JavaScript"? Remember conditional operators? Well, they're back! In fact, here is an example:

`b = (x == 1 ? 2 : 3)`

A few years ago, I grew a goatee, which I have since shaved off. At the time, my reason for growing it was strictly personal and strange. You see, I wanted to pass myself off as the evil Ed from a parallel dimension. My plan for work domination failed, but it gave me the opportunity to appreciate the evil things from parallel dimensions. For example, did you know that Ruby has an evil if called unless?

The unless statement executes the code within only when the condition is false. If this doesn't fit the textbook, or, at least Star Trek, example of something from a parallel dimension, I don't know what does.

#### 14.4.2. Looping

Some days I feel like I'm going around in circles, usually in the morning while I'm getting ready for work. The problem probably stems from a deep-seated need for coffee to get moving in the morning. This wasn't always the case, but back in high school, I worked in a pancake house and got hooked. The free coffee just seemed to helpthat is, until I drank fifteen 20-ounce cups in the course of a day. I could have threaded a sewing machine needle while the machine was running. It hasn't been that bad in a while, but my morning ritual still requires coffee, as Ruby, shown in Listing 14-2, illustrates.

##### Listing 14-2. My Morning in Ruby: while Loop

 ```cupsofcoffee = 0 while cupsofcoffee < 4 puts "hurry..." cupsofcoffee += 1 end ```

The great thing about describing one's morning programmatically is that there are always alternative ways of expressing one's self. For example, some mornings the blanket monster is holding me back and I just can't seem to get moving until there is a certain level of coffee in my system. Mornings like these are better expressed by the code shown in Listing 14-3.

##### Listing 14-3. My Morning in Ruby: until Loop

 ```cupsofcoffee = 0 until cupsofcoffee >= 4 puts "hurry..." cupsofcoffee += 1 end ```

A while back, I used to have one of those coffee pots that had a timer. On those mornings when I had programmed it the night before, coffee was already going. Ah, a set number of cups of coffee just waiting for cream and sugar. I suppose Listing 14-4 best sums it up.

##### Listing 14-4. My Morning in Ruby: for/in Loop

 ```puts "for-in loop" for x in ["hurry...", "hurry...", "hurry...", "hurry..."] puts x end ```

Nowadays, I have one of those coffee makers that takes a Pod. Just drop in the Pod and hit the button, and 90 seconds later there's coffee. This takes making coffee from being an art to being more of a science, a feeling that is best conveyed by the example shown in Listing 14-5.

##### Listing 14-5. My Morning in Ruby: for/in Loop

 ```puts "Iterators" 1.step(4,1) do |x| puts "hurry..." end ```

﻿