Giter VIP home page Giter VIP logo

while-and-until-looping-readme's Introduction

The while and until Constructs

Objectives

  1. Describe the while construct and how it implements looping
  2. Describe the until looping construct

while

The while construct is a little different from the loop construct that we looked at earlier. The while construct will keep executing a block as long as a specific condition is true.

Let's look at a long and repetitiously-counting code that uses if statements to count from 0 to 20 and outputs "The current number is less than 20." if so. Well, we can refactor that into simple, readable, short code with the while construct:

counter = 0
while counter < 20
  puts "The current number is less than 20."
  counter += 1
end

Think about the above code like this:

  • While it is true that the variable counter is set to a value that is less than 20, execute the code in the block.
  • Inside the block, puts a phrase, and increment the counter by one.
  • Go back to the top! Check to see if the counter is less than 20. If it is true that the value is less than 20, go back into the block. Otherwise, break out of the loop and don't execute the code inside the loop.

We can achieve all of that with just a few lines of code utilizing a while construct. Go ahead and copy and paste the above code in irb.

Examples

Basic while Example: Hot Dog Eating Contest

Let's say you are a world famous competitive eater participating in the Coney Island Nathan's Hot Dog Eating Contest in Brooklyn, NY. You're kind of new to the competitive eating game though, so you only have the capacity for seven (7) hot dogs.

num_of_hotdogs_eaten = 0
# => 0 (return value)

while num_of_hotdogs_eaten < 7
  num_of_hotdogs_eaten += 1
  puts "You have now eaten #{num_of_hotdogs_eaten} hot dog(s)."
end
# => nil (return value)

puts "You ate a total of #{num_of_hotdogs_eaten} hot dogs!"
# => nil (return value)

# > "You have now eaten 1 hot dog(s)."
# > "You have now eaten 2 hot dog(s)."
# > "You have now eaten 3 hot dog(s)."
# > "You have now eaten 4 hot dog(s)."
# > "You have now eaten 5 hot dog(s)."
# > "You have now eaten 6 hot dog(s)."
# > "You have now eaten 7 hot dog(s)."

# > "You ate a total of 7 hot dogs!"

until

Until is simply the inverse of a while loop. An until keyword will keep executing a block until a specific condition is true. In other words, the block of code following until will execute while the condition is false. One helpful way to think about it is to read until as "if not".

counter = 0
until counter == 20
  puts "The current number is less than 20."
  counter += 1
end
  • The counter once again starts at 0. If it is not true that the counter is equal to 20, the program will execute the code in the block.
  • Inside the block, we will puts a phrase and increment the counter by 1.
  • Then, the program will go back to the top of the until loop and once again check to see if the counter is equal to 20. If it is not true that the counter is equal to 20, then the program will continue executing the code in the block. Otherwise, the program will break out of the loop.

while-and-until-looping-readme's People

Contributors

annjohn avatar codebyline avatar ihollander avatar joshuabamboo avatar maxwellbenton avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

while-and-until-looping-readme's Issues

no sandbox tab

I have not seen the Sandbox tab in my lesson to follow along with.

Missing code

Seems like example code should follow the following sentence: "Let's look at a long and repetitiously-counting code that uses if statements to count from 0 to 20?"

And should probably not have a question mark.

Discussion of 'times'

In this line:

The while construct is a little different from the first two constructs (loop and times) that we looked at earlier.

Could be wrong, but I don't think times has actually been mentioned yet.

Non-repeatable test for Student name in classroom_show_spec.rb

What I believe is an unintended consequence of using Faker::Name.name is that it occasionally generates an apostrophe in the name. In the way the form gets rendered, the apostrophe becomes an alt-code like so:

   +  <li>
   +    Name: Bria O&#39;Connell
   +  </li>       

Diff:
@@ -1,2 +1,67 @@

   -Bria O'Connell

While I am not certain this isn't intentional, it is a non-repeatable test.

RSpec.describe "classroom_show_view" do
let(:classroom) { Classroom.create(course_name: 'Math', semester: "Spring #{Time.now.year}") }
let(:student){ Student.create(name: 'Bobby', hometown: Faker::Address.city, birthday: Faker::Date.between(25.years.ago, 18.years.ago)) }

before do
5.times do
Student.create(name: Faker::Name.name, hometown: Faker::Address.city, birthday: Faker::Date.between(25.years.ago, 18.years.ago))
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.