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

4 Key excerpts on "Programming Control Structures"

  • C Programming
    eBook - ePub

    C Programming

    Learn to Code

    8 Control Structures
    DOI: 10.1201/9781003188254-8

    8.1 Introduction

    Whatever we have discussed till now, all problems have been relatively simple and did not require any decision-making process. But in real life, we come across many problems where we need to decide what to do and what not to do. Assume that we want to collect some flowers from a tree, and the tree has many branches. So, we need to choose a branch in such a manner that we can get the maximum number of flowers (Figure 8.1 ). Similarly, we may have problems that need some work to be done repeatedly. So, to solve different kinds of problems, we need the concept of control structure. Control structure determines the flow of control in a program, that is, it indicates the order in which the various instructions in a program are executed inside the computer.
    Figure 8.1 Decision-making problem example.
    Control statements in C are divided into three categories (Figure 8.2 ):
    • Sequence Control Structure: This ensures that the instructions in the program are executed in the same order in which they appear. As you can see in Figure 8.2a, there are nine program statements, and the compiler starts execution from Statement 1 and continues through to Statement 9 without skipping any of the intervening statements.
    • 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
  • Learn C Programming from Scratch
    eBook - ePub

    Learn C Programming from Scratch

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

    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 loops. By the end of this chapter, readers will have a solid understanding of these fundamental programming constructs, enabling them to effectively control the flow of their C programs and make decisions based on various conditions. Control statements Control statements govern a program’s flow, that is, the order in which instructions in a program may get executed. The presence of control statements in our program enables us to make decisions, to do tasks recurrently, or to jump from one section of code to another. The flow of a program is generally sequential, where instructions are executed one after the other, in sequence, like starting from the first instruction and then executing the second, third, fourth and so on. There are instances when a programmer needs to execute a set of instructions based on certain conditions. For example, executing instructions only if a specific condition is met, such as determining if an input number is even. Similarly, there are cases where a programmer wants to execute a set of instructions continuously until a certain state is reached or a condition is fulfilled. Additionally, there may be situations where it is necessary to skip a set of statements and proceed to a different set of instructions. In such scenarios, the flow of execution may not follow a strict sequential order. For instance, instruction 5 might be followed by instruction 9, or instructions 10, 11, and 12 could be executed multiple times. Furthermore, it may be necessary to skip instructions 21, 22, and 23 and proceed directly to the subsequent instructions. These scenarios are enabled by the presence of control statements in our program
  • 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
  • The PHP Workshop
    eBook - ePub

    The PHP Workshop

    A New, Interactive Approach to Learning PHP

    • Alexandru Busuioc, David Carr, Markus Gray, Vijay Joshi, Mark McCollum, Bart McLeod, M A Hossain Tonu(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)

    3. Control Statements

    Overview
    By the end of this chapter, you will be able to describe Boolean expressions; leverage logical operators to compose Boolean expressions; choose the right comparison operators within a control statement; describe branching and different looping techniques in PHP; apply branching with if else , switch case, break , and continue statements; differentiate between bounded and unbounded loops; implement loops such as while, do while , for , and foreach ; and write a PHP script to create a movie listing application.

    Introduction

    Since PHP is a dynamically typed language where types are associated with data instead of variables, it's essential to understand the role that types play in the data operations landscape. In the previous chapter, we learned about the available data types in PHP, their usage with variables and typecasting. We also practiced adding and removing items from an array and went through type conversion and alternative approaches to assigning string data to a variable with heredoc and nowdoc .
    In this chapter, we will discuss control statements and why they are essential, and we'll explore what PHP has to offer in this area. Control statements are the most important feature of any programming language. In simple terms, they help to control the flow of a program. Branching and looping are the main types of control structures that help to decide program flow. They also help to craft recursive loops for complex program flows.
    Branching allows us to follow the correct path among multiple conditions based on a certain logic. For example, say that we want to make contact with a person. The person might have an email address or a cell number and we might want to either email or SMS the person. A branching structure will help us to determine whether there is an email address associated with that contact information and email the person based on that logic. And if an email address is not available, then we can opt for an alternative communication approach, such as SMS.
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.