9 C++ statements to kick off your C++ programming journey
Tags: codenewbie, cpp, beginners
Published: 2021-12-22 17:28:09

C++ is a multi-paradigm programming language that gives you incredible flexibility. In order to harness the power of the C++ programming language, you need to know how to handle statements. Every C++ program is written using statements. Statements instruct a computer on which tasks to perform. 

In this article, we’ll cover nine basic statements to help you get started as a C++ programmer. 

**We’ll cover:**
* [Getting started with C++ statements](#statements)
  * [What are statements?](#whatare)
  * [C++ compilers](#compilers)
* [9 C++ statements to kick off your C++ programming journey](#nine)
  * [1. `#include` ](#include)
  * [2. `#define`](#define)
  * [3. `using`](#using)
  * [4. Main function definition](#main)
  * [5. Variable declaration](#variable)
  * [6. `if`](#if)
  * [7. `cout`](#cout)
  * [8. `endl`](#endl)
  * [9. `cin`](#cin)
* [Wrapping up and next steps](#nextsteps)

<a name="statements"></a>
## Getting started with C++ statements

<a name="whatare"></a>
### What are statements?

A statement translates to **instructions to a computer to perform a specific task**. Any C++ program operates off source code, consisting of a culmination of statements determining your computer’s actions. There are many C++ statements to learn. As you start mastering the basics, you can even create your own C++ statements. 

There are various kinds of statements in the C++ programming language. For instance, some statements are called *conditional statements,* because they define a condition upon which a certain task should be performed. This article also includes statements known as *preprocessing directives*. The preprocessor rather than the compiler performs actions on the preprocessing directives. The preprocessor processes the data and produces an output for use in the compiler. Preprocessor directives are preceded by the hash sign (`#`).  

<br>
 

<br>

<a name="compilers"></a>
### C++ compilers
If you don’t have a compiler already, you’ll need one to start running C++ code. A compiler is a program that translates your written source code into executable machine code. Compilers are included in applications known as integrated development environments (IDEs) and code or text editors. Your choice of IDE or code editor might depend on your operating system. For instance, Dev-C++ only runs on Windows, while Visual Studio Code runs on Windows, Linux, and macOS. 
<br>

<a name="nine"></a>
## 9 C++ commands to kick off your C++ programming journey
<a name="include"></a>
### 1. `#include`
The `#include` statement tells your preprocessor to include a specified file in the program. These files are usually header files from the standard library or a current, user-defined library. Header files contain C++ declarations and macro definitions. Including a header file in your program provides the definitions and declarations you need to use related system calls or libraries. 

There are two types of `include` statements, with distinct syntax and uses: 
* `#include<filename>` <filename>
  * Use this statement when including standard files from the standard library. These files contain definitions for pre-defined functions, and they must be included before using functions. For example, I/O functions are contained in an `iostream` file in the standard library. As such, you need to include the `iostream file` before you use an I/O function such as `cout` and `cin`.
* `#include "filename"`
  * Use this statement when including user-defined files from the current directory.
<br>

<a name="define"></a>
### 2. `#define`
The `#define` statement is used to define macros. A macro is a block of code in a program that is assigned a name. Whenever a defined macro’s name appears in your source code, the compiler will replace it with the block of code you assigned to that macro’s name. Leveraging macros makes the process of writing code less tedious.

The syntax for the `#define` statement is as follows: 

```
#define macro_name replacement_text
```

**Note**: No semicolon is needed after a `#define` statement. 
<br>

<a name="using"></a>
### 3. `using`
The [`using` statement](https://www.educative.io/edpresso/what-is-the-using-keyword-in-cpp) tells your compiler to bring a specified member into a current scope, or a specified base class method or variable into the current class’ scope. The `using` keyword is followed by whichever member, class method, or variable needs to be brought into scope.

For instance, we often see the `using` keyword when we’re bringing a namespace such as `std` into scope. The syntax for this scenario is the following:

```
#include <iostream>
using namespace std;
```

<a name="main"></a>
### 4. Main function definition
The main function is a special function called at program startup. In hosted environments such as operating systems, the main function is the entry point from which the computer will begin running code.

Defining the main function requires setting parameters within the parentheses. If no parameters are needed, the parentheses can be empty. You’ll use curly braces `{}` to enclose the statements determining your function’s behavior. No semicolon is needed after function definition. 

The general syntax for defining a function is:
<center>
ReturnType FunctionName ( Parameter1,Parameter2, ...) { Statements }
</center>

The following code shows an example of defining a main function of type `int`:
```
#include <iostream>
using namespace std;

int main(){
// function statements
}
```

<a name="variable"></a>
### 5. Variable declaration
To declare a variable, begin by specifying the variable’s data type, followed by its variable name. 

The following is the syntax for declaring an integer variable `n`:

```
int n;
```
<br>

<a name="if"></a>
### 6. `if`
`if` is a type of [control statement](https://www.educative.io/edpresso/what-are-control-statements) known as a conditional statement. You’ll follow the `if` keyword with a condition. If that condition is met, your compiler will then move on to perform the next task you define (in other words, the *then* of your *if-then* statement). 

The following example demonstrates the syntax for `if`, where 30 > 10 is the condition upon which the program should display the `cout` statement.
```
if (30 > 10) {
  cout << “30 is greater than 10”;
}
```
<br>

<a name="cout"></a>
### 7. `cout`
The `cout` statement is used with an insertion operator or output operator `<<`. `cout` prints the code following the insertion operator onto the console.`cout` is defined in the iostream header file. 

An example of the use of `cout` is the following variation of “Hello world”:

```
#include <iostream>

using namespace std;

int main() {
  cout << "Hello world, ";
  cout << "and all who inhabit it!";
}
==> Hello world, and all who inhabit it!
```

<a name="endl"></a>
### 8. `endl`
`endl` can be used with `cout` to add a new line after text. With this new knowledge, let’s see how we can affect the output of the previous code for `cout`. 

```
#include <iostream>

using namespace std;

int main() {
  cout << "Hello world," << endl;
  cout << "and all who inhabit it!";
}
==> Hello world, 
==> and all who inhabit it!
```

<a name="cin"></a>
### 9. `cin`
The `cin` statement is used with the extraction operator or input operator `>>`. `cin` will assign an inputted value to the variable following the extraction operator. You can use `cin` after variable declaration, and write a `cin` statement to define a value for the variable. When the program runs, the input you’ve entered will be assigned to the variable. `cin` is defined in the iostream header file.
 
`cin` and `cout` are often used together. In the following example, a `cout` statement displays a message prompting a user to input a value. The `cin` statement then assigns the value to the variable.

```
#include <iostream>
using namespace std;

int main() {
  int age;
    cout << "Enter your age"; // Prompts user to input value
    cin >> age; // Assigns user input to variable age
}
```

<a name="nextsteps"></a>
## Wrapping up and next steps
Congratulations! You now know nine basic C++ statements. There’s so much more to learn. The best way to learn is to practice using these statements in your code. 

To help you hit the ground running as a C++ programmer, Educative has created the [**C++ from Scratch**](https://www.educative.io/courses/learn-cpp-from-scratch) course. You’ll find several C++ tutorials and C++ templates. And with a cloud-based IDE, you can practice coding immediately from any computer without installing an IDE or code editor. 

*Happy learning!*
<br>


### Continue learning about C++
* [Why C++ is a good first language to learn](https://www.educative.io/blog/c-is-a-good-first-language-to-learn)
* [Learn C++ from scratch: The complete guide for beginners](https://www.educative.io/blog/how-to-learn-cpp-the-guide-for-beginners)
* [Is C++ still a good language to learn for 2022?](https://www.educative.io/blog/learn-cpp-for-2022)
<br>

### Start a discussion
Was this guide helpful for you? How have you been starting your C++ programming journey? Let us know in the comments below!