WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL).

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Selection (decision) control structure Learning objective
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Chapter 4B: More Advanced PL/SQL Programming
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
true (any other value but zero) false (zero) expression Statement 2
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Introduction to PL/SQL
C++ for Engineers and Scientists Third Edition
Python Control of Flow.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Programming Logic and Design, Second Edition, Comprehensive
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Flow of Control Part 1: Selection
Program with PL/SQL Lesson 4. Writing Control Structure.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CPS120: Introduction to Computer Science Decision Making in Programs.
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
Bordoloi and Bock Control Structures: Iterative Control.
Chapter 5: Making Decisions
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Bordoloi and Bock CONTROL STRUCTURES: CONDITIONAL CONTROLS.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
implicit and an explicit cursor
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Pascal Programming Making decisions - Selection Statements National Certificate Unit 4 Carl Smith.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Restrictions Objectives of the Lecture : To consider the algebraic Restrict operator; To consider the Restrict operator and its comparators in SQL.
Controlling Program Flow with Decision Structures.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Program with PL/SQL Lesson 4.
Creating Stored Procedures and Functions
Writing Control Structures
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Handling Exceptions.
Decision Making in Code Logical Tests & Truth Logical Expressions
Expressions and Control Flow in JavaScript
Using Subqueries to Solve Queries
Using the Set Operators
Conditions and Ifs BIS1523 – Lecture 8.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Controlling Program Flow
Presentation transcript:

WRITING CONTROL STRUCTURES (CONDITIONAL CONTROL)

Objectives After completing this lecture, you should be able to do the following: Identify the uses and types of control structures Construct an IF statement Use CASE statements and CASE expressions Construct and identify different loop statements Make use of guidelines while using the conditional control structures

Controlling Flow of Execution

Overview  This lecture describes of PL/SQL control statements: conditional control statements.  IF THEN END IF;  IF THEN ELSE END IF;  IF THEN ELSIF ELSE END IF;

Overview  Conditional control, which is the ability to direct the flow of execution through your program based on a condition.  Almost every piece of code you write will require conditional control, which is the ability to direct the flow of execution through your program based on a condition. You do this with IF-THEN-ELSE.

IF Statements The IF statement allows you to design conditional logic into your programs. With it, you’ll be able to implement requirements such as: If the salary is between ten and twenty thousand, then apply a bonus of $1,500. If the salary is between twenty and forty thousand, apply a bonus of $1,000. If the salary is over forty thousand, give the employee a bonus of $500.

IF statement types  The IF statement comes in three flavors, as shown in the following table: IF typeCharacteristics IF THEN END IF; This is the simplest form of the IF statement. The condition between IF and THEN determines whether the set of statements between THEN and END IF should be executed. If the condition evaluates to FALSE, the code is not executed IF THEN ELSE END IF;This combination implements an either/or logic: based on the condition between the IF and THEN keywords, execute the code either between THEN and ELSE or between ELSE and END IF. One of these two sections of executable statements is performed. IF THEN ELSIF ELSE END IF;This last and most complex form of the IF statement selects a condition that is TRUE from a series of mutually exclusive conditions and then executes the set of statements associated with that condition. If you’re writing IF statements like this using any release from Oracle9i Database Release 1 onwards, you should consider using searched CASE statements instead.

The IF-THEN Combination  The general format of the IF-THEN syntax is as follows: IF condition THEN... sequence of executable statements... END IF;

The IF-THEN Combination  The condition is a Boolean variable, constant, or expression that evaluates to TRUE, FALSE, or NULL.  If condition evaluates to TRUE, the executable statements found after the THEN keyword and before the matching END IF statement are executed.  If condition evaluates to FALSE or NULL, those statements are not executed.

The IF-THEN Combination  The following IF condition compares two different numeric values. Remember that if one of these two values is NULL, then the entire expression returns NULL.  In the following example, the bonus is not given when salary is NULL: IF salary > THEN give_bonus (employee_id,500); END IF;

The IF-THEN Combination  There are exceptions to the rule that a NULL in a Boolean expression leads to a NULL result. Some operators and functions are specifically designed to deal with NULLs in a way that leads to TRUE and FALSE (and not NULL) results.  For example, you can use IS NULL to test for the presence of a NULL: IF salary > OR salary IS NULL THEN give_bonus (employee_id,500); END IF; In this example, “salary IS NULL” evaluates to TRUE in the event that salary has no value, and otherwise to FALSE. Employees whose salaries are missing will now get bonuses too

The IF-THEN Combination  Notes :  Using operators such as IS NULL and IS NOT NULL, are good ways to detect and deal with potentially NULL values.  For every variable that you reference in every Boolean expression that you write, be sure to think carefully about the consequences if that variable is NULL.

Example

The IF-THEN-ELSE Combination  The condition is a Boolean variable, constant, or expression.  If condition evaluates to TRUE, the executable statements found after the THEN keyword and before the ELSE keyword are executed (the “TRUE sequence of executable statements”).  If condition evaluates to FALSE or NULL, the executable statements that come after the ELSE keyword and before the matching END IF keywords are executed (the “FALSE/NULL sequence of executable statements”).

The IF-THEN-ELSE Combination  The important thing to remember is that one of the two sequences of statements will always execute, because IF-THEN- ELSE is an either/or construct. Once the appropriate set of statements has been executed, control passes to the statement immediately following the END IF keyword.  Following is an example of the IF-THEN-ELSE construct that builds upon the IFTHEN example shown in the previous slide: IF salary <= THEN give_bonus (employee_id, 0); ELSE give_bonus (employee_id, 500); END IF;

The IF-THEN-ELSIF Combination  This last form of the IF statement comes in handy when you have to implement logic that has many alternatives; it is not an either/or situation.  The IF-ELSIF formulation provides a way to handle multiple conditions within a single IF statement.  In general, you should use ELSIF with mutually exclusive alternatives (i.e., only one condition can be TRUE for any execution of the IF statement). The general format for this variation of IF is: IF condition-1 THEN statements-1 ELSIF condition-N THEN statements-N [ELSE else_statements] END IF;

The IF-THEN-ELSIF Combination  Each ELSIF clause must have a THEN after its condition.  Only the ELSE keyword does not need the THEN keyword.  The ELSE clause in the IF-ELSIF is the “otherwise” of the statement.  If none of the conditions evaluate to TRUE, the statements in the ELSE clause are executed.  But the ELSE clause is optional.  You can code an IFELSIF that has only IF and ELSIF clauses.  In such a case, if none of the conditions are TRUE, no statements inside the IF block are executed.

Example

Avoiding Syntax Gotchas Keep in mind these points about IF statement syntax:  Always match up an IF with an END IF In all three variations of the IF statement, you must close off the executable statements associated with the conditional structure with an END IF keyword.  You must have a space between the keywords END and IF, If you type ENDIF instead of END IF, the compiler will get confused and give you the following hard-to-understand error messages: ORA-06550: line 14, column 4: PLS-00103: Encountered the symbol ";" when expecting one of the following:

Example