Notes
Variables in C++
Definition:
- A variable is a representation of some value or number.
- In programming, variables can store more than just numbers. They can store characters, strings, or even whole sentences.
Steps to Create and Use a Variable:
- Declaration: Specifies the data type and the variable name.
- Assignment: Assigns a value to the variable.
Declaration:
-
When declaring a variable, you need to specify its data type (e.g.,
int,double,char,bool, etc.) and give it a unique identifier (the variable name). -
Example:
int x; // Declares a variable 'x' that will store an integer.
Assignment:
-
To assign a value to a variable, use the assignment operator
=followed by the value. -
Example:
x = 5; // Assigns the value 5 to variable x. -
The declaration and assignment can be combined into one step:
int x = 5; // Declares and assigns the value 5 to x. -
Displaying the Value:
-
Use
std::coutto display the value of a variable. -
Example:
std::cout << x; // Displays the value of x (which is 5).
-
Example of Working with Variables:
-
Declaration:
int x; // Declare an integer variable x. -
Assignment:
x = 5; // Assign the value 5 to x. -
Display:
std::cout << x; // Output the value of x, which is 5.
Different Data Types in C++:
-
int(Integer): Stores whole numbers (e.g., 5, -10).-
Example:
int age = 21; int year = 2023; int days = 7; -
If you assign a decimal value to an
int, the decimal part will be truncated (removed).-
Example:
int days = 7.5; // The value is truncated to 7.
-
-
-
double(Floating-point number): Stores numbers that include a decimal portion.-
Example:
double price = 10.99; double gpa = 2.5; double temperature = 25.1; -
The
doubletype allows decimals, unlikeint.
-
-
char(Character): Stores a single character (e.g., ‘A’, ‘b’, ’$’).-
Example:
char grade = 'A'; char initial = 'B'; char currency = '$'; -
Important note: You can only store one character in a
charvariable. Trying to store more than one character will result in a warning.-
Example (incorrect):
char grade = "AB"; // Warning: Overflow in conversion from string to char.
-
-
-
bool(Boolean): Stores a value that is either true or false.-
Example:
bool student = true; // The student is enrolled. bool power = false; // The power is off. bool for_sale = true; // The item is for sale. -
A boolean is useful for situations that have only two states, like “on” or “off,” “yes” or “no.”
-
-
string(Sequence of characters): Represents a sequence of characters or text.-
Example:
std::string name = "John Doe"; std::string day = "Friday"; std::string food = "Pizza"; std::string address = "123 Fake Street"; -
Important note: A
stringcan hold more than one character, even entire sentences or names.
-
Displaying Variables with Text:
- You can combine a variable with a string of text using
std::coutto display both together.-
Example:
std::cout << "Hello, " << name; // Displays "Hello, John Doe"
-
- Multiple pieces of text can be concatenated and displayed in one statement:
-
Example:
std::cout << "Hello " << name << ", you are " << age << " years old."; // Displays: "Hello John Doe, you are 21 years old."
-
Key Notes on Variables:
-
intstores whole numbers. -
doublestores numbers with decimals. -
charstores single characters. -
boolstores true or false values. -
stringstores a sequence of characters (text).You can store numbers in strings, but they will be treated as text. For example:
std::string age = "21"; // This is treated as text, not a number.
Assignment for Practice:
- In the comment section, you are encouraged to post examples of:
- An integer variable
- A double variable
- A character variable
- A boolean variable
- A string variable
Conclusion:
This lesson introduced variables and their types in C++ programming. We covered how to declare and assign values to variables and how to display them. The key takeaway is understanding how to use different data types (like int, double, char, bool, and string) to store and manipulate different kinds of data in a C++ program.