PL/SQL script language loop for while usage learning example

Source: Internet
Author: User

PL/SQL script language loop for while usage learning example (1), loop learning (a), loop www.2cto.com declare -- sum variable I variable 1 .. 100 v_sum number (5); v_ I number (5); begin v_sum: = 0; v_ I: = 1; loop v_sum: = v_sum + v_ I; v_ I: = v_ I + 1; exit when v_ I> 100; -- exit condition end loop; dbms_output.put_line (v_sum); (B), for loop use www.2cto.com IN THE for loop, do not need to loop variable (index) declared, oracle treats it as binary_integer by default to use -- for loop for variable in set loop body; end loop; -- variables do not need to declare the type variables that automatically match the elements in the set. They can also control the number of cycles and cannot be directly assigned values. -- 1 .. 100 fixed syntax of the numeric set www.2cto.com example (1) for v_ii in 1 .. 10 loop v_sum: = v_sum + v_ii; v_ii: = v_ii + 1; end loop; -- Example (2) for v_ii in 1 .. 5 loop v_sum: = v_sum + v_ I; v_ I: = v_ I + 2; end loop; dbms_output.put_line (v_sum); (c) Example of while loop usage (3) -- while executes the conditional loop body; end loop; while v_ I <= 100 loop v_sum: = v_sum + v_ I; v_ I: = v_ I + 1; end loop; dbms_output.put_line (v_sum ); end; www.2cto.com (2), three methods output multiplication table example (1) Use loop to implement declare v_ I number (10); v_j number (10); begin v_ I: = 1; loop v_j: = 1; loop dbms_output.put (v_ I | '*' | v_j | '=' | v_ I * v_j | '); v_j: = v_j + 1; exit when v_j> v_ I; end loop; dbms_output.put_line (''); v_ I: = v_ I + 1; exit when v_ I> 9; end loop; end; example (2) use for to implement declare begin v_d: = 1; for v_ I in 1 .. 9 loop for v_j in 1 .. v_ I loop dbms_output.put (v_ I | '*' | v_j | '=' | v_ I * v_j | ''); end loop; dbms_output.put_line (''); end loop; end; www.2cto.com example (3) use while to implement declare v_ I number (10); v_j number (10); begin v_ I: = 1; while v_ I <10 loop v_j: = 1; while v_j <= v_ I loop dbms_output.put (v_ I | '*' | v_j | '=' | v_ I * v_j | ''); v_j: = v_j + 1; end loop; dbms_output.put_line (''); v_ I: = v_ I + 1; end loop; end;

Related Article

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.