Changing Variable Values
Note that if you assign a new value to an existing variable, it will overwrite the previous value:
Example
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
cout << myNum; // Outputs 10Other Types
A demonstration of other data types:
Example
int myNum = 5; // Integer (whole number without decimals)
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)
int myNum = 5; // Integer (whole number without decimals)
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
The cout object is used together with the << operator to display variables.
To combine both text and a variable, separate them with the << operator:
Example
int myAge = 35;
cout << "I am " << myAge << " years old.";
int myAge = 35;
cout << "I am " << myAge << " years old.";You can also combine different types, which you will learn more about in a later chapter.
Example
string name = "John";
int age = 35;
double height = 6.1;
cout << name << " is " << age << " years old and " << height << " feet tall.";
string name = "John";
int age = 35;
double height = 6.1;
cout << name << " is " << age << " years old and " << height << " feet tall.";Add Variables Together
To add a variable to another variable, you can use the + operator:
Example
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;watch this video
resources
https://www.w3schools.com/cpp/cpp_variables.asp