1<br />Using ORACLE®<br />LOOPS and CONTROL structures <br />
2<br />CONTROL STRUCTURES<br />Control structures are those constructs that alter the flow of program execution.<br />
3<br />LOOPS<br />Control structures are those constructs that alter the flow of program execution.<br />Loop is one such ...
4<br />SIMPLE LOOP<br />A simple loop begins with a LOOP keyword, ends with a END LOOP keyword and in between are the stat...
5<br />WHILE LOOP<br />The WHILE loop is an entry control loop meaning that the condition is checked while entering the lo...
6<br />LOOPS<br />The FOR loop is an entry control loop with a LOWER_BOUND.. UPPER_BOUND specified in the condition in the...
7<br />NESTED LOOPS<br />We can nest one loop inside another by giving labels to the loops.<br />SYNTAX<br /><<outer_loop>...
8<br />SELECTIVE STATEMENTS<br />Selective statements are the constructs that perform the action based on the condition th...
9<br />IF<br />The IF statement has a condition ,which when satisfied the statements in its body are executed else the are...
10<br />IF….ELSE<br />The IF statement provides only the action to be taken if condition is satisfied .<br />But the IF…EL...
11<br />IF…ELSIF…ELSE<br />The IF statement provides only the action to be taken if condition is satisfied .<br />But the ...
12<br />CASE<br />The CASE construct is a special form of the ELSIF construct which matches a selector values with list of...
THANK YOU<br />13<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMI...
Upcoming SlideShare
Loading in …5
×

Oracle: Control Structures

1,935
-1

Published on

Oracle: Control Structures

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,935
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Oracle: Control Structures

  1. 1. 1<br />Using ORACLE®<br />LOOPS and CONTROL structures <br />
  2. 2. 2<br />CONTROL STRUCTURES<br />Control structures are those constructs that alter the flow of program execution.<br />
  3. 3. 3<br />LOOPS<br />Control structures are those constructs that alter the flow of program execution.<br />Loop is one such control structure. A loop iterates a given set of statements until a condition is satisfied.<br />There are three types of looping constructs:<br />Simple loop.<br />While loop<br />For loop.<br />FOR counter IN<br />Lwr_bnd..Upr_bnd<br />LOOP<br />Statement 1;<br />……<br />……<br />END LOOP<br />loop<br />Statement 1;<br />…….<br />…….<br />EXIT WHEN[condition]<br />END LOOP<br />WHILE( condition) LOOP<br />Statement 1<br />……..<br />…….<br />END LOOP<br />
  4. 4. 4<br />SIMPLE LOOP<br />A simple loop begins with a LOOP keyword, ends with a END LOOP keyword and in between are the statements to be performed. We must include an EXIT WHEN statement to set the exit condition or else it will form an infinite loop.<br />EXAMPLE:<br />DECLARE ename VARCHAR2(20); counter NUMBER :=40;<br />BEGIN<br />LOOP<br /> SELECT age INTO eage FROM InfoTable WHERE <br /> age = counter;<br /> DBMS_OUTPUT.PUT_LINE(‘Age is’ ||eage); SYNTAX<br /> counter += 5;<br /> EXIT WHEN counter >50;<br />END LOOP<br />END<br />loop<br />Statement 1;<br />…….<br />…….<br />EXIT WHEN[condition]<br />END LOOP<br />
  5. 5. 5<br />WHILE LOOP<br />The WHILE loop is an entry control loop meaning that the condition is checked while entering the loop unlike the Simple loop where condition is checked while exit.<br />EXAMPLE:<br />DECLARE ename VARCHAR2(20); counter NUMBER :=40;<br />BEGIN<br />WHILE counter <55 <br />LOOP<br /> SELECT age INTO eage FROM InfoTable WHERE <br /> age = counter;<br /> DBMS_OUTPUT.PUT_LINE(‘Age is’ ||eage); SYNTAX<br />counter += 5; <br />END LOOP<br />END<br />WHILE( condition) LOOP<br />Statement 1<br />……..<br />…….<br />END LOOP<br />
  6. 6. 6<br />LOOPS<br />The FOR loop is an entry control loop with a LOWER_BOUND.. UPPER_BOUND specified in the condition in the same format. The counter variable will be implicitly declared.<br />EXAMPLE:<br />DECLARE<br />ename VARCHAR2(20);<br />eage NUMBER := 40;<br />BEGIN<br />FOR i IN 40..55<br />LOOP<br />select name INTO ename FROM Infotable WHERE age = eage; SYNTAX<br />eage := eage+5;<br />DBMS_OUTPUT.PUT_LINE('Name is'||ename);<br />END LOOP;<br />END<br />FOR counter IN<br />Lwr_bnd..Upr_bnd<br />LOOP<br />Statement 1;<br />……<br />……<br />END LOOP<br />
  7. 7. 7<br />NESTED LOOPS<br />We can nest one loop inside another by giving labels to the loops.<br />SYNTAX<br /><<outer_loop>>label for outer loop<br />LOOP<br /> <<inner_loop>> label for inner loop<br />LOOP Statement 1;<br /> …..<br /> EXIT WHEN [cond]<br /> END LOOP inner _loop;<br />Statement 1;<br />……<br />……<br />EXIT WHEN [cond]<br />END LOOP outer_loop;<br /><<outer_loop>><br />FOR counter IN<br />Lwr_bnd..Upr_bnd<br />LOOP<br /> <<inner_loop><br /> LOOP<br /> Statement 1;<br /> …..<br /> EXIT WHEN [cond]<br />END LOOP Inner _loop;<br />Statement 1;<br />……<br />……<br />END LOOP Outer_loop;<br />
  8. 8. 8<br />SELECTIVE STATEMENTS<br />Selective statements are the constructs that perform the action based on the condition that is satisfied.There are four selective constructs:<br />IF<br />IF…..ELSE<br />IF……ELSIF……ELSE<br />CASE<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />ELSIF<br /> Statement1;<br /> Statement2;<br />ELSE<br /> Statement1;<br /> Statement2;<br />END IF;<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />……<br />END IF;<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />ELSE<br />Statement1;<br />Statement2;<br />END IF;<br />CASE selector<br />WHEN expression 1 THEN result 1;<br />WHEN expression 2 THEN result 2;<br />…….<br />ELSE [result n]<br />END<br />
  9. 9. 9<br />IF<br />The IF statement has a condition ,which when satisfied the statements in its body are executed else the are not executed.<br />EXAMPLE:<br />VARIABLE ename VARCHAR2(20);<br />BEGIN<br />ename :=&ename;<br />IF ename=‘Bill‘ THEN<br />DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');<br />END IF;<br />END<br />The above code outputs “Hi BILL!Welcome” if the user enters the name as Bill else no output is shown.<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />……<br />END IF;<br />
  10. 10. 10<br />IF….ELSE<br />The IF statement provides only the action to be taken if condition is satisfied .<br />But the IF…ELSE construct provides both the actions to be taken when the condition is satisfied given in the IF block and the action when it is not which is given in the ELSE block.<br />EXAMPLE:<br />VARIABLE ename VARCHAR2(20);<br />BEGIN<br />ename :=&ename;<br />IF ename='bill‘ THEN<br />DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');<br />ELSE<br />DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill ');<br />END IF;<br />END<br /> Here the output is 'Hi BILL! Welcome’ when user enters Bill and displays the message “Sorry! Access only to bill “ if the user enters any other name.<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />ELSE<br />Statement1;<br />Statement2;<br />END IF;<br />
  11. 11. 11<br />IF…ELSIF…ELSE<br />The IF statement provides only the action to be taken if condition is satisfied .<br />But the IF…ELSEIF…ELSE construct provides both the actions to be taken when one the condition ,another action if another condition is satisfies and finally the action is both conditions fail.<br />EXAMPLE:<br />VARIABLE ename VARCHAR2(20);<br />BEGIN<br />ename :=&ename;<br />IF ename=‘Bill‘ THEN<br />DBMS_OUTPUT.PUT_LINE('Hi BILL!Welcome');<br />ELSIF ename= ‘Steve’ THEN<br />DBMS_OUTPUT.PUT_LINE(‘Hi Steve! Come on in');<br />ELSE<br />DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill or steve ');<br />END IF;<br />END<br />Here the output is 'Hi BILL! Welcome’ when user enters Bill, output is 'Hi Steve! Come on in’ when user enters Steve and displays the message "Sorry! Access only to bill “ if the user enters any other name.<br />IF (condition..)<br />THEN<br />Statement1;<br />Statement2;<br />ELSIF<br /> Statement1;<br /> Statement2;<br />ELSE<br /> Statement1;<br /> Statement2;<br />END IF;<br />
  12. 12. 12<br />CASE<br />The CASE construct is a special form of the ELSIF construct which matches a selector values with list of expresions and if either is matched the executes those statements.<br />EXAMPLE:<br />VARIABLE ename VARCHAR2(20);<br />BEGIN<br />ename :=&ename;<br />CASE ename<br />WHEN ‘Bill’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Bill! Come on in');<br />WHEN ‘Steve’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Steve! Come on in');<br />WHEN ‘Larry’ THEN DBMS_OUTPUT.PUT_LINE(‘Hi Larry! Come on in’);<br />ELSE DBMS_OUTPUT.PUT_LINE(‘ Sorry!Access only to bill or steve or larry ');<br />END IF;<br />END<br />Here the output is 'Hi BILL! Come on in’ when user enters Bill, output is 'Hi Steve! Come on in’ when user enters Steve and output is 'Hi Larry! Come on in’ when user enters Larry and displays the message "Sorry! Access only to bill “ if the user enters any other name.<br />CASE selector<br />WHEN expression 1 THEN result 1;<br />WHEN expression 2 THEN result 2;<br />…….<br />ELSE [result n]<br />END<br />
  13. 13. THANK YOU<br />13<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,<br />please visit: <br />www.dataminingtools.net<br />

×