Lizzie
Lizzie

Reputation: 179

While Loop and If/Else in PL/SQL

I am trying to write a procedure that will produce the following output

exec WinOrLose(4)

Welcome to the Win or Lose Game. Your number is 4. 

You win.
You lose.
You win.
You lose.

==> You lose!

So far I have this:

CREATE or REPLACE Procedure WinOrLose (
  p_choice number ) AS

  v_answer number;

DECLARE
  v_answer := p_choice

BEGIN
  dbms_output.put_line ('Welcome to the Win or Lose Game. Your number is ' || 
  v_answer);

  FOR v_answer in 1..10 
    IF MOD(v_answer, 2) = 0 THEN -- v_answer is even
          dbms_output.put_line (You lose)

END;
/

I'm unsure of where to go from there. My thought process (psuedocode) is this:

SET v_answer := 1
   While Loop (outside)
     MOD(v_answer,2) = 0 then dbms.output (YOU LOSE)
   ELSE
     dbms.output (YOU WIN)
   end if;
     v_answer := p_choice

Upvotes: 0

Views: 1899

Answers (1)

Radim Bača
Radim Bača

Reputation: 10701

CREATE or REPLACE Procedure WinOrLose (
  p_choice number ) AS
BEGIN
  dbms_output.put_line ('Welcome to the Win or Lose Game. Your number is ' || 
  p_choice);

  FOR v_counter in 1..p_choice LOOP
    IF (MOD(v_counter, 2) = 0) 
    THEN
          dbms_output.put_line ('You win');
    ELSE
          dbms_output.put_line ('You lose');
    END IF;
  END LOOP;

  IF (MOD(p_choice , 2) = 0) 
  THEN
    dbms_output.put_line ('==> You win!');
  ELSE
    dbms_output.put_line ('==> You lose!');
  END IF;
END;
/

Upvotes: 2

Related Questions