Skip to main content
Engineering LibreTexts

6.5: Compound Statement

  • Page ID
    29061
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The Compound Statement

    Often, we will want to have more than a single statement within our if statements. Some texts refer to this as a compound statement. The brace symbols – the opening { and the closing } - are used to create a compound statement. For example:

    if(expression)
    {
      statement;
      statement;
    }
    else
    {
      statement;
      statement;
    }
    

    You can indeed use the braces with only a single statement, and many programmers do that just to be consistent with all of their code. Just be aware that some people refer to this as a compound statement. Others simply refer to it as a block of code. Either is correct, you just need to understand the terminology.

    Other Uses of a Compound Statement

    "A compound statement is a unit of code consisting of zero or more statements. It is also known as a code block or a block of code. The compound statement allows a group of statements to become one single entry. You used a compound statement in your first program when you formed the body of the function main. All C++ functions contain a compound statement known as the function body.

    A compound statement consists of an opening brace, optional declarations, definitions, and statements, followed by a closing brace. Although all three are optional, one should be present."1

    Definitions

    Compound Statement
    A unit of code consisting of zero or more statements.
    Block
    Another name for a compound statement.
     

    Footnotes

    1 Behrouz A. Forouzan and Richard F. Gilberg, Computer Science A Structured Approach using C++ Second Edition (United States of America: Thompson – Brooks/Cole, 2004) 100.


    This page titled 6.5: Compound Statement is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.