课件教学讲稿les_第1页
课件教学讲稿les_第2页
课件教学讲稿les_第3页
课件教学讲稿les_第4页
课件教学讲稿les_第5页
已阅读5页,还剩31页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Writing Control StructuresObjectivesAfter completing this lesson, you should be able to do the following:Identify the uses and types of control structuresConstruct an IF statementUse CASE statements and CASE expressionsConstruct and identify loop statementsUse guidelines when using conditional contr

2、ol structuresControlling Flow of ExecutionforloopwhileIF StatementSyntax:IF condition THEN statements;ELSIF condition THEN statements;ELSE statements;END IF;Simple IF StatementDECLARE v_myage number:=31;BEGIN IF v_myage 11 THEN DBMS_OUTPUT.PUT_LINE( I am a child ); END IF;END;/Output:IF THEN ELSE St

3、atementDECLAREv_myage number:=31;BEGINIF v_myage 11 THEN DBMS_OUTPUT.PUT_LINE( I am a child ); ELSE DBMS_OUTPUT.PUT_LINE( I am not a child );END IF;END;/Output:IF ELSIF ELSE ClauseDECLARE v_myage number:=31;BEGIN IF v_myage 11 THEN DBMS_OUTPUT.PUT_LINE( I am a child ); ELSIF v_myage 20 THEN DBMS_OUT

4、PUT.PUT_LINE( I am young ); ELSIF v_myage 30 THEN DBMS_OUTPUT.PUT_LINE( I am in my twenties); ELSIF v_myage 40 THEN DBMS_OUTPUT.PUT_LINE( I am in my thirties); ELSE DBMS_OUTPUT.PUT_LINE( I am always young ); END IF;END;/Output:NULL Value in IF StatementDECLARE v_myage number;BEGIN IF v_myage 3; END

5、LOOP;END;/Basic LoopsExample:WHILE LoopsSyntax:Use the WHILE loop to repeat statements while a condition is TRUE.WHILE condition LOOP statement1; statement2; . . .END LOOP;WHILE LoopsExampleDECLARE v_countryid locations.country_id%TYPE := CA; v_loc_id locations.location_id%TYPE; v_new_city locations

6、.city%TYPE := Montreal; v_counter NUMBER := 1;BEGIN SELECT MAX(location_id) INTO v_loc_id FROM locations WHERE country_id = v_countryid; WHILE v_counter = 3 LOOP INSERT INTO locations(location_id, city, country_id) VALUES(v_loc_id + v_counter), v_new_city, v_countryid); v_counter := v_counter + 1; E

7、ND LOOP;END;/FOR LoopsUse a FOR loop to shortcut the test for the number of iterations.Do not declare the counter; it is declared implicitly.FOR counter IN REVERSE lower_bound.upper_bound LOOP statement1; statement2; . . .END LOOP;FOR LoopsExample:DECLARE v_countryid locations.country_id%TYPE := CA;

8、 v_loc_id locations.location_id%TYPE; v_new_city locations.city%TYPE := Montreal;BEGIN SELECT MAX(location_id) INTO v_loc_id FROM locations WHERE country_id = v_countryid; FOR i IN 1.3 LOOP INSERT INTO locations(location_id, city, country_id) VALUES(v_loc_id + i), v_new_city, v_countryid ); END LOOP

9、;END;/FOR LoopsGuidelinesReference the counter within the loop only; it is undefined outside the loop.Do not reference the counter as the target of an assignment.Neither loop bound should be NULL.Guidelines for LoopsUse the basic loop when the statements inside the loop must execute at least once.Us

10、e the WHILE loop if the condition must be evaluated at the start of each iteration.Use a FOR loop if the number of iterations is known.Nested Loops and LabelsYou can nest loops to multiple levels.Use labels to distinguish between blocks and loops.Exit the outer loop with the EXIT statement that refe

11、rences the label.Nested Loops and Labels.BEGIN LOOP v_counter := v_counter+1; EXIT WHEN v_counter10; LOOP . EXIT Outer_loop WHEN total_done = YES; - Leave both loops EXIT WHEN inner_done = YES; - Leave inner loop only . END LOOP Inner_loop; . END LOOP Outer_loop;END; / PL/SQL CONTINUE StatementDefin

12、itionAdds the functionality to begin the next loop iterationProvides programmers with the ability to transfer control to the next iteration of a loopUses parallel structure and semantics to the EXIT statementBenefitsEases the programming processMay see a small performance improvement over the previo

13、us programming workarounds to simulate the CONTINUE statementPL/SQL CONTINUE Statement: ExampleDECLARE v_total SIMPLE_INTEGER := 0;BEGIN FOR i IN 1.10 LOOP v_total := v_total + i; dbms_output.put_line (Total is: | v_total); CONTINUE WHEN i 5; v_total := v_total + i; dbms_output.put_line (Out of Loop

14、 Total is: | v_total); END LOOP;END;/12anonymous block completedTotal is: 1Out of Loop Total is: 2Total is: 4Out of Loop Total is: 6Total is: 9Out of Loop Total is: 12Total is: 16Out of Loop Total is: 20Total is: 25Out of Loop Total is: 30Total is: 36Total is: 43Total is: 51Total is: 60Total is: 70P

15、L/SQL CONTINUE Statement: ExampleDECLARE v_total NUMBER := 0;BEGIN FOR i IN 1.10 LOOP v_total := v_total + 1; dbms_output.put_line (Total is: | v_total); FOR j IN 1.10 LOOP CONTINUE BeforeTopLoop WHEN i + j 5; v_total := v_total + 1; END LOOP; END LOOP;END two_loop;anonymous block completedTotal is: 1Total is: 6Total is: 10Total is: 13Total is: 15Total is: 16Total is: 17Total is: 18Total is: 19Total is: 20SummaryIn this lesson, you should have learned how to change the logical flow of statements by using the following control structures:Conditional (IF statement)CASE expressions

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论