C++ String Concatenation

String Concatenation

The + operator can be used between strings to add them together to make a new string. This is called concatenation:

Example

string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;

In the example above, we added a space after firstName to create a space between John and Doe on output. However, you could also add a space with quotes (" " or ' '): Continue reading C++ String Concatenation

C++ Strings

C++ Strings

Strings are used for storing text/characters.

For example, “Hello World” is a string.

A string variable contains a collection of characters surrounded by double quotes: Continue reading C++ Strings

C++ Logical Operators

Logical Operators

As with comparison operators, you can also test for true (1) or false (0) values with logical operators.

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example Try it
&& Logical and Returns true if both statements are true x < 5 &&  x < 10
|| Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

Continue reading C++ Logical Operators

C++ Comparison Operators

Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either 1 or 0, which means true (1) or false (0). These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else article. Continue reading C++ Comparison Operators

C++ Assignment Operators

Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x: Continue reading C++ Assignment Operators

C++ Operators

C++ Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

int x = 100 + 50;

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

Example

int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

C++ divides the operators into the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Continue reading C++ Operators

C++ Data Types Examples

Real-Life Examples

Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items:

Example

// Create variables of different data types
int items = 50;
double cost_per_item = 9.99;
double total_cost = items * cost_per_item;
char currency = '$';

// Print variables
cout << "Number of items: " << items << "\n";
cout << "Cost per item: " << cost_per_item << currency << "\n";
cout << "Total cost = " << total_cost << currency << "\n";

Continue reading C++ Data Types Examples

C++ String Data Types

String Types

The string type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes : Continue reading C++ String Data Types

C++ Character Data Types

Character Types

The char data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:

Example

char myGrade = 'B';
cout << myGrade;

Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters: Continue reading C++ Character Data Types