-- A guessing game
-- Demonstrates some if and case statements
-- Shows if, if-else, if,elsif,else
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure if_and_case is
value_to_guess: constant := 23;
n: Integer;
guesses: Natural;
got_it: Boolean;
begin
guesses := 0;
loop
put("Enter an integer: ");
get(n);
guesses := guesses + 1;
if n <= 0 then
put_line("You think outside the box - I like that!");
end if; -- End if required
if abs n < 1000 then
put_line("You like to think small!!");
else
put_line("You like to think big!!");
end if;
if n < value_to_guess then
put_line("Too small.");
put_line("You need to think bigger!.");
elsif n > value_to_guess then
put_line("Too large.");
put_line("Scale it back!!.");
else -- else optional
put_line("You got it!");
got_it := true;
end if;
exit when got_it;
end loop;
case guesses is
when 0 =>
put_line("Oh no, something's wrong!"); -- No break!
when 1 =>
put_line("Wow! You got it first time!");
when 2 | 4 | 6 | 8 | 10 =>
put_line("Wow! Only a few guesses!");
put_line("You seem quite even tempered!");
when 3 | 5 | 7 | 9 =>
put_line("Wow! Only a few guesses!");
put_line("You must have an odd strategy!");
when 11 .. 20 =>
put_line("Binary search helps, doesn't it!");
when 21 .. 23 | 25 =>
put_line("We needed one more example!");
when others => -- Required if not all values accounted for
put_line("Remember to use binary search strategy!");
end case;
end if_and_case;