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
- What are statements?
- C++ compilers
- 9 C++ statements to kick off your C++ programming journey
- 1 .
#include
- 2 .
#define
- 3 .
using
- 4 . Main function definition
- 5 . Variable declaration
- 6 .
if
- 7 .
cout
- 8 .
endl
- 9 .
cin
- 1 .
- Wrapping up and next steps
Getting started with C++ statements
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 (#
).
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. If you don’t have one already, check out our picks for the best C++ IDEs and code editors on the Educative blog. Your choice of program might depend on your operating system. For instance, Dev-C++ only runs on Windows, while Visual Studio Code runs on Windows, Linux, and macOS.
9 C++ statements to kick off your C++ programming journey
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>
- 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 theiostream file
before you use an I/O function such ascout
andcin
.
- 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
#include "filename"
- Use this statement when including user-defined files from the current directory.
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.
3. using
The using
statement 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;
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:
The following code shows an example of defining a main function of type int
:
#include <iostream>
using namespace std;
int main() {
// function statements
}
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;
6. if
if
is a type of control statement 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”;
}
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!
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!
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
}
Visit the original article on Educative to test your knowledge with an interactive quiz!
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 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!
Continue learning about C++ on Educative
- A tutorial on modern multithreading and concurrency in C++
- Why C++ is a good first language to learn
- Learn C++ from scratch: The complete guide for beginners
- Is C++ still a good language to learn for 2022?
Start a discussion
What do you hope to accomplish with C++ in the future? Was this article helpful? Let us know in the comments below!