# Need Help with another Simple Program

**URL:** <https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269>\
**Category:** Ruby\
**Created:** [July 18, 2009, 3:51am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269 "2009-07-18T03:51:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 3:51am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/1 "2009-07-18T03:51:59Z")

</div>

As an exercise out of Chris P.'s ‘Learning to Program,’ I’ve attempted  
to build a program that calculates the number of leap years between two  
given years. Here is my code:

puts ‘Use this calculator to determine the number of leap years in a  
specific period.’  
total = 0 # total number of leap years  
puts ‘Enter the first year below:’  
year\_one = gets.chomp.to\_i  
puts ‘Enter the second year below:’  
year\_two = gets.chomp.to\_i

while year\_one.to\_i \< year\_two.to\_i  
if year\_one%4 == 0  
if year\_one%100 != 0 || year\_one%400 == 0  
total = total + 1  
year\_one = year\_one+ 1  
else  
year\_one = year\_one+ 1  
end  
end  
end  
puts 'Between ’ + year\_one.to\_s + ’ and ’ + year\_two.to\_s + ‘, there  
were ’  
puts total.to\_s + ’ leap years.’

- 

This is a different approach than Pine takes (most of my attempts are  
formulated differently, but based around the right concept). He does  
this:  
puts ‘Pick a starting year (like 1973 or something):’  
starting = gets.chomp.to\_i  
puts ‘Now pick an ending year:’  
ending = gets.chomp.to\_i  
puts ‘Check it out… these years are leap years:’  
year = starting  
while year \<= ending  
if year%4 == 0  
if year%100 != 0 || year%400 == 0  
puts year  
end  
end  
year = year + 1  
end

- 

What’s going wrong?

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 4:15am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/2 "2009-07-18T04:15:27Z")

</div>

On Fri, Jul 17, 2009 at 9:52 PM, Max N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> year_one = year_one+ 1
> 
> ```
> 
> end  
> end  
> end
> 
> What’s going wrong?

Consider this snippet:

i = 0  
while i \< 1  
if false  
i += 1 # oops, we never get here  
end  
end

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 6:33am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/3 "2009-07-18T06:33:03Z")

</div>

> Consider this snippet:
> 
> i = 0  
> while i \< 1  
> if false  
> i += 1 # oops, we never get here  
> end  
> end

I’m not sure I understand this. What does that snippet exemplify?

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 6:58am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/4 "2009-07-18T06:58:00Z")

</div>

Max N. wrote:

> > Consider this snippet:
> > 
> > i = 0  
> > while i \< 1  
> > if false  
> > i += 1 # oops, we never get here  
> > end  
> > end
> 
> I’m not sure I understand this. What does that snippet exemplify?

Look at this:

while year\_one.to\_i \< year\_two.to\_i  
if false  
if year\_one%100 != 0 || year\_one%400 == 0  
total = total + 1  
year\_one = year\_one+ 1  
else  
year\_one = year\_one+ 1  
end

Also, what value is stored in the variable year\_one right before the  
while loop starts?

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 11:08am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/5 "2009-07-18T11:08:29Z")

</div>

Max N. wrote:

> I think I’ve determined my problem; it relates to the variable  
> ‘year\_one.’ year\_one is supposed to be the first year entered when the  
> program starts.

And what was year\_one in your first post?

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 7:37am UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/6 "2009-07-18T07:37:11Z")

</div>

> Look at this:
> 
> while year\_one.to\_i \< year\_two.to\_i  
> if false  
> if year\_one%100 != 0 || year\_one%400 == 0  
> total = total + 1  
> year\_one = year\_one+ 1  
> else  
> year\_one = year\_one+ 1  
> end
> 
> Also, what value is stored in the variable year\_one right before the  
> while loop starts?

I think I’ve determined my problem; it relates to the variable  
‘year\_one.’ year\_one is supposed to be the first year entered when the  
program starts.

Check out my revision, which appears to work:

puts ‘Use this calculator to determine the leap years in a specific  
period.’  
puts ‘Enter the starting point below:’  
start = gets.chomp.to\_i  
puts ‘Enter the ending point below:’  
ending = gets.chomp.to\_i

year = start  
total = 0

while year \<= ending  
if year%4 == 0  
if year%100 != 0 || year%400 == 0  
puts year  
total = total + 1  
end  
end

year = year + 1

end

if total == 1  
puts 'There is 1 leap year between ’  
puts start.to\_s + ’ and ’ + ending.to\_s + ‘.’  
else  
puts 'There are ’ + total.to\_s + ’ leap years between ’  
puts start.to\_s + ’ and ’ + ending.to\_s + ‘.’  
end

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/7 "2009-07-18T17:32:10Z")

</div>

On Sat, Jul 18, 2009 at 12:33 AM, Max N.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > Consider this snippet:
> > 
> > i = 0  
> > while i \< 1  
> > if false  
> > i += 1 # oops, we never get here  
> > end  
> > end
> 
> I’m not sure I understand this. What does that snippet exemplify?

In this snippet the loop exit condition will never be occur because i  
will not be incremented. Your original version had a similar error:

while  
if # when this is false  
if  
year\_one = year\_one+ 1 # we never get to here  
else  
year\_one = year\_one+ 1 # or here, so we keep looping  
end#inner if  
end#outer if  
end#while

I wasn’t sure how much you would want to be given away, so I tried to  
keep my example abstract. 🙂

---

<div class="post-metadata">

**Author:** ![maxnorman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@maxnorman](https://www.ruby-forum.com/u/maxnorman)\
**Post date:** [July 18, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/need-help-with-another-simple-program/172269/8 "2009-07-18T17:42:41Z")

</div>

> I wasn’t sure how much you would want to be given away, so I tried to  
> keep my example abstract. 🙂

I appreciate your courtesy. I figured out my the problem a little while  
ago, but your explanation is still useful.
