Expand All

conditionals.step

goals do
  goal 'Use `gets` to get input from the user of your program.'
  goal 'Use a conditional statement to execute a branch of code only some of the time.'
  goal 'if/elsif/else, while, unless'
end


step do
  message "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_in_file 'conditionals_test.rb', <<-'CONTENTS'
print "How many apples do you have? > "
apple_count = gets.to_i
puts "You have #{apple_count} apples."
  CONTENTS
end

step do
  message 'Continuing on from the end of conditionals_test.rb...'
   type_in_file 'conditionals_test.rb', <<-'CONTENTS'
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
  CONTENTS
  console 'ruby conditionals_test.rb'
  message '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.'
  message '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.'
end


step do
  message 'What goes after the `if` is any expression that returns a **boolean**, (the values `true` or `false`). '
end


step do
    message "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'."

    message 'Create a new file called while_loop.rb'
    type_in_file 'while_loop.rb', <<-'CONTENTS'
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}!"
    CONTENTS
    console 'ruby while_loop.rb'

    message "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**"
end

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

  message "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."

  message '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.'

  source_code <<-'CONTENTS'
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
CONTENTS

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

  message "Here is a sample of how the program might run."

  source_code <<-'CONTENTS'
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
CONTENTS
end

explanation do
  message "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."
end

next_step 'nil'