Fundamentals of PL/SQL part 2 (Basics)

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Statement-Level Control Structures Sections 1-4
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Chapter 4 Program Control Statements
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CPS120 Introduction to Computer Science Iteration (Looping)
PL/SQL Loops. Building Logical Conditions All logical conditions must yield a boolean condition. You can build a simple Boolean condition by combining.
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Execution Dong Shao, Nanjing Unviersity.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Bordoloi and Bock Control Structures: Iterative Control.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Core Java Statements in Java.
Chapter 9 - JavaScript: Control Statements II
Chapter 4 – C Program Control
Chapter 6: Loops.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Def: A control structure is a control statement and
Writing Control Structures
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Lecture 7: Repeating a Known Number of Times
Chapter 4 - Program Control
Branching Constructs Review
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
JavaScript: Control Statements.
Chapter 8: Control Structures
Looping.
Conditinoal Constructs Review
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Conditinoal Constructs Review
Writing Control Structures
Chapter 4 - Program Control
Loops in C.
3 Control Statements:.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Statement-Level Control Structures
2.6 The if/else Selection Structure
Loop Construct.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
Chapter 4 - Program Control
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Conditional Loops Counted Loops
CSC215 Lecture Control Flow.
REPETITION Why Repetition?
Presentation transcript:

Fundamentals of PL/SQL part 2 (Basics)

Loops Loop Type Description PL/SQL Basic LOOP In this loop structure, sequence of statements is enclosed between the LOOP and END LOOP statements. At each iteration, the sequence of statements is executed and then control resumes at the top of the loop. PL/SQL WHILE LOOP Repeats a statement or group of statements until a given condition is true. It tests the condition before executing the loop body. PL/SQL FOR LOOP Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. Nested loops in PL/SQL You can use one or more loop inside any another basic loop, while or for loop.

Simple/Basic Loops Basic loop structure encloses sequence of statements in between the LOOP and END LOOP statements. With each iteration, the sequence of statements is executed and then control resumes at the top of the loop. Syntax: The syntax of a basic loop in PL/SQL programming language is: LOOP Sequence of statements; END LOOP;

While Loop A WHILE LOOP statement in PL/SQL programming language repeatedly executes a target statement as long as a given condition is true. Syntax: WHILE condition LOOP sequence_of_statements END LOOP;

For Loop Reverse FOR LOOP Statement A FOR LOOP is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax: FOR counter IN initial_value .. final_value LOOP sequence_of_statements; END LOOP; Reverse FOR LOOP Statement By default, iteration proceeds from the initial value to the final value, generally upward from the lower bound to the higher bound. You can reverse this order by using the REVERSE keyword. In such case, iteration proceeds the other way. After each iteration, the loop counter is decremented.

Nested Loops PL/SQL allows using one loop inside another loop The syntax for a nested basic LOOP statement in PL/SQL is as follows: LOOP Sequence of statements1 Sequence of statements2 END LOOP; The syntax for a nested FOR LOOP statement in PL/SQL is as follows: FOR counter1 IN initial_value1 .. final_value1 LOOP sequence_of_statements1 FOR counter2 IN initial_value2 .. final_value2 LOOP sequence_of_statements2

Nested Loops PL/SQL allows using one loop inside another loop The syntax for a nested WHILE LOOP statement in PL/SQL is as follows: WHILE condition1 LOOP sequence_of_statements1 WHILE condition2 LOOP sequence_of_statements2 END LOOP;

Labelling in Loops PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and >>)and appear at the beginning of the LOOP statement. The label name can also appear at the end of the LOOP statement. You may use the label in the EXIT statement to exit from the loop.

Control Statements Control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. PL/SQL supports the following control statements. Labeling loops also helps in taking the control outside a loop. Control Statement Description EXIT statement The Exit statement completes the loop and control passes to the statement immediately after END LOOP CONTINUE statement Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. GOTO statement Transfers control to the labeled statement. Though it is not advised to use GOTO statement in your program.

Exit Statement The EXIT statement in PL/SQL programming language has following two usages: When the EXIT statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. If you are using nested loops (i.e. one loop inside another loop), the EXIT statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax: The syntax for a EXIT statement in PL/SQL is as follows: EXIT;

Continue Statement The CONTINUE statement causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. In other words, it forces the next iteration of the loop to take place, skipping any code in between. Syntax: The syntax for a CONTINUE statement is as follows: CONTINUE;

GOTO Statement A GOTO statement in PL/SQL programming language provides an unconditional jump from the GOTO to a labeled statement in the same subprogram. Syntax: The syntax for a GOTO statement in PL/SQL is as follows: GOTO label; .. << label >> statement; NOTE: Use of GOTO statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a GOTO can be rewritten so that it doesn't need the GOTO.

Restrictions With GOTO Statements GOTO Statement in PL/SQL impose the following restrictions: A GOTO statement cannot branch into an IF statement, CASE statement, LOOP statement, or sub-block. A GOTO statement cannot branch from one IF statement clause to another, or from one CASE statement WHEN clause to another. A GOTO statement cannot branch from an outer block into a sub-block (that is, an inner BEGIN-END block). A GOTO statement cannot branch out of a subprogram. To end a subprogram early, either use the RETURN statement or have GOTO branch to a place right before the end of the subprogram. A GOTO statement cannot branch from an exception handler back into the current BEGIN-END block. However, a GOTO statement can branch from an exception handler into an enclosing block.

Examples Write a program in PL/SQL to find factorial between 2 to 50 Write a program to calculate Factorial of a number (Take input from user using & Substitution variable) Find Greatest Number Using Case Statement. (Take input from user using substitution variable)

Solutions Write a program in PL/SQL to find prime numbers till 100 DECLARE i number(3); j number(3); BEGIN i := 2; LOOP j:= 2; exit WHEN ((mod(i, j) = 0) or (j = i)); j := j +1; END LOOP; IF (j = i ) THEN dbms_output.put_line(i || ' is prime'); END IF; i := i + 1; exit WHEN i = 100; END;

Solutions Write a program to calculate Factorial of a number (Take input from user using & Substitution variable) DECLARE num NUMBER:='&Input_Number'; i NUMBER; f NUMBER:=1; BEGIN FOR i IN 1..num LOOP f := f * i; END LOOP; DBMS_OUTPUT.PUT_LINE(f||' Is Factorial Of '||num); END;

Solutions Find Greatest Number Using Case Statement. (Take input from user using substitution variable) DECLARE num1 NUMBER; num2 NUMBER; BEGIN num1:='&INPUT_FIRST_NUMBER'; num2:='&INPUT_SECOND_NUMBER'; CASE WHEN(num1>num2)THEN DBMS_OUTPUT.PUT_LINE('FIRST NUMBER IS GREATER : '||num1); WHEN(num1<num2)THEN DBMS_OUTPUT.PUT_LINE('SECOND NUMBER IS GREATER : '||num2); ELSE DBMS_OUTPUT.PUT_LINE('BOTH NUMBERS ARE SAME!'); END CASE; END;

Summary Types of Loops Types of Control Statements Restrictions with GOTO Statements