Computer Science

Programming Control Structures

Programming control structures are used to control the flow of execution of a program. They allow the programmer to specify which statements should be executed under certain conditions, or to repeat a set of statements multiple times. Common control structures include if/else statements, loops, and switch statements.

Written by Perlego with AI-assistance

8 Key excerpts on "Programming Control Structures"

  • 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: Some Assembly Required
    eBook - PDF

    Some Assembly Required

    Assembly Language Programming with the AVR Microcontroller

    • Timothy S Margush(Author)
    • 2016(Publication Date)
    • CRC Press
      (Publisher)
    305 C H A P T E R 9 Control Structures A large percentage of statements in a high-level program are part of the fundamental control structure called sequence. That is, they are executed in the same sequence they appear in the program. The sequence structure is used inside methods, functions, and subroutines. It is used inside blocks and loop bodies. Sequence structures are entered at the top and exited at the bottom. There is no branching into or out of a sequence structure. Most high-level languages provide four basic control structures: sequence, selection, repetition, and call–return. These are indicated by keywords such as if, else, for, while, and by symbols representing a func-tion name. The call–return control structure is usually treated separately under the topic of functions or methods. It is nevertheless, a control struc-ture that serves an important role in programming. In conjunction with recursive techniques, it also provides a powerful iteration structure. Assembly language provides the necessary tools to implement all of these higher-level language constructs. It also allows any number of arbi-trarily complex control structures to be developed. Programmers have learned from experience that restricting programming practices to a few well-understood and carefully implemented structures makes program-ming, debugging, and maintenance much easier and more efficient. Since you are already familiar with the basic control structures used in popular high-level languages, this chapter will demonstrate how to trans-late those structures into assembly language. Remember that the basic fetch–execute cycle includes a step that automatically increments the PC. This is how sequential execution is accomplished. Instructions intended to 306 ◾ Some Assembly Required be executed in sequence are located in successive memory locations so the fetch–execute cycle naturally fetches and executes them in sequence.
  • Book cover image for: Principles Of Computer Programming NQF3 SB
    • S Sasti D Sasti(Author)
    • 2019(Publication Date)
    • Macmillan
      (Publisher)
    155 Module 8 Control structures in high-level programming Module 8 Overview At the end of this module, you will be able to: • Unit 8.1: Explain and implement sequential control structures. • Unit 8.2: Explain and implement logical and relational operators. • Unit 8.3: Explain and use selection control structures. • Unit 8.4: Explain and implement iteration control structures. Unit 8.1: Sequential control structures 8.1.1 Types of control structures Definition: Control structure A control structure in a computer program determines the order in which the program statements are executed. In structured computer programming there are three types of control structures : l Sequential control structure: Code is executed one line at a time from start to finish in order. l Decision control structure (selection): A decision is made and, depending on the outcome of the decision, only certain parts of the code are executed and the other statements are skipped. We will explain this in more detail in Unit 8.3. l Repetition control structure (iteration): This structure allows certain blocks of code statements to be executed many times depending on the conditions. We will explain this in more detail in Unit 8.4. Let’s take a closer look at each control structure. 8.1.2 Sequential control structure Definition: Sequential control structure A sequential control structure is the ordered execution of instructions line by line in a sequential way. The sequential control structure means that the program code is executed line by line in the order in which it is written. Figure 8.1 illustrates a selection control structure by means of a flowchart. 156 Topic 2 We have seen sequential algorithm design in Module 3. From the flowchart in Figure 8.1, we can see the flow of the program. There is no deviation in the algorithm from the Start symbol to the Stop symbol. In Module 7 we introduced you to procedural programming which allows you to break up a problem into smaller parts.
  • Book cover image for: Students' Guide to Programming Languages
    • Malcolm Bull(Author)
    • 2016(Publication Date)
    • Newnes
      (Publisher)
    5: Programming structure and structures Objectives After reading this chapter, you should be able to: • Describe the physical construction of a program and the con-cept of structured programming. • Name the basic constructions which are used in an imperative programming language. • Describe the surface structures by which the underlying con-structions are implemented in specific languages. • Describe an exception condition. • Draw a tree diagram or a Jackson diagram to represent the structure of a program. • Describe the concept of modular programming. • Describe how functions, procedures and/or subroutines are used to construct a program. • Describe some ways in which data can be passed between a program and its subprograms. • Distinguish between local variables and global variables. • Describe the concept of scope. • Distinguish between calling by value and calling by reference. • Describe the concept of common data. In the preceding chapters, we have seen how data is used and how spe-cial data types can be specified. Now we need to look at the processing aspects of a programming language. Throughout this discussion, we shall concern ourselves with imperative programming languages, that is, those languages such as C, Cobol, Basic, Fortran, Pascal and the other third generation languages, where a program represents a set of do this, do that instructions to the computer. The discussion does not 216 Programming structure and structures apply to other language models, such as enquiry languages, logic-ori-ented programming languages and others which we shall meet later. Structured programming In order to see the basic processes which go on inside a program, let us start by looking at the subject of structured programming. Structured programming is a well-known technique for developing computer programs. A structured program is one which is composed of a set of discrete units.
  • Book cover image for: Software for Computer Control
    eBook - PDF

    Software for Computer Control

    Proceedings of the Second IFAC/IFIP Symposium on Software for Computer Control, Prague, Czechoslovakia, 11-15 June 1979

    • M. Novak(Author)
    • 2014(Publication Date)
    • Pergamon
      (Publisher)
    Structured programming is concerned not only with the reliability and correctness of prog-rams that are produced, but also, more importantly, with the assurance of correctness. Program structures must be such that at each level of refinement they can be understood, so that unintended interactions between constit-uents can be detected and eliminated. (A warning flag for incipient trouble is the presence of an interaction between constit-uents at different levels of refinement.) With the classical kind of computing problem, from the total job to be done to the most elementary steps, we can identify units of execution (actions), each of which takes a finite time. While one action is being executed, no other action is being executed, so the actions form a strict sequence in time. Each action has a definite start and a defi-nite finish, at which the system is in a defi-nite state. The program is similarly written so that each specification of an action has one starting point and a single terminating point. At these points there can be assertions about the state of the calculation. The sufficiently simple ways of connection actions at each level are now recognised to be sequencing, selection from mutually exclusive cases, and iterations or repetitions. REAL-TIME PROGRAMMING The traditional structures described above are not adequate for real-time programming problems. The essence of the real-time sys-tem is that the computer is interacting with a number of external processes, not susceptible to alteration by the real-time system designer; the external processes provide stimuli to the computer which must be satisfied by responses within externally defined time constraints. The external processes continue for long periods of time, and continuous availability of the real-time system is important. Also the means of communication with the external processes is likely to involve strange input/ output devices.
  • Book cover image for: Real Time Programming 1985
    eBook - PDF

    Real Time Programming 1985

    Proceedings of the 13th IFAC/IFIP Workshop, Purdue University, West Lafayette, Indiana, USA, 7-8 October 1985

    • G.M. Bull, T.J. Williams(Authors)
    • 2014(Publication Date)
    • Pergamon
      (Publisher)
    Structured Programming emphasizes use of a number of basic program structures: Sequences, IF-THEN-ELSEs, Loops, CASE statements, and possible extensions to deal with parallel activities. However, structured languages like PASCAL do not include the means for making the structures stand out. An invisible structure does not simplify programming, and, in environments like process control, where documentation is not emphasized, the structure should be displayed automatically. Process control needs egoless documentation even more that egoless programming. Languages are most difficult to deal with when they force a continual mental translation between an application perspective and a programming perspective based on the language objects. Among the devices for matching an application to a language, subroutines, named variable and data structures, and enumerated variables, need more variety of mechanisms. The simple subroutine call becomes confusing when a large number of inputs, outputs, and parameters are involved. The process control block is one answer to this problem. But optional arguments and other higher level relationships can usefully be automated. The handling of logic in computer languages is a particularly weak area of design, typically forcing the mental translation of application states onto a TRUE/FALSE Procrustrean bed, only weakly softened by enumerated variables. The earlier discussion of ladder diagrams referenced the difficulty of carrying out mental Boolean calculations to continually interpret their possible effects. But the irregular structure of the typical combination of IF-THEN-ELSEs also contributes to this difficulty. Reversed logic and skip tests form a classic source of programming errors. The CASE statement is certainly a more symmetrical response to the problem, but it requires again a continuous mental translation between an index and the meaning of each of its values.
  • Book cover image for: BASIC
    eBook - PDF

    BASIC

    Made Simple Computerbooks

    • J. Maynard(Author)
    • 2016(Publication Date)
    • Made Simple
      (Publisher)
    3 Program Control The examples at which we have looked thus far have been very limited because the program flow has always been the same—from top to bottom without any opportunity for alternative courses of action. And yet the great value of the program is its ability to choose different paths depending on the data currently being processed. A computer program can examine an item of data—from a file or input by the user or whatever—and select a different range of instructions to be executed depending on the current value of that data. This is akin to selecting a task for the day depending on where in the week we are—if it's Monday—wash the car; if it's Tuesday—do the washing and so on. A more powerful attribute of a program is its ability to execute the same set of instructions for different sets of data. Thus a program written to, say, calculate weekly payroll does not need instructions to identify individual workers and their particular pay anomalies. Each worker will have a file entry in a standard format that will completely describe his pay structure including any peculiarities. One set of instructions will cope with any combina-tion of pay factors. Only a single payroll program needs therefore to be written. Programs can also process repetitive data without intervention between the individual items which make it up. Thus a large array containing details of, say, stock balances could be dealt with by a section of program looping round but changing its array pointer on each pass. The first time though the coding would refer to array item 1; the second time through to array item 2 and so on. 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.
  • Book cover image for: Joe Grand's Best of Hardware, Wireless, and Game Console Hacking
    • Joe Grand, Deborah Kaplan, Frank Thornton, Albert Yarusso, Lee Barken, Tom Owad, Ryan Russell, Bobby Kinstle, Marcus R Brown, Job de Haas(Authors)
    • 2006(Publication Date)
    • Syngress
      (Publisher)
    If you ever need to learn an object-ori- ented language such as Java, you'll have some extra concepts to study, but many of the basic principles will be the same. If you want to do more advanced programming in these languages, look at the suggestions for further reading at the end of this chapter. You'll find yourself writing complex programs in no time! Programming Concepts In this section, we explore some of the essential concepts necessary for any programming language: 9 Assignment 9 Control structures (looping, conditional branching, and unconditional branching) 9 Storage structures (structures, arrays, hash tables, and linked lists) 9 Readability (comments, function and variable names, and pretty printing) These concepts will serve you well for the specific programming languages we're learning here: C and assembly language. But, they are also important general concepts you'll need in any language you might learn, such as C++,Java, or Perl. Coding 101 9 Chapter 4 71 Assignment Assignment occurs when your program stores some information in memory so you can use it later. To get to the information, you need some kind of handle for later access. Frequently we use named variables. Let's say we would like to greet the user of our program by name. We might write a program that stores the user's name in a variable called . The command to print the greeting might look something like: print out Hello, . When the program runs, the computer will recognize as a named variable and will look in its memory in the spot labeled name to find the character string. The variable corresponds to the string Buffy in the example shown in Figure 4.1. The computer will then perform variable substitu- tion and put the string Buffy where it saw the name of the variable: Hello, Buffy. Figure 4.1 Variable Assignment Chunk of computer's memory labeled name Buffy Many programming languages make you declare variables before you use them for the first time.
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.