Computer Science

Statements in C

Statements in C are instructions that tell the computer what to do. They are the building blocks of C programs and can be used to perform a wide range of tasks, from simple arithmetic operations to complex data manipulations. Some common types of statements in C include assignment statements, conditional statements, and loop statements.

Written by Perlego with AI-assistance

4 Key excerpts on "Statements in C"

  • Learn C Programming
    Exploring Conditional Program Flow .
  • Looping statements : These include while(){…} , do(){…}while , and for(){…} . They are similar to control statements, but their primary purpose is to iterate ; that is, to perform a statement 0 or more times. We'll explore these further in Chapter 7 , Exploring Loops and Iterations .
  • Expression statements : These are simple statements that evaluate expressions and return some kind of result or value. We'll examine these in Chapter 5 Exploring Operators and Expressions .
  • Except for control, looping, and the wide variety of simple expression statements available, we have already encountered the essential C program statements that make up the bulk of our C programs. Now, we will explore functions, function definitions, function return values, function parameters, and function calls further.

    Introducing functions

    Functions  are callable  segments of program code that perform one or more statements of related computational work. Functions group statements into a cohesive set of instructions that perform a specific, complex task. This may comprise a single statement, only a few statements, or many statements. Functions can also call other functions. Functions made up of one or more statements, are the next, higher-up, more complex units of program composition. Statements make functions; functions make programs. main()  is a function made of statements and other functions.
    The process of writing programs – or rather, solving a given problem – with a computer program is primarily the task of breaking the problem down into smaller pieces – into functions – and focusing on the work to be done in each smaller piece. When we break a problem down into smaller parts, we can easily see the essence of the problem. We can focus our attention either on aspects of the larger problem or on the fine details of the subdivided problem pieces.
  • Ivor Horton's Beginning Visual C++ 2012
    • Ivor Horton(Author)
    • 2012(Publication Date)
    • Wrox
      (Publisher)
    As you’ll see, all functions consist of a header that defines (among other things) the function name, followed by the function body that consists of a number of program statements enclosed between a pair of braces. The body of a function may contain no statements at all, in which case, it doesn’t do anything.
    A function that doesn’t do anything may seem somewhat superfluous, but when you’re writing a large program, you may map out the complete program structure in functions initially, but omit the code for many of the functions, leaving them with empty or minimal bodies. Doing this means that you can compile and execute the whole program with all its functions at any time and add detailed coding for the functions incrementally.

    Program Statements

    The program statements making up the function body of main() are each terminated with a semicolon. It’s the semicolon that marks the end of a statement, not the end of a line. Consequently, you can spread a statement over several lines when this makes the code easier to follow, and several statements can appear in a single line. The program statement is the basic unit in defining what a program does. This is a bit like a sentence in a paragraph of text, where each sentence stands by itself in expressing an action or an idea, but relates to and combines with the other sentences in the paragraph in expressing a more general concept. A statement is a self-contained definition of an action that the computer is to carry out, but that can be combined with other statements to define a more complex action or calculation.
    The action of a function is always expressed by a number of statements, each ending with a semicolon. Take a quick look at each of the statements in the example just written, just to get a general feel for how it works. I will discuss each type of statement more fully later in this chapter.
    The first statement in the body of the main() function is:
    int apples, oranges; // Declare two integer variables
    This statement declares two variables, apples and oranges . A variable is just a named bit of computer memory that you can use to store data, and a statement that introduces the names of one or more variables is called a variable declaration. The keyword int in the statement indicates that the variables with the names apples and oranges
  • Programming in C++
    eBook - ePub

    Programming in C++

    Object Oriented Features

    • Laxmisha Rai(Author)
    • 2019(Publication Date)
    • De Gruyter
      (Publisher)
    3 Programming Basics The scholar who cherishes the love of comfort is not fit to be deemed a scholar. ― Confucius

    3.1 Introduction

    It is important to understand simple C++ programs to study the basics of programming. Almost all C++ programs are wrapped around identifiers, variables, keywords, literals, data types, and methods. In this chapter, we will explain all of these fundamental concepts that are significant to understand the nuts and bolts of a programming language such as C++.

    3.2 Variables and identifiers

    Suppose, if we want to perform an operation, that is, calculate the sum of two numbers a and b and store the result in c , then it is represented as:
    c = a + b
    To perform this operation in a program, we must decide the kind of data types the numbers are. Let us say that all of them are integers , then we first declare a, b , and c as integers. Integers are natural and whole numbers that do not include decimals or fractions. Then, the statements in a program must include the following:
    int a,b,c; c = a + b;
    The previous two statements are responsible for allocating memory locations to store data values a, b , and c , where a, b , and c are the names associated with respective memory allocations. These names are called variables .
    The general syntax for variable declaration is as follows: <data type><variables>; Example: int a,b,c;
    Here a, b , and c are variables and int is the data type. The above declaration can also be written as follows:
    int a; int b; int c;
    The names given to the variables are called identifiers
  • Ivor Horton's Beginning Visual C++ 2008
    • Ivor Horton(Author)
    • 2011(Publication Date)
    • Wrox
      (Publisher)
    body of the function.
    As you'll see, all functions consist of a header that defines (amongst other things) the function name, followed by the function body that consists of a number of program statements enclosed between a pair of braces. The body of a function may contain no statements at all, in which case it doesn't do anything.
    A function that doesn't do anything may seem somewhat superfluous, but when you're writing a large program, you may map out the complete program structure in functions initially but omit the code for many of the functions leaving them with empty or minimal bodies. Doing this means that you can compile and execute the whole program with all its functions at any time and add detailed coding for the functions incrementally.
    Program Statements
    The program statements making up the function body of main() are each terminated with a semicolon . It's the semicolon that marks the end of a statement, not the end of the line. Consequently a statement can be spread over several lines when this makes the code easier to follow and several statements can appear in a single line. The program statement is the basic unit in defining what a program does. This is a bit like a sentence in a paragraph of text, where each sentence stands by itself in expressing an action or an idea, but relates to and combines with the other sentences in the paragraph in expressing a more general idea. A statement is a self-contained definition of an action that the computer is to carry out, but that can be combined with other statements to define a more complex action or calculation.
    The action of a function is always expressed by a number of statements, each ending with a semicolon. Take a quick look at each of the statements in the example just written, just to get a general feel for how it works. I will discuss each type of statement more fully later in this chapter.
  • 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.