Control structures
Control structures are used to control the flow of programs execution. The control structures are classified into the following categories:
- Conditional Control
- Iterative Control
Conditional Control
PL/SQL supports the following types of Conditional control statements or Selection Statements
- Simple if statement
- If...else statement
- If ...elsif statement
1.Simple If
The simple if statement is used to check a simple condition, if it is true it will execute the statements that are present between "if "and "end if". Otherwise it will not execute those statements.
Syntax :
If ( Condition ) then
Statements ;
------------- ;
End If;
2.If...Else Statement
The if …else statement is used to define two blocks of statements, in order to execute only one block.
Syntax :
If (Condition ) then
True Block Statements ;
------------------------- ;
Else
False Block Statements ;
-------------------------- ;
End if;
if the condition is true, it will execute the True Block statements otherwise it will execute the false block statements.
3.If...ElsIf Statement
The if …elsIf statement is used to define Several blocks of statements, in order to execute only one block.
Syntax :
If (condition 1) then
statements 1 ;
--------------- ;
Elsif (condition 2) then
statements 2;
------------- ;
Elsif (condition 3) then
statements 3;
------------;
|
|
Elsif (condition n) then
statements n;
------------;
Else
statements;
------------;
End if;
if condition1 is true, it will execute statements1 otherwise it will check Condition2, if it is true it will execute statements2. if not it will go to condition3, like this the process continues until the condition matches. If no condition matches, it will execute the else block statements.
Eg 1. Write a program to check whether the given number is a positive number of a negative number using simple if statement
declare
n number:=&n;
begin
if(n>=0) then
dbms_output.put_line(n||' is a Positive Number');
end if;
if(n<0) then
dbms_output.put_line(n ||' is a Negative Number');
end if;
end;
/
Branching Statements
declare
x integer:=&x;
y integer:=&y;
p char(1):='&Operator';
r number;
begin
if p='+' then
goto Addition;
elsif p='-' then
goto Subtraction;
elsif p='*' then
goto Multiplication;
elsif p='/' then
goto Division;
else
goto Expt;
end if;
<<Addition>>
r:=x+y;
dbms_output.put_line(r);
return;
<<Subtraction>>
r:=x-y;
dbms_output.put_line(r);
return;
<<Multiplication>>
r:=x*y;
dbms_output.put_line(r);
return;
<<Division>>
r:=x/y;
dbms_output.put_line(r);
return;
<<Expt>>
dbms_output.put_line(sqlerrm);
end;
/
Loops
PL/SQL supports, the following types of loops
1. Simple Loop
2. While Loop
3. For Loop
1.Simple Loop
This loop is used to execute a series of statements as long as the condition is false. If the condition becomes true, it will exit from that loop
Syntax :
Loop
Statements;
-----------
Exit When <condition>;
Increment/decrement Operations
End Loop;
Eg 2 :Write a program to print first 25 natural numbers using simple loop
Declare
N number:=1;
Begin
Loop
Exit When N>25;
Dbms_output.put_line(N);
N:=N+1;
End loop;
End;
/
2.While Loop
The while Loop is used to execute a series of statements as long as the condition is True. If the condition becomes false, it will exit from that loop
Syntax :
While <condition> Loop
Statements;
-----------
[ Exit When <condition>; ]
Increment/decrement Operations
End Loop;
Eg 3: Write a program to print first 25 natural numbers using While loop
Declare
N number:=1
Begin
While N<=25 Loop
Dbms_output.put_line(N);
N:=N+1;
End loop;
End;
/
3.For Loop
The For loop is used to execute a set of statements as long as the condition is false. If the condition becomes true, it will exit from that loop
Syntax :
For <variable> in [reverse] <start value> .. <end Value> Loop
Statements;
-----------
End Loop;
Eg 4: Write a program to print first 25 natural numbers using For loop
Declare
N number:=1;
Begin
For N in 1..25 Loop
Dbms_output.put_line(N);
End loop;
End;
/
Eg 5 :Write a program to print first 25 natural numbers using For loop in Reverse order
Declare
N number:=1;
Begin
For N in Reverse 1..25 Loop
Dbms_output.put_line(N);
End loop;
End;
/
Eg 6: Write a program to print multiplication table for the given number
Declare
i number:=1;
r number:=&number;
Begin
While i<=10 loop
dbms_output.put_line(r||'*'|| i ||'='||r*i);
i:=i+1;
End loop;
End;
/
No comments:
Post a Comment