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 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. We’ll revisit all of these topics again in more detail in future lessons.

Line 1 is a special type of line called a preprocessor directive. Preprocessor directives perform a special task prior to compilation. In this case, we are indicating 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 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 be executed when we run our program. std::cout (which stands for “character output”) and the operator << allow us to send text (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!”. Non-zero numbers are typically used to indicate that something went wrong, and the program had to abort. 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.

If you want to compile and run this program yourself, you can, 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 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 program must have?

Show Solution

Question #4

When a program is run, where does execution start?

Show Solution

Question #5

Which 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
Configuring your compiler: Warning and error levels

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

  • Great explanation, having a small quiz at the end was very helpful, thanks :)

  • Found that you need to declare a function just like a variable before it can be used. So this for example will not work:

    However if you move the test() function to before the main like this:

    It will.
    Just felt if worth noting as I'm used to JS at this point and functions can be called from a any point in the file before or after its "declared", and there are probably more in that same situation.  
    I got stuck for a little minute there just though that someone might have more trouble finding the solution.

  • george

    Thanks that clears it up :)

    P.s. I love the tutorial

  • george

    What does arbitrary value mean?

    • Alex

      By arbitrary value, I really meant a value that is left to the programmer's choice.

      I rewrote the definition to make it clearer:

      Variables serve the same purpose here: to provide a name for a value that can vary.

      I think that captures the intent slightly better.

  • Aly Khairy

    When I pasted
    #include <iostream>

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

    the cout and main weren't coloured.

    The code wouldn't run and gave me an error.

  • Dyl@n

    Thank you for making this so easy to understand, my only question is that after I finish this whole thing should I be able to write c and c++ without a problem? I just dont want to spend all this time and have the lessons graze over important things later on.

  • AD Das

    hey man..these are all using visual c++ but in our school we use turbo C++ where the syntax is much different.. is there anywhere where i can learn using turbo C++ ??

    • Alex

      Most of what is taught in these tutorials should still work in Turbo C++, excluding newer C++ features (e.g. C++11 stuff) and particular features they never implemented full support for.

  • R4Z3R

    What is the difference between header and library!?

    • Alex

      A header file is a file that typically contains declarations meant to be used by other files. Once your programs get to the point where they're more than one file, header files become quite common.

      A library is typically a collection of reusable header files and pre-compiled code. In order to use the library, you #include the header files in your code and link in the precompiled code.

      I cover header files in lesson 1.9 -- Header files and libraries in appendix A.

  • Thanks for this awesome post

  • techdummy

    MR ALEX i dont know anything at all and i am a dummy in this field. Do i need to know anything beforehand to get started into the world of programming.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">