Search

1.1 — Statements and the structure of a program

Chapter introduction

Welcome to the first primary chapter of these C++ tutorials!

In this chapter, we’ll take a first look at a number of topics that are essential to every C++ program. Because there are quite a few topics to cover, we’ll cover most at a fairly shallow level (just enough to get by). The goal of this chapter is to help you understand how basic C++ programs are constructed. By the end of the chapter, you will be able to write your own simple programs.

In future chapters, we’ll revisit the majority of these topics and explore them in more detail. We’ll also introduce new concepts that build on top of these.

In order to keep the lesson lengths manageable, topics may be split over several subsequent lessons. If you feel like some important concept isn’t covered in a lesson, it’s possible that it’s covered in the next lesson.

Statements

A computer program is a sequence of instructions that tell the computer what to do. A statement is a type of instruction that causes the program to perform some action.

Statements are by far the most common type of instruction in a C++ program. This is because they are the smallest independent unit of computation in the C++ language. In that regard, they act much like sentences do in natural language. When we want to convey an idea to another person, we typically write or speak in sentences (not in random words or syllables). In C++, when we want to have our program do something, we typically write statements.

Most (but not all) statements in C++ end in a semicolon. If you see a line that ends in a semicolon, it’s probably a statement.

In a high-level language such as C++, a single statement may compile into many machine language instructions.

For advanced readers

There are many different kinds of statements in C++:

  1. Declaration statements
  2. Jump statements
  3. Expression statements
  4. Compound statements
  5. Selection statements (conditionals)
  6. Iteration statements (loops)
  7. Try blocks

By the time you’re through with this tutorial series, you’ll understand what all of these are!

Functions and the main function

In C++, statements are typically grouped into units called functions. A function is a collection of statements that executes sequentially. As you learn to write your own programs, you’ll be able to create your own functions and mix and match statements in any way you please (we’ll show how in a future lesson).

Rule

Every C++ program must have a special function named main (all lower case letters). When the program is run, execution starts with the first statement inside of function main and then continues sequentially.

Programs typically terminate (finish running) when the last statement inside function main is executed (though they may abort early in some circumstances).

Functions are typically written to do a specific job. For example, a function named “max” might contain statements that figures out which of two numbers is larger. A function named “calculateGrade” might calculate a student’s grade from a set of test scores. We will talk a lot more about functions soon, as they are the most commonly used organizing tool in a program.

Author's note

When discussing functions, it’s fairly common shorthand to append a pair of parenthesis to the end of the function’s name. For example, if you see the term main() or doSomething(), this is shorthand for functions named main or doSomething respectively. This helps differentiate functions from other kinds of objects (such as variables) without having to write the word “function” each time.

Dissecting Hello world!

Now that you have a brief understanding of what statements and functions are, let’s return to our “Hello world” program and take a high-level look at what each line does in more detail.

Line 1 is a special type of line called a preprocessor directive. This preprocessor directive indicates that we would like to use the contents of the iostream library, which is the part of the C++ standard library that allows us to read and write text from/to the console. We need this line in order to use std::cout on line 5. Excluding this line would result in a compile error on line 5, as the compiler wouldn’t otherwise know what std::cout is.

Line 2 is blank, and is ignored by the compiler. This line exists only to help make the program more readable to humans (by separating the #include preprocessor directive and the subsequent parts of the program).

Line 3 tells the compiler that we’re going to write (define) a function called main. As you learned above, every C++ program must have a main function or it will fail to compile.

Lines 4 and 7 tell the compiler which lines are part of the main function. Everything between the opening curly brace on line 4 and the closing curly brace on line 7 is considered part of the main function. This is called the function body.

Line 5 is the first statement within function main, and is the first statement that will execute when we run our program. std::cout (which stands for “character output”) and the << operator allow us to send letters or numbers to the console to be output. In this case, we’re sending it the text “Hello world!”, which will be output to the console. This statement creates the visible output of the program.

Line 6 is a return statement. When an executable program finishes running, the program sends a value back to the operating system in order to indicate whether it ran successfully or not. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”. This is the last statement in the program that executes.

All of the programs we write will follow this general template, or a variation on it.

Author's note

If parts (or all) of the above explanation are confusing, that’s to be expected at this point. This was just to provide a quick overview. Subsequent lessons will dig into all of the above topics, with plenty of additional explanation and examples.

You can compile and run this program yourself, and you will see that it outputs the following to the console:

Hello world!

If you run into issues compiling or executing this program, check out lesson 0.8 -- A few common C++ problems.

Syntax and syntax errors

In English, sentences are constructed according to specific grammatical rules that you probably learned in English class in school. For example, normal sentences end in a period. The rules that govern how sentences are constructed in a language is called syntax. If you forget the period and run two sentences together, this is a violation of the English language syntax.

C++ has a syntax too: rules about how your programs must be constructed in order to be considered valid. When you compile your program, the compiler is responsible for making sure your program follows the basic syntax of the C++ language. If you violate a rule, the compiler will complain when you try to compile your program, and issue you a syntax error.

Let’s see what happens if we omit the semicolon on line 5 of the “Hello world” program, like this:

Feel free to compile this ill-formed program yourself.

Visual Studio produces the following error (your compiler may generate an error message with different wording):

c:\vcprojects\test1.cpp(6): error C2143: syntax error : missing ';' before 'return'

This is telling you that you have a syntax error on line 6: the compiler was expecting a semicolon before the return statement, but it didn’t find one. Although the compiler will tell you which line of code it was compiling when it encountered the syntax error, the omission may actually be on a previous line. In this case, the error is actually at the end of line 5 (the compiler didn’t discover the issue until line 6).

Syntax errors are common when writing a program. Fortunately, they’re typically straightforward to find and fix, as the compiler will generally point you right at them. Compilation of a program will only complete once all syntax errors are resolved.

You can try deleting characters or even whole lines from the “Hello world” program to see different kinds of errors that get generated. Try restoring the missing semicolon at the end of line 5, and then deleting lines 1, 3, or 4 and see what happens.

Quiz time

The following quiz is meant to reinforce your understanding of the material presented above.

Question #1

What is a statement?

Show Solution

Question #2

What is a function?

Show Solution

Question #3

What is the name of the function that all programs must have?

Show Solution

Question #4

When a program is run, where does execution start?

Show Solution

Question #5

What symbol are statements in C++ often ended with?

Show Solution

Question #6

What is a syntax error?

Show Solution

Question #7

What is the C++ Standard Library?

Show Hint

Show Solution


1.2 -- Comments
Index
0.12 -- Configuring your compiler: Choosing a language standard

350 comments to 1.1 — Statements and the structure of a program

  • mqu

    "A statement is an instruction in a computer program that tells the computer to perform an action."

    I was trying to make sense out of this definition, but I couldn't.
    What exactly is "an instruction in a computer program"? It makes intuitive sense to me, what is an instruction in assembler language, but what is an instruction in C++? Is this term defined somewhere in relation to C++ or other high-level languages?

    Is it equivalent to `statement`? From the definition, it seems like there could be other `types of instructions` other than those that tell the computer to perform an action. Does anyone else think this concept is not particularly clear?

    Also, the answer to `what is a function` seems a little ambiguous: 'A collection of statements that executes sequentially.'

    That can be said about any arbitrary set of sequential statements, taken out of context (e.g. from the middle of some function), however, it won't really constitute a function.

  • Why OS should be returned "0"?

    Why should the OS knows whether our program is run successfully or not as it is not going to fix it.

    "When an executable program finishes running, the program sends a value back to the operating system in order to indicate whether it ran successfully or not. "

    • Alex

      Because some other program may care whether your program executed successfully so that it can decide what to do next based on whether your program succeeded or not.

      For example, in an installer, an installer may execute some other program, and if that other program is not successful, the installer will terminate with an error. Otherwise it will proceeed.

      The status code from the other program can be used to determine "success".

      • SengT

        I just want to confirm, in the example above, we don't need return 0 correct? Also out of curiosity, does the return statement come with any other number besides 0?

        • Alex

          main() will implicitly return 0 if you don't provide a return statement. It's the only function that does this.

          You can return other values if you like.

  • Rod Nussbaumer

    "When a program is run, where does execution start?"

    Execution of the program starts with the code that calls main(). You probably should have asked what the first statement, or line of your code is first to execute.

    • nascardriver

      There are several steps that happen before `main` is called. `main` isn't even always the first user-defined function that gets called. However, at this point in the tutorials, we haven't covered anything that happens before `main`, so that's what we consider the entry point. Formulating this question in a way that makes it correct would be too confusing for the majority of readers.

  • Ahmed

    Not a big deal but: "std::cout (which stands for “character output”)"...

    "cout" actually stands for "console out"

    • nascardriver

      "cout" standard for "character out[put]".
      Here's the answer from the original author of C++
      http://www.stroustrup.com/bs_faq2.html#cout

  • Hello,

    Firstly i would like to thank you very much for the lessons and tell you how much i appreciate the time you must have put into this.

    Secondly, i have a simple question:

    Why use

    and not write at the beginning

    .

    Thank you!

  • KapyoniK

    You can use the constant EXIT_SUCCESS as a return value instead of 0. It's a constant specifically designed for this. Same goes for EXIT_FAILURE.
    They are defined in stdlib.h as follow :

    Using such constants is a good way to prevent bugs in future evolutions. I think it's a good practice to know about them and use them.

    Tip : if you want to see the code of any given piece of code in Visual Studio (but it works on other IDEs as well) press CTRL while clicking on a name like a link, and you will be directed to the source code of that item.

  • Md jaynal abedin

    main()->int
    {
        std::cout << "Hello world!\n"   ;
        return 0                        ;
    }

    // It is an C++ one-line comment.

    /* It is an C multi-line comment.
    * Comments are ignored by compiler,
    * and are exist for humans to read. */

    /* Semicolon in C++ for statements
    * is like a period for sentences
    * in the real English. */

    /* Preprocessor directive on line 1
    * tells the compiler to insert there a
    * header file of the iostream library. */

    /* The whitespace on line 2 is
    * being ignored by compiler, but
    * enhances the code's readability. */

    /* Declaration statement on line 3
    * was not terminated by semicolon.
    * It tells the compiler to expect the
    * definition of the main() within a
    * lines below, between the braces. */

    /* Braces on lines 4 and 7 are
    * borders of the main() body. */

    /* Statement on line 5 is first that
    * will be executed at the program's
    * runtime, because it is the first
    * statement of the venerable main(). */

    /* Statement on line 6 with "return"
    * keyword is the last to be executed
    * within main(). If anything is placed
    * below the "return", it will not be
    * executed. That is useful for the
    *

  • Charon

    >When discussing functions, it’s fairly common shorthand to append a pair of parenthesis to the end of the function’s name. For example, if you see the term main() or doSomething(), this is shorthand for functions named main or doSomething respectively. This helps differentiate functions from other kinds of objects (such as variables) without having to write the word “function” each time.

    Uff, thats just plain wrong. Something and Something() is not the same. One is a variable, the other is a function.

    >This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”.

    It actually doesnt return anything else than 0. How 0 is interpreted is totally up to whatever continues to use this value.

    An example for this would be if you ask your daughter if she liked the food and she says "It was fine." than its totally up to you to interpret what that means.

    • Brandon

      Of course Something and Something() are not the same. That's exactly why they say that parenthesis are often used in order to make the distinction. "...if you see the term main() or doSomething(), this is shorthand for functions named main or doSomething respectively." You literally repeated exactly what they said and then said they were wrong. Never once did they say that Something and Something() are the same, rather that the latter is used to denote when you are describing a function...

      As for the return value, of course this is left up for interpretation by the process that invoked the program. This tutorial is designed for beginners, and given that 0 is standard convention for saying "everything went okay", this is sufficient to say in order to not overwhelm the new programmer.

      Don't complain just to be heard.

      • Charon

        >Of course Something and Something() are not the same. That's exactly why they say that parenthesis are often used in order to make the distinction. "...if you see the term main() or doSomething(), this is shorthand for functions named main or doSomething respectively." You literally repeated exactly what they said and then said they were wrong. Never once did they say that Something and Something() are the same, rather that the latter is used to denote when you are describing a function...

        doSomething() is not a shorthand for anything. It IS a function ( or the instantiation of a function template, or ... ). For the machine and the human it is a function.

        When did your parents tell you about the birds and the bees stuff ? Not right from the beginning i would assume ? So your parents told you about the world, but they definitely told you a placeholder story, so that you dont inquire anymore. Why do people do that ? Simply because we dont want to let children focus on sex, which is something unecessary until they reach a certain age (up to the parents to decide).

        If i try to teach you something, and i say " ... and then X used to make Y", and you ask "What is X ?" but i dont want you to focus on X, i tell you a placeholder. The only reason of the placeholder is because your brain cant make a connection to nothing. And when i tell you about a system, but there is not enough time to explain everything, i let your brain make connection to placeholder items, like X.

        Now this obviously has advantages, but i want to highlight disadvantages:

        1. First you have to create a nonsensical placeholder.
        2. Secondly, your brain connects it to other things, which are all wrong. For instance "Everything went ok." is not a computer term, or has anything to do with anything. But your brain connects it ofcourse.
        3. Lastly, once you learn how things really work your brain has to "unlearn" connections. This is very painful. I mean that literally, it is a painful process, and your brain will try to avoid it at all costs, with the reasoning that the original connection must have had [i]some[/i] meaning. Which we ofcourse know it didnt. But try convincing your brain of that.

        One of the bigger disadvantages of this system is that you use brain processing power on 100% garbage. The second disadvantage is that you will never really get rid of BAD connections. Its like trying to wipe a harddrive. No matter how hard you scrub it, and what tool you use, with carefully excecuted tools you will always find traces of old data.

        Check my comment 0.2 where i said the same thing. It should be right at the top.

        If you want to know how to properly learn than i recommend this talk by Kathy Sierra.
        https://www.youtube.com/watch?v=FKTxC9pl-WM
        If links are against the sites rules this can be taken down.

        The reasoning for my comments are twofold:
        1. The site profits from feedback and user profit from improved instructions based on feedback.
        2. Writing things out helps me order thoughts and oppinions i have, makes them transparent for other peopel to review, check for logical and knowledge errors, and give feedback. It also helps me attract like minded people, which might read those comments, understand the depth of perception the writer has and get in touch.

        So my concrete suggestions are:
        case 1: doSomething() is not shorthand for anything, it directly is a function for the C++ language ( or a function template, but that is something the reader doesnt need to know about currently )
        case 2: I find my father-daughter exmaple infintely more truthful and accurate than making a garbage placeholder. Simply saying the program can give another program a value, or [i]return[i/] a value, is more truthful than the example given.

        Proper teaching mostly consists of what NOT to teach, so you dont squander your students brain power on garbage.

        Cheers
        Charon

        • Alex

          > doSomething() is not a shorthand for anything

          This is easily disproven by finding articles that use function_name() to talk about functions outside of code examples.

          Here's a good example: https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#i6-prefer-expects-for-expressing-preconditions -- the authors use "expects()" as shorthand for "the expects function". If they didn't value the additional readability that convention can provide, they would have just written "expects".

          (Side note: for extra fun, look who the authors of that site are)

          Context is everything here. In C++ code, doSomething and doSomething() are absolutely not the same thing. In common language (which we use much more often, including on non-reference technical sites), we often talk about things in different ways from how we might if we're being technically precise. Sometimes this is borne of ignorance, sometimes of convention, but most often just for ease of discussion (e.g. saying "const reference" is a lot easier than "reference to a const value").

          Throughout this tutorial series, I try point out where these disconnects between common language and technical language exist.

          To your other point, I added a note that 0 meaning "everything is okay" is a common convention. Technically, there's no reason it couldn't be otherwise, but if it's otherwise, you're going against convention and that has its own cognitive burdens associated.

          • Charon

            >This is easily disproven by finding articles that use function_name() to talk about functions outside of code examples.

            My apologises, i read "When discussing functions," as "On the topic of functions," instead of literally interpreting it as "When [i]discussing[/i] functions,", eg. a literal discussion.

            >To your other point, I added a note that 0 meaning "everything is okay" is a common convention. Technically, there's no reason it couldn't be otherwise, but if it's otherwise, you're going against convention and that has its own cognitive burdens associated.

            And thats exactly my point, you are making a wrong statement, in order to not explain a convention explcitly, a logic step so to speak. In one word, you make the reader participate in a "Cargo Cult", by making them imitate a concept without them actually understanding why they are doing it. I think i pointed out why this is a burden enough already.

            I find your revision infinitely better, than what was written before there.

            I have to point out that i have no clue how to target the "general" mass of people who want to learn how to program, since i have no clue on the general mass of people. So i can only represent my own viewpoint.

            The only thing i am saying is that people imitate what other people say, before they understand it. In that regard they are no different to a parrot. And if they simply imitate what other people are talking, you want to make sure that at least they are repeating correct things, before they understand it. No "logic gaps" allowed.

            Cheers
            Charon

            • Alex

              A belated correction here: a return value of 0 from main() _is_ defined by the C++98 standard to mean a successful exit in C++. Also returning 0 from your program does not guarantee the status code returned to the OS will be 0 -- it may be a different value that represents a successful execution on that OS.

  • Alek

    Hello,
    This code ís C++17 version ?

  • krishna

    why is int used to declare main section ....cant we use void instead as it when it used as a declaration statement it is more universal??

    • nascardriver

      `main`'s return value is used by other programs to determine if execution of your program was successful (`return 0` means success, everything else means failure)

  • Vitaliy Sh.

    List of possible typos:

    "We'll also introduce new concepts that build on top of these."
    ** 'built' && 'on the top' ?

    "Excluding this line would result in a compile error ..."
    ** 'compiler' (text in Question #6 solution: '... is a compiler error ...') ?

    "std::cout (which stands for “character output”) and the operator << ..."
    ** 'operator<<' (with an in-place comment that it's OK)?

    Question #3 - self:
    "What is the name of the function that all program must have?"
    ** 'programs' || 'every program'?

    Question #5 - solution:
    "The semicolon (;)" -- small, hard to visually distinguish from a colon.
    **

      (please, that is not called 'semicolon' in every language...) ?

    Question #7 - solution:
    "The C++ Standard Library is a library that ships with C++ that contains additional functionality to use in your programs."
    ** 'The C++ Standard Library ships with C++. That library contains ...'
    ('... that ships with C++ that ...' is confusing)?

    • nascardriver

      > build/built
      The sentence is correct as is.

      > compile/compiler error
      Both are fine. There are too many lessons to try to achieve perfect consistency.

      > operator<< No quotes needed, but I changed it to "operator <<". That way around it makes more sense until operator overloads are introduced. > all program
      Fixed.

      > The semicolon
      Added code tags, they make it a little bigger.

      > The C++ Standard Library
      Split into 2 sentences.

      Thanks for your suggestions, happy new year!

  • Vitaliy Sh.

    Maybe your, sires, willing to change quizzes Show/Hide buttons?

    I'm know nothing about Java, but want to implement show/hide functionality in my p. use copy of that site for the quizzes.

    In the upper right corner of https://alpinelinux.org/, there is a drop-down menu, without any Java (that i didn't learned yet).

    Based on that above mentioned page, and the different Web sources (mozilla's MDN included), i somehow glued HTML to what is below.

    I edited "Question 7" from here with the help of my web-browser to show the difference between original and Java-less implementation. No, off course, Java can more, but for that particular anchors (Show/Hide) thingy...

    • nascardriver

      Java is used for Android/Desktop development, JavaScript (JS) is used for web-development.
      Although they share a part of their name, they're very different.

      We don't write the lessons in HTML, we only use HTML for some highlights, eg. <em> and <code>. The other features (eg. links, quizzes) are written with bbcode and transpiled to HTML by the server.

      The nice animation on alpinelinux is achieved via CSS.

      The current way of showing/hiding the quizzes works, so there's no reason change it.

  • patrick

    #include <iostream>

    main()
    {
       std::cout << "Hello world!";
       return 0;
    }

    I remove the 'int' but the program still work. why?

  • Vitaliy Sh.

    Hi!

    Can your people, please, embed the "forward reference" to the next chapter "1.2 -- Comments" in there, or earlier?

    With respect to quizes, nevertheless:
    The comments are are useful to express one's understanding of lesson's material by embedding it into the project/solution, somewhere at the bottom of the code.
    And then, comparing that with the lesson's definitions.
    With the small programs like hello it is more convenient than open an separate file. And that will be inside of a project/solution.

    target 1.1_hello of project www_learncpp_com:

  • Vitaliy Sh.

    ...
    If you feel like some important concept isn’t covered in a lesson, it’s possible it’s covered in the next lesson.
    ...

    Maybe a typo: no "that" after "possible".

    Maybe change to:
    ...
    , maybe it's covered in the next lesson.

  • Todd Riemenschneider

    For what its worth anyone using raspian. Here's what I did to run the program.

    To compile a file with c++ on raspian

    g++ -std=c++0x hello.cpp -ohello

    The name of the program to create is specified with the –o flag, in this case we called it hello.  
    The –std=c++0x flag tells the compiler to use the latest version of the c++ standard.

  • Unit

    "In a high-level language such as C++, a single statement may compile into many machine language instructions."

    I thought C++ was considered a low-level language, could it be that it's actually a high-level, but is also considered a low-level language?

    • Alex

      C++ is still generally considered a high-level language, but it's definitely at the lower end of that grouping. The terms "high-level" and "low-level" aren't static, and they have been evolving as newer and higher-level languages have been released.

  • Rusty

    Why is the file location listed out inside the console when I execute the program?

  • Mike

    Where's the line numbers in Visual Studio 2019? Are they turned off by default now or something.

    • Alex

      They should be on by default. But if they're not, you can turn them on by following the instructions here: https://docs.microsoft.com/en-us/visualstudio/ide/reference/how-to-display-line-numbers-in-the-editor?view=vs-2019

Leave a Comment

Put all code inside code tags: [code]your code here[/code]