Programming Basics
In this section, we will cover the basics of programming, including variables, data types, and basic operations.
Variables
Variables are used to store data in a program. They have a name and a data type. In C++, you need to declare the variables before you can use them.
1#include <iostream>
2using namespace std;
3
4int main() {
5 // Example: Variables
6 string playerName = "Kobe Bryant";
7 int pointsScored = 81;
8
9 // Output
10 cout << "Player Name: " << playerName << endl;
11 cout << "Points Scored: " << pointsScored << endl;
12
13 return 0;
14}
In the above example, we declare two variables: playerName
of type string
and pointsScored
of type int
. We then assign values to these variables: Kobe Bryant
for playerName
and 81
for pointsScored
. Finally, we use the cout
statement to output the values of these variables.
When naming variables, it is important to use meaningful names that describe the data they store. This makes the code more readable and easier to understand.
Data Types
In C++, every variable has a data type that determines what kind of data it can store. Some common data types in C++ include:
int
- for whole numbersfloat
- for floating-point numbersdouble
- for double-precision floating-point numberschar
- for single charactersbool
- for boolean values (true
orfalse
)string
- for sequences of characters
Basic Operations
Once you have variables with values, you can perform various operations on them. Some basic operations include:
- Arithmetic operations (+, -, *, /, %)
- Comparison operations (==, !=, <, >, <=, >=)
- Logical operations (&&, ||, !)
These operations allow you to manipulate and compare values to perform different tasks in your program.
Now that you have an understanding of variables, data types, and basic operations, you are ready to dive deeper into the world of programming!
xxxxxxxxxx
using namespace std;
int main() {
// Example: Variables
string playerName = "Kobe Bryant";
int pointsScored = 81;
// Output
cout << "Player Name: " << playerName << endl;
cout << "Points Scored: " << pointsScored << endl;
return 0;
}