Expand All

Conditionals

Goals

  • Use gets to get input from the user of your program.

  • Use a conditional statement to execute a branch of code only some of the time.

  • if/elsif/else, while, unless

Step 1

In this section, we'll modify the file we've created in previous steps which we called conditionals_test.rb. Verify that it has only the following contents.

Type this in the file conditionals_test.rb:
print "How many apples do you have? > "
apple_count = gets.to_i
puts "You have #{apple_count} apples."

Step 2

Continuing on from the end of conditionals_test.rb...

Type this in the file conditionals_test.rb:
print "How many apples do you have? > "
apple_count = gets.to_i

if apple_count > 5
  puts "Lots of apples!"
else
  puts 'Not many apples...'
end
Type this in the terminal:
ruby conditionals_test.rb

Sometimes, we want our programs to make decisions for us. We call this concept flow control and it is found in most object oriented programming languages. The if ... else ... end construct is a way of selectively executing code based on values that exist in the program.

Try running the program with different values for apple_count to see each side of the conditional get executed. Notice how if you enter an apple count less than or equal to 5, Not many apples... is outputted. If you enter an apple count greater than 5, then Lots of apples! is outputted. The results of the expression apple_count > 5 returns true or false, and with the use of the if statement, your program can start to make decisions as to what parts of the program to execute.

Step 3

What goes after the if is any expression that returns a boolean, (the values true or false).

Step 4

A while loop continues repeating until a certain statement is false. Here, the program continually asks us for numbers until we say the string 'stop'.

Create a new file called while_loop.rb

Type this in the file while_loop.rb:
total = 0
user_input = nil
while user_input != 'stop'
  print 'Enter a number to add to the total. > '
  user_input = gets.chomp
  total = total + user_input.to_i
end
puts "Your final total was #{total}!"
Type this in the terminal:
ruby while_loop.rb

It's easy for a while loop to get out of control! If your loop body doesn't do anything to make the while condition false, your loop will run forever. Programmers call this an infinite loop. If you get into an infinite loop, you can terminate the program by typing ctrl+c

Challenge(s)

Let's extend the program you created in the numbers and arithmetic assignment to perform more mathematical operations.

Using what you've learned about flow control, allow the user to select whether they would like two numbers to be added, subtracted, multiplied, or divided.

Here is a sample of how the program might run. The > has been used below to indicate that the user should input the value proceeding it.

What is the first number?
> 2
What is the second number?
> 4
Would you like to add (1), subtract (2), multiply (3), or divide (4) these numbers?
> 3
The product is 8

As an optional challenge, can you modify this program to continuously ask for numbers?

Here is a sample of how the program might run.

What number do you want to include?
> 2
What number do you want to include?
> 4
What number do you want to include?
> 6
What number do you want to include?
> DONE
Would you like to add (1), subtract (2), multiply (3), or divide (4) these numbers?
> 3
The product is 48

Explanation

Without some kind of conditional, your program would do the same thing every time. Conditionals let you choose to do different things depending on what data you have in hand.

Next Step: