Oracle:for Loop Statement Practice

Source: Internet
Author: User

--Printing output positive integers from 1 to 10
DECLARE
V_i Number (10): = 0;
BEGIN
LOOP
V_i: = v_i + 1;
Dbms_output.put_line (v_i);
EXIT when v_i=10; --or v_i>9;
END LOOP;
END;

--an even number of printouts from 1 to 10
DECLARE
V_i Number (10): = 0;
BEGIN
LOOP
V_i: = v_i + 1;
IF v_i MOD 2 = 0 Then
Dbms_output.put_line (v_i);
END IF;
EXIT when v_i=10;
END LOOP;
END;

--The sum of the positive integers from 1 to 100 of the printout.
--The first method
DECLARE
V_i Number (10): = 0;
V_sum Number (10): = 0;
BEGIN
LOOP
V_i: = v_i + 1;
V_sum: = V_sum + v_i;
EXIT when v_i=100;
END LOOP;
Dbms_output.put_line (v_sum);
END;
--The second method
DECLARE
V_sum Number (10): = 0;
BEGIN
For v_i in 1..100 LOOP
V_sum: = V_sum + v_i;
END LOOP;
Dbms_output.put_line (v_sum);
END;

--Printing output positive integers from 1 to 10
--The first method
DECLARE
V_i Number (10): = 0;
BEGIN
For v_i in 1..10 LOOP
Dbms_output.put_line (v_i);
END LOOP;
END;
--The second method
DECLARE
V_i Number (10): = 0;
BEGIN
For v_i in REVERSE 1..10 LOOP--Reverse
Dbms_output.put_line (v_i);
END LOOP;
END;

Oracle:for Loop Statement Practice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.