Prerequisite – PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements.
In declare part, we declare variables and between begin and end part, we perform the operations.
- PL/SQL - Functions - In this chapter, we will discuss the functions in PL/SQL. The following program calculates the factorial of a given number by calling itself.
- Enter an integer: 10 Factorial of 10 = 3628800 This program takes a positive integer from the user and computes the factorial using for loop. Since the factorial of a number may be very large, the type of factorial variable is declared as unsigned long long. If the user enters a negative number, the program displays a custom error message.
Here, first, we take three variables num, fact, and temp and assign the value in num variable (i.e which number factorial we want).
And then after we assign fact variable to 1.
Examples:
Below is the required implementation:
What is PL/SQL Subprograms? Subprograms are named PL/SQL blocks that can be called with a set of parameters. PL/SQL has two types of subprograms, procedures, and functions. Subprograms have: A declarative part, with declarations of types, cursors, constants, variables, exceptions, and nested subprograms.
-- and temp of datatype number fact number := 1; -- with the help of while loop loop temp := temp -1; end loop; dbms_output.put_line( 'factorial of ' || num || ' is ' || fact); end ; -- Program End |
Output :
Summary: in this tutorial, you will learn how PL/SQL WHILE loop Beijer e designer software download. statementto execute a sequence of statements repeatedly based on a condition that is checked at the beginning of each iteration.
Factorial Program In Pl Sql Query
Introduction to PL/SQL WHILE Loop
Sometimes, you don't know in advance how many times a sequence of statements needs to execute because it depends on a condition which is not fixed at compile time. In such cases, you should use PL/SQL WHILE LOOP
statement.
The following illustrates the PL/SQL WHILE LOOP
syntax:
A condition
is a Boolean variable or expression that evaluates to a Boolean value of TRUE
, FALSE
or NULL
. The condition is checked at the beginning of each iteration.
- If the
condition
evaluates toTRUE
, thesequence_of_statements
is executed. - If the
condition
evaluates toFALSE
orNULL
, the loop terminates and control is passed to the next executable statement following theEND LOOP
keywords.
It is important to note that the sequence of statements may not execute at all if the condition
evaluates to FALSE
or NULL
before entering the loop.
In addition, inside the loop you have to update some variables to make the condition
becomes to FALSE
or NULL
at some points to terminate the loop, otherwise, you will have an endless loop, which causes a stack overflow error.
Factorial Program In Pl Sql Server
The PL/SQL WHILE
loop is effective when you don't know how many times the loop will execute. If the number of iterations is predetermined, you should use the PL/SQL FOR loop statement instead.
The following flowchart illustrates the PL/SQL WHILE
loop statement:
PL/SQL WHILE LOOP example
This example calculates factorial of 10 by using PL/SQL WHILE LOOP
statement:
How it works.
- First, we initialize the counter to 10 and factorial to 1.
- Second, in each iteration, we multiply the factorial with the counter and decrease the counter by 1. The loop is terminated when the counter reaches zero.
- Third, we display the result of the factorial of 10.
Factorial Program In Pl Sql Compiler
Below is the required implementation:
What is PL/SQL Subprograms? Subprograms are named PL/SQL blocks that can be called with a set of parameters. PL/SQL has two types of subprograms, procedures, and functions. Subprograms have: A declarative part, with declarations of types, cursors, constants, variables, exceptions, and nested subprograms.
-- and temp of datatype number fact number := 1; -- with the help of while loop loop temp := temp -1; end loop; dbms_output.put_line( 'factorial of ' || num || ' is ' || fact); end ; -- Program End |
Output :
Summary: in this tutorial, you will learn how PL/SQL WHILE loop Beijer e designer software download. statementto execute a sequence of statements repeatedly based on a condition that is checked at the beginning of each iteration.
Factorial Program In Pl Sql Query
Introduction to PL/SQL WHILE Loop
Sometimes, you don't know in advance how many times a sequence of statements needs to execute because it depends on a condition which is not fixed at compile time. In such cases, you should use PL/SQL WHILE LOOP
statement.
The following illustrates the PL/SQL WHILE LOOP
syntax:
A condition
is a Boolean variable or expression that evaluates to a Boolean value of TRUE
, FALSE
or NULL
. The condition is checked at the beginning of each iteration.
- If the
condition
evaluates toTRUE
, thesequence_of_statements
is executed. - If the
condition
evaluates toFALSE
orNULL
, the loop terminates and control is passed to the next executable statement following theEND LOOP
keywords.
It is important to note that the sequence of statements may not execute at all if the condition
evaluates to FALSE
or NULL
before entering the loop.
In addition, inside the loop you have to update some variables to make the condition
becomes to FALSE
or NULL
at some points to terminate the loop, otherwise, you will have an endless loop, which causes a stack overflow error.
Factorial Program In Pl Sql Server
The PL/SQL WHILE
loop is effective when you don't know how many times the loop will execute. If the number of iterations is predetermined, you should use the PL/SQL FOR loop statement instead.
The following flowchart illustrates the PL/SQL WHILE
loop statement:
PL/SQL WHILE LOOP example
This example calculates factorial of 10 by using PL/SQL WHILE LOOP
statement:
How it works.
- First, we initialize the counter to 10 and factorial to 1.
- Second, in each iteration, we multiply the factorial with the counter and decrease the counter by 1. The loop is terminated when the counter reaches zero.
- Third, we display the result of the factorial of 10.
Factorial Program In Pl Sql Compiler
In this tutorial, you've learned how to use PL/SQL WHILE LOOP
statement to execute a sequence of statements repeatedly based on a condition that is checked before each iteration of the loop.