Help with leap year programing

Thanks for the pointers Eduardo!

The new code here works.

puts “Enter starting year:”
starting_year = gets.chomp.to_i
puts “Enter ending year:”
ending_year = gets.chomp.to_i
year = starting_year
while true
if year%4==0
if year%100!=0 || year%400 ==0
puts year.to_s + ’ is a Leap Year’
end
end
year = year +1
break if year >= ending_year
end

unknown wrote in post #1093610:

Am 24.01.2013 02:01, schrieb Kristine L.:

while true
if year%4==0
if year%100!=0 || year%400 ==0
puts year.to_s + ’ is a Leap Year’
end
end
year = year +1
break if year >= ending_year
end

Usually you would not use a while loop for tasks like this,
try for example:

print "Enter starting year: "
starting_year = gets.chomp.to_i
print "Enter ending year: "
ending_year = gets.chomp.to_i

starting_year.upto(ending_year) do |year|
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
puts “#{year} is a leap year”
end
end

which saves 3 lines of code and some possibilities for typos/bugs.

BTW, for an infinite loop there exists `loop do … end’.

thanks for posting this solution, I like it better as it saves lines of
code, it looks clean. I am 58 and new to programming with ruby being my
first language. I also just got my ccna but there doesn’t seem to be any
work in networking, I didn’t understand other languages but I seem to be
getting ruby. this is great!!