0

I'm curious if there's a way to have the program go back up the if statement stack?

Ideally, the program would return to line 2 and prompt the user for the input variable, then continue to evaluate like it did the first time. Think of it like a cursor in a text editor, I just want to move it from either of those two comments back up to line 2. The two places of interest are commented out below:

while true
input = gets.chomp
    if input != input.upcase
        puts "HUH?! SPEAK UP, SONNY!"
    elsif input == 'BYE'
        puts "HUH?! SPEAK UP, SONNY!"
        input = gets.chomp
        if input == 'BYE'
            puts "HUH?! SPEAK UP, SONNY!"
            input = gets.chomp
                if input == 'BYE'
                    puts "GOOD BYE!"; 
                    break   
                else
                    # return to top-level if statement
                end     
        else
            # return to top-level if statement
        end
    else
        random_year = rand(1930..1950)
        puts "NO, NOT SINCE #{random_year}!"
    end
end
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
bwobst
  • 340
  • 3
  • 13
  • What do you mean by "return to top-level if statement"? What should happen next? – Femaref Aug 04 '13 at 14:38
  • In this code, if you will always return to top level if statement(and not to `input=gets.chomp`), it will loop forever, I have a strong feeling suggesting that. – Anton Aug 04 '13 at 14:39
  • This is really awful way of writing a sequential code – Mchl Aug 04 '13 at 14:40

4 Answers4

0

You need to use a while statement to set a condition flag and check it, which will loop back to the while statement if you don't change the flag:

flag = 0
while flag1 == 0
  if var = "string"
  then ...statements...
  flag1 = 1      ; this allows us to break out of this while loop
  else ...statements...
  end
end

If flag1 is not 0 at the end of the while statement, the while statement will loop back. For two such conditions, you need to nest the while loops. You might have to re-order your statements to make multiple while loops work this way.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jody Bruchon
  • 885
  • 6
  • 11
0

In the code you show, you don't need to do anything to make the flow of execution go back to line 2. Just omit the else clauses in the two places you marked. The flow of execution will drop down to the bottom of the while loop, then loop back to the top, then go back to line 2.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alex D
  • 29,755
  • 7
  • 80
  • 126
0

You can avoid this level of neasted ifs with:

byecount = 0
while byecount < 3
  input = gets.chomp

  if input == "BYE"
    byecount += 1
    next
  else
    byecount = 0
  end

  if input != input.upcase
    puts "HUH?! SPEAK UP, SONNY!"
  else
    puts "NO, NOT SINCE #{rand(1930..1950)}!"
  end
end
puts "GOOD BYE!"

Or you can write a catch..throw flow structure. (Really.. if you need to use it, something is wrong with your design)

catch :exitloop do
  while ...
    if ...
      if ...
        if ...
          throw :exitloop
        end
      end
    end
  end
end
Guilherme Bernal
  • 8,183
  • 25
  • 43
0

Here's how I'd write a similar exercise:

BYE = 'BYE'
HUH = "HUH?! SPEAK UP, SONNY!"

loop do
  input = gets.chomp

  if input != input.upcase
    puts HUH
    next
  end

  if input != BYE
    random_year = rand(1930..1950)
    puts "NO, NOT SINCE #{random_year}!"
    next
  end

  puts HUH
  input = gets.chomp

  if input == BYE

    puts HUH
    input = gets.chomp

    if input == BYE

      puts "GOOD BYE!"; 
      break   

    end     

  end

end

I used loop instead of while. Matz, the main man for Ruby, recommends loop. See "Is there a “do … while” loop in Ruby?" for further discussion about it.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • The links you posted says nothing against `while ... end`. He is against `begin ... end while ...`, because it does not do what is looks like at first look. (I prefer `loop do` too) – Guilherme Bernal Aug 04 '13 at 17:17