Computer Science

Control Statements in SQL

Control statements in SQL are used to control the flow of execution of SQL statements. These statements include conditional statements such as IF-THEN-ELSE and CASE statements, as well as iterative statements such as WHILE and FOR loops. Control statements allow for more complex and dynamic SQL queries to be written.

Written by Perlego with AI-assistance

8 Key excerpts on "Control Statements in SQL"

  • Introduction to Programming
    eBook - ePub

    Introduction to Programming

    Learn to program in Java with data structures, algorithms, and logic

    Control Flow Statements

    This chapter describes one particular kind of Java statement
    , called control statements, which allow the building of a program flow according to the logic of the implemented algorithm, which includes selection statements, iteration statements, branching statements, and exception handling statements.
    In this chapter, we will cover the following topics:
    • What is a control flow?
    • Selection statements: if, if....else, switch...case
    • Iteration statements: for, while, do...while
    • Branching statements: break, continue, return
    • Exception handling statements: try...catch...finally, throw, assert
    • Exercise – Infinite loop
    Passage contains an image

    What is a control flow?

    A Java program is a sequence of statements that can be executed and produce some data or/and initiate some actions. To make the program more generic, some statements are executed conditionally, based on the result of an expression
    evaluation. Such statements are called control flow statements because, in computer science, control flow (or flow of control) is the order in which individual statements are executed or evaluated.
    By convention, they are divided into four groups: selection statements, iteration statements, branching statements, and exception handling statements. In the following sections, we will use the term block, which means a sequence of statements enclosed in braces. Here is an example: { x = 42; y = method(7, x); System.out.println("Example"); } A block can also include control statements – a doll inside a doll, inside a doll, and so on.
    Passage contains an image

    Selection statements

    The control flow statements of the selection statements group are based on an expression evaluation. For example, here is one possible format: if(expression) do something. Or, another possible format: if(expression) {do something} else {do something else}.
    The expression may return a boolean value (as in the previous examples) or a specific value that can be compared with a constant. In the latter case, the selection statement has the format of a switch statement, which executes the statement or block that is associated with a particular constant value.
  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    Selection Control Structure: This is also known as decision control structure, and it ensures the computer makes decisions about which statement is to be executed next. Figure 8.2b shows the flow of execution. After executing Statement 2, the compiler arrives at Statement 3 which is a condition, and if the condition is satisfied (is true) then the compiler executes Statements 4, 5, and 6, otherwise it executes 7, 8, and 9.
  • Loop Control Structure: This helps the computer to execute group/single statements repeatedly until a condition is satisfied. Figure 8.2c shows the flow of execution. The compiler executes Statements 4 through 6 repeatedly until the condition becomes true, and the moment it is false, it starts its execution from Statement 7 onwards.
  • Figure 8.2 Categories of control statements.
    All programs discussed so far come under the sequence control structure. In this chapter, we will introduce several programs that will reflect the properties of the selection and loop control structure. After completing this chapter, the student will know the following:
    • What different control structures are available in the C language.
    • The syntax of different control structures.
    • Be able to differentiate between sequence and selection control structure.
    • Write code for more realistic problems.
    • What a compound statement is.
    The C language provides several keywords for control structure declaration. We classify all those keywords according to our control structure category. Figure 8.3 shows a detailed classification of all control statements and introduces several new keywords. We divide the selection control statements into two groups, because some selection statements do not require conditions; we can break the flow of execution arbitrarily.
    Figure 8.3 Classification of control structure.
    In the next section we will discuss all control statements, their syntaxes, flowcharts, and some example programs that show their properties and execution procedure.

    8.2 Selection with if Statements

    Suppose we are executing a set of instructions, and we want some instruction to execute only when a specific condition is satisfied. With the help of an if selection control statement, we can achieve this. The syntax of if, with and without a compound statement, is shown in Figure 8.4
  • Python Fundamentals
    eBook - ePub

    Python Fundamentals

    A practical guide for learning Python, complete with real-world projects for you to explore

    • Ryan Marvin, Mark Ng'ang'a, Amos Omondi(Authors)
    • 2018(Publication Date)
    • Packt Publishing
      (Publisher)
    In this chapter, we will delve into controlling program flow and start working on building more structured programs. This should also prepare us for learning various kinds of loops later in this chapter. To start us off, we are going to define some terms:
    • Program flow
    • Control statement

    Program Flow

    Program flow describes the way in which statements in code are executed. This also includes the priority given to different elements.
    Python uses a simple top-down program flow. This is to say that code is executed in sequence from the top of the file to the very bottom. Each line has to wait until all of the lines that come before it have completed execution before their own execution can begin.
    This top-down program flow makes it easy to understand and debug Python programs, as you can visually step through the code and see where things are failing.
    In a top-down scenario, a problem is broken down into simple modules, each responsible for a part of the solution, closely related to one another. For instance, consider a salary calculation. There would be a module responsible for each of the following:
    • Tax computation
    • Debt computation (if needed)
    • Net amount computation

    Control Statement

    Having defined the program flow, we can now understand what a control statement is.
    A control statement is a structure in code that conditionally changes the program flow. A control statement achieves this by conditionally executing different parts of code. A control statement can also be used to repeatedly and conditionally execute some code.
    You can think of a control statement as a traffic police officer at a junction who only lets traffic through if the exit is clear. Checking whether the exit is clear would be the condition, in this case. The officer will only let cars through the junction when the exit is clear.
    The two main control statements in Python are:
    • if
    • while

    The if Statement

    An if statement allows you to execute a block of code if a condition is true. Otherwise, it can run an alternative block of code in its else
  • Learn T-SQL From Scratch
    eBook - ePub

    Learn T-SQL From Scratch

    An Easy-to-Follow Guide for Designing, Developing, and Deploying Databases in the SQL Server and Writing T-SQL Queries Efficiently

    HAPTER 9

    Variables and Control Flow Statements

    R emember the full form of T-SQL?
    It’s Transact – Structured Query Language . We have learnt querying so far, now let’s get started with the language and programming. In this chapter and onwards, we’ll learn the programmability features and objects.
    In this chapter, you will learn about the variables. In any programming language, variables are the founding stone. It’s nearly impossible that any programming language exists without variables. You will also learn about loop, case, and control flow statements. All these features form the basics of programming.
    If you are familiar with any programming language, then you will find the concepts discussed in this chapter nothing different. Concepts of programming are the same everywhere, in every programming language. The difference is just the syntax.

    Structure

    In this chapter, we will cover the following topics:
    • Variables
    • Tiny, yet powerful keywords
    • CASE statement
    • IF statement
    • WHILE loop

    Objective

    By the end of this chapter, you will know all the features that form the basics of T-SQL programming. You’ll be able to make use of the variables. You’ll be able to implement the loops for performing iterations. You will be able to implement the case statement to check for specific conditions in the query (more specifically in the select, insert, and update statements) and return the desired values. Similarly, you will be able to make use of the control flow statement to define the flow of the code. For example, if a particular condition is true then do X else Y .
    You can build huge programs with the help of queries learned so far, and with these programming features.

    Variables

    Variables can be treated as the temporary storage, which has limited scope in which they are defined. Variable declaration too has a similar definition as that of columns. Each variable should have a data type assigned during declaration, similar to the columns. The data types define the type of data that the variable can hold.
  • Introduction to Python Programming
    3 Control Flow Statements
    AIM Understand if, if…else and if…elif…else statements and use of control flow statements that provide a means for looping over a section of code multiple times within the program. LEARNING OUTCOMES At the end of this chapter, you are expected to •  Use if, if…else and if…elif…else statements to transfer the control from one part of the program to another. •  Write while and for loops to run one or more statements repeatedly.
    •  Control the flow of execution using break and continue statements.
    •  Improve the reliability of code by incorporating exception handling mechanisms through try-except blocks.
    Python supports a set of control flow statements that you can integrate into your program. The statements inside your Python program are generally executed sequentially from top to bottom, in the order that they appear. Apart from sequential control flow statements you can employ decision making and looping control flow statements to break up the flow of execution thus enabling your program to conditionally execute particular blocks of code. The term control flow details the direction the program takes.
    The control flow statements (FIGURE 3.1 ) in Python Programming Language are
    1.  Sequential Control Flow Statements: This refers to the line by line execution, in which the statements are executed sequentially, in the same order in which they appear in the program.
    2.  Decision Control Flow Statements: Depending on whether a condition is True or False, the decision structure may skip the execution of an entire block of statements or even execute one block of statements instead of other (if, if…else and if…elif…else).
    3.  Loop Control Flow Statements: This is a control structure that allows the execution of a block of statements multiple times until a loop termination condition is met (for loop and while
  • Computer Programming for Absolute Beginners
    eBook - ePub

    Computer Programming for Absolute Beginners

    Learn essential computer science concepts and coding techniques to kick-start your programming career

    Chapter 7: Program Control Structures
    If all of our code was simply executed in sequence, our programs would always do the same thing, no matter what data we provided them with. We must be able to control the path through the program so that some part of the code executes at the designated time, and other parts at other times, depending on the values provided by the data. For instance, only if it is cold outside do you put on warm clothes, not always. The same thing applies to our code. When things are a certain way, we want something to happen.
    In a way, we can say that we, by this, will introduce some sort of intelligence, or at least some decision-making capabilities into our code. If things are this way, do this, if not, do that. In this chapter, you will learn the following topics:
    • Controlling the execution path of the program
    • Making decisions with the help of if statements
    • Selecting one out of many options with switch statements
    • Repeating code execution with for loops
    • Iterating over code until a condition is false using while and do while
    • Going over a sequence of data, one item at the time using for each
    In this chapter, we will dive into some real programming. In the topics that we will cover here, we will be able to control the execution path of the program. Let's first explore what that means.

    Controlling the execution path

    In Chapter 5 , Sequence – The Basic Building Block of a Computer Program , we learned that the code within a program is executed in sequence.
    A sequence is one of the three basic logical structures we have in programming. So, in this chapter, we will cover the other two, selection and iteration .

    Selection statements

    There are situations when we only want to execute some code if a condition is met. For example, if you recall our application from Chapter 5 , Sequence - The Basic Building Block of a Computer Program which turned on the outdoor light, we had a condition that said if our phone detected that we were within a given range from our house, it should send a signal to the home computer. To refresh your memory, let's take a look at some images you have seen before. Figure 7.1
  • Python Made Simple
    eBook - ePub

    Python Made Simple

    Learn Python programming in easy steps with examples

    HAPTER 4

    Control Statements

    Introduction

    In Python, statements in a program are executed one after another in the order in which they are written. This is called sequential execution of the program. But in some situations, the programmer may need to alter the normal flow of execution of a program or to perform the same operations a number of times. For this purpose, Python provides a control structure which transfers the control from one part of the program to some other part of the program. A control structure is a statement that determines the control flow of the set of instructions. There are different types of control statements supported by Python like decision control, loop control, and jump statements. We will cover the following topics in this chapter:
    • Concept of indentation
    • Decision control statements
    • Looping statements
    • Jump statements

    Objectives

    • Understanding the concept of various control statements
    • Understanding the different types of decision-making statements and their usage
    • Understanding the concept of looping statements and their usage
    • Understanding the concept of jump statements and their importance

    Concept of indentation

    One of the most distinctive features of Python is its use of indentation to mark a blocks of code. In Python, code blocks are identified by indentation rather than using a symbol like curly braces. Without extra symbols, programs are easier to read. Indentation refers to the spaces that are used at the beginning of a statement. The statement with the same indentation belongs to the same group called a suite. Indentation clearly identifies what code block a statement belongs to. A code block may consist of single or multiple statements. Indentation has no effect on the program logic as it is simply used to align program lines.
    To indicate a code block in Python, you must indent each line of the block by the same amount. By default, Python uses four spaces to indent a block or it can be increased or decreased by the programmer. In most other programming languages, indentation is used only to help make the code look better. But in Python, it is required for indicating what code block a statement belongs to. Consider the following statements:
  • Programming in Python
    eBook - ePub

    Programming in Python

    Learn the Powerful Object-Oriented Programming

    HAPTER 4

    Control Structures

    Highlights

    • Python if, if else, if…elif…if statements
    • Python while, for, infinite loop
    • Python break, continue, and pass statements
    Generally, a program is executed in a sequence normally from top to bottom. The statements get executed one after the other as interpreter transfers the control from the current statement to the next statement as soon as the execution of the current statement gets over. However, in some situations the sequential flow does not work. The control needs to move to some other location in the program depending upon certain condition or there may be a requirement to execute a set of statements repetitively. Therefore, this chapter intends to emphasize on control structures which are used to alter the order of execution of a program. In this chapter, we will learn three types of control structures decision making, looping, and controlling.

    4.1. Decision Making

    Decision making is done with a conditional statement which allows selecting one or another execution path in a program. It causes to execute a particular block of statements after the evaluation of a given conditional expression. In other words, an appropriate action is preceded by the evaluation of a test condition to either true or false. Table 4.1
  • Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.