Output (Print Text) in C++
Example
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
using namespace std;
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
New Lines in C++
Examples:
using namespace std;
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
Pro Tips: Absolutely! When you use consecutive `\n` characters in C++, it creates a blank line, resulting in an empty line in the output.
Example
using namespace std;
int main() {
cout << "Hello World! \n\n";
cout << "I am learning C++";
return 0;
}
An alternative method to insert a new line in C++ is by utilizing the endl
manipulator. By using endl
, a new line is added.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
Comments in C++
Single-line Comments in C++
cout << "Hello World!";
Example
Multi-line Comments in C++
/*
to begin the comment block and */
to end it. Any text enclosed between these delimiters will be disregarded by the compiler and won't be executed.Example
/* The code below will print the words Hello World!
to the screen, and it is amazing */
cout << "Hello World this is blog base media!";
//
) or multi-line (/* */
) comments is entirely up to you. Typically, single-line comments are suitable for shorter explanations, while multi-line comments are more appropriate for longer comments or blocks of text. The choice depends on personal preference and the length of the comment.Variables in C++
- In C++, the `int` data type is used to store whole numbers (integers) without any decimals. Examples of `int` values include 123, -123, 0, and so on.
- In C++, the `double` data type is used to store floating-point numbers, which are numbers that can have decimal places. Examples of `double` values include 19.99, -19.99, 3.14159, and so on.
- In C++, the `char` data type is used to store single characters. Each `char` value is surrounded by single quotes, such as 'a', 'B', '@', or '5'. It can represent alphabets, digits, special characters, or any other single character.
- In C++, the `string` data type is used to store sequences of characters, which represent text. String values are surrounded by double quotes, such as "Hello World", "Blogbasemedia", or "12345". It allows you to manipulate and work with textual data in your program.
- In C++, the `bool` data type is used to store values that have two states: `true` or `false`. It is typically used for logical operations and conditions. A `bool` variable can hold either the value `true` or `false`. For example, you can use a `bool` variable to represent the state of a condition, such as whether a condition is true or false.
Declaring (Creating) Variables in C++
Syntax
type
represents one of the C++ data types like int
, double
, char
, string
, or bool
. The variableName
is the name you choose for the variable, following the rules of C++ variable naming. The equal sign (=
) is used to assign a value to the variable at the time of creation.int
or double
. Here's an example:myNum
of type int
and assign it the value 10:cout << myNum;
myNum = 10;
cout << myNum;
Example:
int myNum = 10; // myNum is 10
myNum = 30; // Now myNum is 10
cout << myNum; // Outputs 30
Other Data Types in C++
double myFloatNum = 5.99; // Floating point number (with decimals)
char myLetter = 'D'; // Character
string myText = "Hello"; // String (text)
bool myBoolean = true; // Boolean (true or false)
Display Variables in C++
Example
cout << "I am " << myAge << " years old.";
Adding Variables Together in C++
Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
Some C++ Exercises to try
Exercise:
Declaring Multiple Variables C++
To declare multiple variables of the same type, separate them with commas.
Example
cout << x + y + z;
Assigning a single value to multiple variables.
Example
x = y = z = 70;
cout << x + y + z;
0 Comments