Branching

Branching is a simple concept, but it’s powerful. In fact, it’s so simple that I bet I don’t even have to explain it at all; I’ll just show you:

puts ​'Hello, what​'​s your name?'
name = gets.chomp
puts ​'Hello, '​ + name + ​'.'
if​ name == ​'Chris'
puts ​'What a lovely name!'
end
<= Hello, what's your name?
=> Chris
<= Hello, Chris.
 What a lovely name!

But if we put in a different name…

<= Hello, what's your name?
=> Chewbacca
<= Hello, Chewbacca.

And that is branching. If what comes after the if is true, we run the code between the if and the end. If what comes after the if is false, we don’t. Plain and simple.

I indented the code between the if and the end just because I think it’s easier to keep track of the branching that way. Almost all programmers do this, regardless of what language they are programming in. It may not seem that helpful in this simple example, but when programs get more complex, it makes a big difference. Often, when people send me programs that don’t work but they can’t figure out why, it’s something that is both:

  • obvious to see what the problem is if the indentation is nice, and

  • impossible to see what the problem is otherwise.

So, try to keep your indentation nice and consistent. Have your if and end line up vertically, and have everything between them indented. I use an indentation of two spaces.

Often, we would like a program to do one thing if an expression is true and another if it is false. That’s what else is for.

puts ​'I am a fortune-teller. Tell me your name:'
name = gets.chomp
if​ name == ​'Chris'
puts ​'I see great things in your future.'
else
puts ​'Your future is...oh my! Look at the time!'
puts ​'I really have to go, sorry!'
end
<= I am a fortune-teller. Tell me your name:
=> Chris
<= I see great things in your future.

Now let’s try a different name:

<= I am a fortune-teller. Tell me your name:
=> Boromir
<= Your future is...oh my! Look at the time!
 I really have to go, sorry!

And one more:

<= I am a fortune-teller. Tell me your name:
=> Ringo
<= Your future is...oh my! Look at the time!
 I really have to go, sorry!

Branching is kind of like coming to a fork in the code: do we take the path for people whose name == 'Chris', or else do we take the other, less fortuitous, path? (Well, I guess you could also call it the path of fame, fortune, and glory. But it’s my fortune-teller, and I say it’s less fortuitous. So there.) Clearly, branching can get pretty deep.

Just like the branches of a tree, you can have branches that themselves have branches, as we can see on the next page.

puts ​'Hello, and welcome to seventh grade English.'
puts ​'My name is Mrs. Gabbard. And your name is....?'
name = gets.chomp
if​ name == name.capitalize
puts ​'Please take a seat, '​ + name + ​'.'
else
puts name + ​'? You mean '​ + name.capitalize + ​', right?'
puts ​'Don​'​t you even know how to spell your name??'
reply = gets.chomp
if​ reply.downcase == ​'yes'
puts ​'Hmmph! Well, sit down!'
else
puts ​'GET OUT!!'
end
end
<= Hello, and welcome to seventh grade English.
 My name is Mrs. Gabbard. And your name is....?
=> chris
<= chris? You mean Chris, right?
 Don't you even know how to spell your name??
=> yes
<= Hmmph! Well, sit down!

Fine, I’ll capitalize my name:

<= Hello, and welcome to seventh grade English.
 My name is Mrs. Gabbard. And your name is....?
=> Chris
<= Please take a seat, Chris.

Sometimes it might get confusing trying to figure out where all the ifs, elses, and ends go. What I do is write the end at the same time I write the if. So, as I was writing the previous program, this is how it looked first:

puts ​'Hello, and welcome to seventh grade English.'
puts ​'My name is Mrs. Gabbard. And your name is....?'
name = gets.chomp
if​ name == name.capitalize
else
end

Then I filled it in with comments, stuff in the code the computer will ignore:

puts ​'Hello, and welcome to seventh grade English.'
puts ​'My name is Mrs. Gabbard. And your name is....?'
name = gets.chomp
if​ name == name.capitalize
# She's civil.
else
# She gets mad.
end

Anything after a # is considered a comment (unless, of course, the # is in a string). After that, I replaced the comments with working code. Some people like to leave the comments in; personally, I think well-written code usually speaks for itself. (The trick, of course, is in writing well-written code.) I used to use more comments, but the more “fluent” in Ruby I become, the less I use them. I actually find them distracting much of the time. It’s a personal choice; you’ll find your own (usually evolving) style.

Anyway, my next step looked like this:

puts ​'Hello, and welcome to seventh grade English.'
puts ​'My name is Mrs. Gabbard. And your name is....?'
name = gets.chomp
if​ name == name.capitalize
puts ​'Please take a seat, '​ + name + ​'.'
else
puts name + ​'? You mean '​ + name.capitalize + ​', right?'
puts ​'Don​'​t you even know how to spell your name??'
reply = gets.chomp
if​ reply.downcase == ​'yes'
else
end
end

Again, I wrote the if, else, and end all at the same time. It really helps me keep track of “where I am” in the code. It also makes the job seem easier because I can focus on one small part, such as filling in the code between the if and the else. The other benefit of doing it this way is that the computer can understand the program at any stage. Every one of the unfinished versions of the program I showed you would run. They weren’t finished, but they were working programs. That way I could test them as I wrote them, which helped me see how my program was coming along and where it still needed work. When it passed all the tests, I knew I was done.

I strongly suggest you approach your programs in this way. These tips will help you write programs with branching, but they also help with the other main type of flow control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.187.201