Home > Programming > Learning Python and Ruby – Basic Flow Control

Learning Python and Ruby – Basic Flow Control

Getting the hang of very basic flow control: branching, for-looping, and while-looping.

JavaScript

if (number > 0) {
    alert('Number is positive.');
}
else if (number == 0) {
    alert('Number is zero.');
}
else {
    alert('Number is negative.');
}

Python

if number > 0:
    print('Number is positive.')
elif number == 0:
    print('Number is zero.')
else:
    print('Number is negative.')

Ruby

if number > 0
    puts 'Number is positive.'
elsif number == 0
    puts 'Number is zero.'
else
    puts 'Number is negative.'
end

Nothing new here, except that the elif and elsif are a bit weird to use. Noteworthy is Ruby’s unless statement – it is exactly like an if statement, but the condition is inverted.

Other branching alternatives include the switch in JavaScript, and the case in Ruby.

 

JavaScript

var list = ['Google', 'Bing', 'Duck Duck Go'];
for (var i in list) {
    alert(list[i]);
}
for (var i = 0; i < list.length; i++) {
    alert(list[i]);
}

Python

list = ['Google', 'Bing', 'Duck Duck Go']
for i in list:
    print i
for i in range(len(list)):
    print list[i]

Ruby

list = ['Google', 'Bing', 'Duck Duck Go']
list.each do |i|
    puts i
end
for i in 0..list.length-1
    puts list[i]
end

The first loop is looping through the objects of a list, while the second is looping through the indexes. It is to be noted that in Javascript, i is not a reference to the current object, but the index in the list. You have to manually get the current object (list[i]) if you want the reference. In python, a for-loop is normally iterating through a list – to iterate through numbers, you can use range(5) to iterate from 0 to 4. The equivalent in Ruby is 0..4, and you iterate through a list by using the .each method of the list.

I think the Javascript for-loop is the most powerful, while the Python’s is the easiest on the eyes. I wish the len() was a list property, though.

 

JavaScript

var i = 0;
while (i < 4) {
    alert(i);
    i++;
}
do {
    alert(i);
    i++;
} while (i < 8);

Python

i = 0
while i < 4:
    print(i)
    i = i + 1

Ruby

i = 0
while i < 4
    puts i
    i = i + 1
end
begin
    puts i
    i = i + 1
end while i < 8

The first loop is a while loop, and the second is a do-while loop. The difference is that the do-while loop does at least one iteration – it goes in before testing the condition. I did not find any do-while loop in Python, which reinforces the belief that Python is a very simple programming language to use. On Ruby’s side, the while loop looks very normal, but the do-while seems to be made from a begin-end scope, with a while condition appended to the end. This looks weird to my eyes. Ruby also has an until statement and modifier, which are exactly like the while and do-while statements, but with the condition inverted.

I tend to avoid these spaghetti code generators if I can, but break and continue can be used in JavaScript to break out of the innermost loop, or stop the current iteration and continue with the next one. What’s available in Python and Ruby? Python has both break and continue. Ruby has break and next, but also has redo which restarts the current iteration (skipping the condition check), and retry which restarts the whole loop, from the beginning. All these (in Ruby only) can be appended a condition, eg. break if i == 7.

I have the feeling that Python is trying to be simple, as Ruby is trying to be more like a spoken language. I’m not used to such programming candies such as, for example, redo and while / until. I use some code to programmatically simulate the if condition of a break. I’m not yet sure if I should be irritated by these, or just be glad and use them.

Categories: Programming Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.