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

7 Key excerpts on "Statements in C"

  • Book cover image for: 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.
  • Book cover image for: C++ for Engineers and Scientists
    Declaration statements always play a software role of informing the compiler of a func-tion’s valid variable names. When a variable declaration also causes the computer to set aside memory locations for the variable, the declaration statement is called a definition statement. (All declarations used in this chapter have also been definition statements.) 11. The sizeof() operator can be used to determine the amount of storage reserved for vari-ables. Programming Projects for Chapter 2 1. (General math) a. Design, write, compile, and run a C++ program that calculates and displays the area of a triangle, such as the one in Figure 2.18, with a base of 1 in and a height of 1.5 in. The area is given by this formula: Area = 1⁄ 2 (base) × (height) a c b height base Figure 2.18 A two-dimensional triangle b. Manually check the values computed by your program. After verifying that your program is working correctly, modify it to determine the area of a two-dimensional triangle with a base of 3.5 in and a height of 1.45 in. 2. (General math) a. Design, write, compile, and run a C++ program to calculate the volume of a sphere with a radius, r , of 2 in. The volume is given by this formula: Volume = 4 π r 3 ____ 3 b. Manually check the values computed by your program. After verifying that your program is working correctly, modify it to determine the volume of a cube with a radius of 1.67 in. Copyright 2012 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.
  • Book cover image for: 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
  • Book cover image for: C++ Programming
    eBook - ePub
    • Yuan Dong, Fang Yang, Li Zheng(Authors)
    • 2019(Publication Date)
    • De Gruyter
      (Publisher)
    float -type value.

    2.2.6Statement

    Statements control the execution flow of a program, and executing a statement will cause corresponding results. C++ statements include the declaration statement, expression statement, case statement, loop statement, jump statement, compound statement, and label statement. For example, the declaration of a variable is achieved by a declaration statement; adding a semicolon (;) at the end of an expression can make it an expression statement; enclosing several statements with a pair of brackets can constitute a compound statement. We will introduce other statements in the following chapters.
    Note that there are no assignment statements and function call Statements in C++, and we have to use expressions to achieve assignment and function call. We have already introduced assignment expressions in Chapter 2.2.5 , and we will introduce function calls in Chapter 3 .
    Adding a semicolon at the end of an assignment expression will make it a statement. For example:
    a=a+3;
    It is an expression statement, and it achieves the same function as an assignment statement.
    The difference between an expression and expression statement is: an expression can be a part of another compound expression and continue to participate in operations, while a statement does not have this function.

    2.3Data Input and Output

    2.3.1I/O Stream

    In C++, the movement of data from one object to another is abstracted as a “stream”. One should establish a stream before using it, and delete it after use. To get data out from a stream is called an extract operation, and to put data into a stream is called an insert operation. I/O stream is used to achieve data input and output, and cin and cout are predefined stream-type objects. cin dealswith standard inputs, i.e., input from terminal. cout deals with standard outputs, i.e., output directed to terminal.

    2.3.2Predefined Input and Output Operator

    “<<” is the predefined input operator. Using it on the stream-type object cout
  • Book cover image for: C++ Programming
    eBook - PDF

    C++ Programming

    From Problem Analysis to Program Design

    The statements enclosed between the curly braces ({ and }) form the body of the function main. The body of the function main contains two types of statements: ? Declaration statements ? Executable statements Declaration statements are used to declare things, such as variables. Copyright 2016 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. 82 | Chapter 2: Basic Elements of C++ In C11, identifiers, such as variables, can be declared anywhere in the program, but they must be declared before they can be used. EXAMPLE 2-27 The following statements are examples of variable declarations: int a, b, c; double x, y; Executable statements perform calculations, manipulate data, create output, accept input, and so on. Some executable statements that you have encountered so far are the assignment, input, and output statements. EXAMPLE 2-28 The following statements are examples of executable statements: a = 4; //assignment statement cin >> b; //input statement cout << a << " " << b << endl; //output statement In skeleton form, a C11 program looks like the following: //comments, if needed preprocessor directives to include header files using statement named constants, if needed int main() { statement_1 . . . statement_n return 0; } The C11 program in Example 2-29 shows where include statements, declaration statements, executable statements, and so on typically appear in the program. Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.
  • Book cover image for: Computer Fundamentals - 8th Edition
    eBook - ePub

    Computer Fundamentals - 8th Edition

    Concepts, Systems & Applications

    • Pradeep K.Sinha, Pradeep K.Sinha, Priti Sinha(Authors)
    • 2004(Publication Date)
    • BPB Publications
      (Publisher)
    c will have the same value 15.

    STATEMENTS

    C program is combination of statements, written between {} braces. Each statement performs a set of operations. Null statement, represented by “;” or empty {} braces, does not perform any operation.
    Simple statements perform single operation and are always terminated by a semicolon “;”. Compound statements, called statement block, perform complex operations combining null, simple, and other block statements.
    Examples:
    a = (x + y) * 10; /* simple statement */ if (sell > cost) /* compound statement follows */ { profit = sell - cost; printf (“profit is %d”, profit), } else /* null statement follows */ { }

    PERFORMING SIMPLE I/O OPERATIONS IN C

    There is no keyword available in C for performing input/output. C provides standard library functions for performing all input/output operations. Figure 21.6 shows some basic library functions to perform simple I/O operations in C.
    I/O library functions Meanings
    getch() Inputs a single character (most recently typed) from standard input (usually console).
    getche() Inputs a single character from console and echoes (displays) it.
    getchar()
    Inputs a single character from console and echoes it, but requires Enter key to be typed after the character.
    putchar() or putch() Outputs a single character on console (screen).
    scanf()
    Enables input of formatted data from console (keyboard). Formatted input data means we can specify the data type expected as input. Format specifiers for different data types are given in Figure 21.7 .
    printf()
    Enables obtaining an output in a form specified by programmer (formatted output). Format specifiers are given in Figure 21.7 . Newline character “\n” is used in printf() to get the output split over separate lines.
    gets()
    Enables input of a string from keyboard. Spaces are accepted as part of the input string, and the input string is terminated when Enter key is hit. Note that although scanf()
  • Book cover image for: 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.