Results 1 to 6 of 6
  1. #1
    Join Date
    May 2011
    Posts
    36

    Unanswered: IF condition inside the for FOR LOOP

    Please clear my doubt on the below function

    1. Where the control will go if the condition got failed after the For loop

    2. What is the purpose of the Exit here


    FUNCTION new_rental(
    memberid IN rental.member_id%TYPE,
    titleid IN rental.title_id%TYPE) RETURN DATE IS
    CURSOR copy_csr IS
    SELECT * FROM title_copy WHERE title_id = titleid FOR UPDATE;
    flag BOOLEAN := FALSE;
    BEGIN
    FOR copy_rec IN copy_csr LOOP
    IF copy_rec.status = 'AVAILABLE' THEN
    UPDATE title_copy
    SET status = 'RENTED' WHERE CURRENT OF copy_csr;
    INSERT INTO rental(book_date, copy_id, member_id,
    title_id, exp_ret_date)
    VALUES (SYSDATE, copy_rec.copy_id, memberid,
    titleid, SYSDATE + 3);
    flag := TRUE;
    EXIT;
    END IF;
    END LOOP;
    COMMIT;
    IF flag THEN
    RETURN (SYSDATE + 3);
    ELSE
    reserve_movie(memberid, titleid);
    RETURN NULL;
    END IF;
    EXCEPTION
    WHEN OTHERS THEN
    exception_handler(SQLCODE, 'NEW_RENTAL');
    END new_rental;





    Thanks,
    Ganesh
    Last edited by GaneshAnanth; 06-03-13 at 10:34.

  2. #2
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    The EXIT command will exit the loop that it is in.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  3. #3
    Join Date
    May 2011
    Posts
    36
    ok I understood.

    Then where the control will go if the condition got failed after the For loop

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713
    Quote Originally Posted by GaneshAnanth View Post
    ok I understood.

    Then where the control will go if the condition got failed after the For loop
    If the "IF" condition fails, perhaps it will go to evaluate the "END LOOP;"?
    Where do you think it would go?
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    May 2011
    Posts
    36
    Please correct me If I am wrong.

    If the first condition got failed then it again continue to loop and checks the next value.

    If the first condition itself passed then it go to the next statement - not again go to the FOR LOOP and check for the next value- Right?



    Thanks,
    Ganesh

  6. #6
    Join Date
    Feb 2006
    Posts
    173
    Provided Answers: 1
    Quote Originally Posted by GaneshAnanth View Post
    If the first condition got failed then it again continue to loop and checks the next value.
    If you mean failed by 'some type of error/exception' then it would goto your 'exception' block, if you mean failed by 'copy_rec.status not equal to 'AVAILABLE' then it would goto the statement right after the 'end if' which would take it back to the top of loop if more rows exists.
    Quote Originally Posted by GaneshAnanth View Post
    If the first condition itself passed then it go to the next statement - not again go to the FOR LOOP and check for the next value- Right
    If you that mean that it makes it to the 'EXIT' statement then yes it would exit the most inner loop and goto the 'COMMIT' statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •