Lab 09: Advanced SQL

Similar documents
Iterative Languages. Scoping

Database Programming with PL/SQL

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Chapter 1 CONTROL STRUCTURES. SYS-ED/ Computer Education Techniques, Inc.

Loops / Repetition Statements

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CS110D: PROGRAMMING LANGUAGE I

Programming Languages

Fundamentals of Programming Session 13

IT 1033: Fundamentals of Programming Loops

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

OVERVIEW OF THE TYPES OF PL/SQL BLOCKS:

Repetition Structures

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Writing Control Structures

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

The for Loop. Lesson 11

Programming for Engineers Iteration

Structured Programming. Dr. Mohamed Khedr Lecture 9

Computational Physics - Fortran February 1997

Loops. Repeat after me

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Lesson 16: Applying the Properties of Operations to Multiply and Divide Rational Numbers

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Chapter 4 Introduction to Control Statements

Chapter 17. Iteration The while Statement

Iteration statements - Loops

Repetition CSC 121 Fall 2014 Howard Rosenthal

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

All about flow control

A Look Back at Arithmetic Operators: the Increment and Decrement

Dept. of CSE, IIT KGP

n Group of statements that are executed repeatedly while some condition remains true

Any Integer Can Be Written as a Fraction

Repetition Structures II

Loops / Repetition Statements

(a) State the differences between SQL and PL/SQL. 2 (b) Differentiate between Cursors and Triggers. 2

While Loops CHAPTER 5: LOOP STRUCTURES. While Loops. While Loops 2/7/2013

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

Chapter 4: Control structures. Repetition

Problem Solving With Loops

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Chapter 4: Control structures

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Control Structures. Control Structures 3-1

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

For Loop. Variations on Format & Specific Examples

Laboratory 5: Implementing Loops and Loop Control Strategies

Full file at

Lecture 7 Tao Wang 1

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Principles of Computer Science

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 4: Repetition Control Structure

Repetition and Loop Statements Chapter 5

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

3 The L oop Control Structure

[Page 177 (continued)] a. if ( age >= 65 ); cout << "Age is greater than or equal to 65" << endl; else cout << "Age is less than 65 << endl";

Repetition, Looping CS101

Lecture 3 Tao Wang 1

Score score < score < score < 65 Score < 50

Lab 09: Advanced SQL

Relationship between Pointers and Arrays

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

2 PL/SQL - fundamentals Variables and Constants Operators SQL in PL/SQL Control structures... 7

Name: Partner: Python Activity 9: Looping Structures: FOR Loops

LAB 5: SELECTION STATEMENTS

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Unit 3. Operators. School of Science and Technology INTRODUCTION

Lecture 10. Daily Puzzle

fifth Solutions to Exercises 85

Ada LOOP statement allows for repetition of a sequence of statements Three forms of a LOOP statement FOR loop_specification LOOP...

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Counters & Time Delays. Microprocessors & Interfacing 1

Recap: Assignment as an Operator CS 112 Introduction to Programming

LAB 12: ARRAYS (ONE DIMINSION)

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Top-Down Program Development

EXAMPLE 1. Change each of the following fractions into decimals.

ITDUMPS QUESTION & ANSWER. Accurate study guides, High passing rate! IT dumps provides update free of charge in one year!

REPETITION CONTROL STRUCTURE LOGO

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CS 112 Introduction to Programming

C: How to Program. Week /Apr/16

APPM 2460: Week Three For, While and If s

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Compiler Construction 2010/2011 Loop Optimizations

ECE 102 Engineering Computation

ECE 122. Engineering Problem Solving with Java

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

APCS Semester #1 Final Exam Practice Problems

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Transcription:

CIS395 - BMCC - Spring 2018 04/25/2018 Lab 09: Advanced SQL A - Use Simple Loops with EXIT Conditions In this exercise, you use the EXIT condition to terminate a simple loop, and a special variable, v_counter, which keeps count of the loop iterations. The value of v_counter is incremented and displayed on the screen with each iteration of the loop. 1- Execute the following PL/SQL script: v_counter NUMBER := 0; -- increment loop counter by one -- if EXIT condition yields TRUE exit the loop IF v_counter = 5 THEN EXIT; DBMS_OUTPUT.PUT_LINE ('Done...'); 2- How many times did the loop execute? 3- What is the EXIT condition for this loop? 4- Rewrite and execute the script using the EXIT WHEN condition instead of the EXIT condition so that it produces the same result. To accomplish this task, replace the IF statement with EXIT WHEN v_counter = 5; statement. B - Use WHILE Loops In this exercise, you use a WHILE loop to calculate the sum of the integers between 1 and 10. 5- Execute the following PL/SQL script:

v_counter NUMBER := 1; v_sum NUMBER := 0; WHILE v_counter <= 10 v_sum := v_sum + v_counter; DBMS_OUTPUT.PUT_LINE ('Current sum is: ' v_sum); -- increment loop counter by one DBMS_OUTPUT.PUT_LINE ('The sum of integers between 1 and 10 is: ' v_sum); 6- What is the test condition for this loop? 7- How many times did the loop execute? 8- How many times will the loop execute a) if v_counter is initialized to 0? b) if v_counter is initialized to 10? 9- Rewrite and execute the script to calculate the sum of the even integers between 1 and 100. C - FOR Loops 10- Execute the following PL/SQL script: FOR v_counter IN 1..5 In this example, there is no declaration section for the PL/SQL block because the only variable used, v_counter, is the loop counter. Numbers 1..5 specify the range of the integer numbers for which this loop is executed. USING THE REVERSE OPTION IN THE : The next example demonstrates the usage of the IN REVERSE option for the loop. 11- Execute the following PL/SQL script: FOR v_counter IN REVERSE 1..5 PREMATURE TERMINATION OF THE :

12- Execute the following example of a FOR loop that uses the EXIT WHEN condition. This condition causes the loop to terminate prematurely. FOR v_counter IN 1..5 EXIT WHEN v_counter = 3; D - Use Numeric FOR Loops with the IN Option In this exercise, you use a numeric FOR loop to calculate a factorial of 10 (10! = 1*2*3...*10). 13- Execute the following PL/SQL script: v_factorial NUMBER := 1; FOR v_counter IN 1..10 v_factorial := v_factorial * v_counter; DBMS_OUTPUT.PUT_LINE ('Factorial of ten is: ' v_factorial); 14- Rewrite and execute the above script using the IN REVERSE option. E CONTINUE Statement The CONTINUE statement exits the current iteration of a loop and transfers control to the next iteration. 15- Execute the following PL/SQL script: v_counter NUMBER := 0; DBMS_OUTPUT.PUT_LINE ('before continue condition, v_counter = ' v_counter); -- if CONTINUE condition yields TRUE, exit the current -- iteration and pass control to the next iteration IF v_counter < 4 THEN CONTINUE; DBMS_OUTPUT.PUT_LINE ('after continue condition, v_counter = ' v_counter); -- if EXIT condition yields TRUE exit the loop IF v_counter = 5 THEN EXIT; DBMS_OUTPUT.PUT_LINE ('Done...');

F - Nested Loops We have explored three types of loops: simple loops, WHILE loops, and numeric FOR loops. Any of these three types of loops can be nested inside one another. For example, a simple loop can be nested inside a WHILE loop, and vice versa. 16- Execute the following PL/SQL script: v_counter1 INTEGER := 0; v_counter2 INTEGER; WHILE v_counter1 < 3 DBMS_OUTPUT.PUT_LINE ('Counter1 = ' v_counter1); v_counter2 := 1; DBMS_OUTPUT.PUT_LINE ('------------Counter2 = ' v_counter2); v_counter2 := v_counter2 + 1; EXIT WHEN v_counter2 > 3; v_counter1 := v_counter1 + 1; 17- Execute the following PL/SQL script: FOR i IN 1..10 FOR j IN 1..i -- append an asterisk at the end of the current line DBMS_OUTPUT.PUT('* '); -- start a new line DBMS_OUTPUT.PUT_LINE(''); 18- How many times did the outer loop execute? 19- How many times did the inner loop execute? 20- Rewrite and execute the script using the REVERSE option for the outer loop. 21- Using any types of nested loops, create a PL/SQL script that produces the following output: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8

22- Using any types of nested loops, create a PL/SQL script that produces the following output: 10 15 20 25 30 35 40 45 12 18 24 30 36 42 48 54 14 21 28 35 42 49 56 63 16 24 32 40 48 56 64 72 18 27 36 45 54 63 72 81 20 30 40 50 60 70 80 90 22 33 44 55 66 77 88 99 23- Using any types of nested loops, create a PL/SQL script that produces the following output: 1 22 333 4444 55555 666666