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

11 Key excerpts on "Control Statements in SQL"

  • Book cover image for: Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

    A step-by-step methodology with problem solving approach (English Edition)

    HAPTER 3Control Statements Introduction
    In this chapter, we will dive into the fundamental constructs that allow programmers to control the flow of their programs and make decisions based on specific conditions. We will begin by exploring control statements, which provide mechanisms for altering the sequential execution of code. Next, we will delve into decision-making statements, such as if-else and switch statements, which enable us to choose different paths of execution based on the evaluation of conditions. Additionally, we will learn techniques for handling multiple cases efficiently. Furthermore, we will explore the various looping structures available in C, including the while , do-while , and for loops, which enable repetitive execution of code. By the end of this chapter, we will have a solid understanding of these important programming constructs, equipping us with the skills to control program flow and make informed decisions in our C programs. Let us begin this exciting journey into the world of control and decision-making in C programming.
    Structure The chapter covers the following topics:
    • Control statements
    • Decision-making statements
    • Different loops used in C
    • Loop control statements
    Objectives
    The objective of this chapter is to provide a comprehensive understanding of control statements and decision-making statements in C programming. We will explore the concept of control flow and learn how to make decisions based on conditions using if-else and switch statements. Additionally, we will explore handling multiple switch-case statements and implementing looping structures such as the while , do-while , and for
  • Book cover image for: 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
  • Book cover image for: Python Fundamentals
    No longer available |Learn more

    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
  • Book cover image for: 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.
  • Book cover image for: Abstraction and Closure in Computer Science
    ________________________ WORLD TECHNOLOGIES ________________________ Chapter 3 Control Flow and Structured Programming Control flow In computer science, control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated. Within an imperative programming language, a control flow statement is a statement whose execution results in a choice being made as to which of two or more paths should be followed. For non-strict functional languages, functions and language constructs exist to achieve the same result, but they are not necessarily called control flow statements. The kinds of control flow statements supported by different languages vary, but can be categorized by their effect: • continuation at a different statement (unconditional branch or jump), • executing a set of statements only if some condition is met (choice - i.e. conditional branch), • executing a set of statements zero or more times, until some condition is met (i.e. loop - the same as conditional branch), • executing a set of distant statements, after which the flow of control usually returns (subroutines, coroutines, and continuations), • stopping the program, preventing any further execution (unconditional halt). Interrupts and signals are low-level mechanisms that can alter the flow of control in a way similar to a subroutine, but usually occur as a response to some external stimulus or event (that can occur asynchronously), rather than execution of an 'in-line' control flow statement. Self-modifying code can also be used to affect control flow through its side effects, but usually does not involve an explicit control flow statement (an exception being the ALTER verb in COBOL). ________________________ WORLD TECHNOLOGIES ________________________ At the level of machine or assembly language, control flow instructions usually work by altering the program counter.
  • Book cover image for: Programming with C++
    • Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons, , Kyla McMullen, Kyla McMullen, Elizabeth Matthews, June Jamrich Parsons(Authors)
    • 2021(Publication Date)
    Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. PROGRAMMING WITH C++ 102 Boolean expressions conditional logical operator conditional statement control structure decision control structure else-if structure equal operator fall through if-then structure if-then-else structure nested-if structures relational operator switch-case structure truth table Key Terms SUMMARY • A control structure alters the sequential execution of statements in a computer program. A decision control structure alters the sequential flow by branching to specific statements based on a condition or decision. Decision control structures help programs seem intelligent because they can make responses that correspond to user input. • Decision control structures can be classified as if-then, if-then-else, nested-if, else-if, and switch-case. • Decision control structures begin with a conditional statement such as if choice == 1. • Conditional statements include relational operators, such as == != > < >= and <=. • The == equal operator, not the = assignment operator, is used in conditional statements. • Relational operators and operands form Boolean expressions, such as choice == 1, that evaluate to True or False. • Boolean expressions have similarities to Boolean data types. Both carry a value of True or False. In practice, however, you use a Boolean expression as part of a conditional statement, whereas you use a Boolean data type when making declaration or assignment statements. • Fall through refers to program execution that continues to the next conditional statement within a struc- ture. Switch-case structures typically have fall through unless break keywords are added.
  • Book cover image for: 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
  • Book cover image for: 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
  • Book cover image for: BASIC
    eBook - PDF

    BASIC

    Made Simple Computerbooks

    • J. Maynard(Author)
    • 2016(Publication Date)
    • Made Simple
      (Publisher)
    But only one set of program statements would be written to deal with each and every array entry. The selection of alternative program paths, the repetition of program statements and the iteration of a section of program are 50 Basic controlled by statements generically known as program control. These and their use are discussed below. 3.1 DECISIONS The simplest decision statement has already been used in the program examples of Chapter 2. The GOTO (or GO TO) statement transfers control to another line number. Each time a GOTO is executed control is always passed to the given line number. The transfer of program control is said to be uncondition-al. Thus execution of 340 GOTO 560 always causes program execution to transfer to line 560. Execution of the lines following line 340 above will only take place if they too are the subject of a GOTO statement. At the end of a program it is preferable to return automatically to the BASIC system to proceed with another job. This is done with the STOP statement 920 STOP When line 920 is reached execution of your program terminates and control returns to BASIC. You may then write another program, rerun the one just finished, call up a stored program from disc, or return to the operating system. The most frequent type of program control required is simple branching. This is when the program has to examine the current value of a data item and, depending on its value, either transfer control to a given line number or carry on with the next sequential line number. This test is performed by the IF statement in combination with the THEN clause which defines the action to be taken if the IF test is met. This can be best illustrated with an example. To transfer control to line 200 if the value of COUNT is 10 use the following 600 IF COUNT=10 THEN GOTO 200 The keyword GOTO is optional and the statement can be written
  • Book cover image for: 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:
  • Book cover image for: Fundamentals of Python
    eBook - PDF
    This type of control statement is also called a one-way selection statement , because it consists of a condition and just a single sequence of statements. If the condition is True , the sequence of statements is run. Other-wise, control proceeds to the next statement following the entire selection statement. Here is the syntax for the if statement: if : Selection: if and if-else Statements Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 80 Loops and Selection Statements C H A P T E R 3 Simple if statements are often used to prevent an action from being performed if a condition is not right. For example, the absolute value of a negative number is the arithmetic negation of that number, otherwise it is just that number. The next session uses a simple if statement to reset the value of a variable to its absolute value: >>> if x < 0: x = –x Multi-Way if Statements Occasionally, a program is faced with testing several conditions that entail more than two alternative courses of action. For example, consider the problem of converting numeric grades to letter grades. Table 3-3 shows a simple grading scheme that is based on two assumptions: that numeric grades can range from 0 to 100 and that the letter grades are A, B, C, and F.
  • 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.