Share c++ bài 2,

lyaiminh.loan

New member
#C ++, #C ++ Hướng dẫn, Lập trình #C ++, #learnC ++, #Programming ### C ++ Bài 2: Biến và kiểu dữ liệu và loại dữ liệu

Trong bài học trước, chúng tôi đã tìm hiểu về những điều cơ bản của lập trình C ++.Trong bài học này, chúng ta sẽ tìm hiểu về các biến và loại dữ liệu.

## Biến

Một biến là một vị trí được đặt tên trong bộ nhớ lưu trữ một giá trị.Giá trị của một biến có thể được thay đổi trong quá trình thực hiện một chương trình.

Để khai báo một biến, bạn sử dụng cú pháp sau:

`` `
<Kiểu dữ liệu> <Tên biến>;
`` `

Ví dụ: mã sau tuyên bố một biến có tên là `num` loại` int`:

`` `
int num;
`` `

## Loại dữ liệu

Có nhiều loại dữ liệu khác nhau trong C ++.Các loại dữ liệu phổ biến nhất là:

* `int`: Một loại dữ liệu số nguyên lưu trữ toàn bộ số.
* `float`: Kiểu dữ liệu dấu phẩy động lưu trữ các số với các điểm thập phân.
* `char`: Một kiểu dữ liệu ký tự lưu trữ một ký tự.
* `bool`: Kiểu dữ liệu Boolean lưu trữ giá trị của` true` hoặc `false '.

Bạn có thể chỉ định loại dữ liệu của một biến khi bạn khai báo nó.Ví dụ: mã sau tuyên bố một biến có tên là `num` loại` int`:

`` `
int num;
`` `

Bạn cũng có thể chỉ định loại dữ liệu của một biến khi bạn gán một giá trị cho nó.Ví dụ: mã sau tuyên bố một biến có tên là `num` của loại` int` và gán giá trị `10` cho nó:

`` `
int num = 10;
`` `

## toán tử vận hành

Bạn có thể sử dụng các toán tử số học để thực hiện các hoạt động toán học trên các biến.Bảng sau đây liệt kê các toán tử số học phổ biến nhất:

|Nhà điều hành |Mô tả |
| --- | --- |
|`+` |Bổ sung |
|`-` |Phép trừ |
|`*` |Phép nhân |
|`/` |Sư đoàn |
|`%` |Mô đun |

Ví dụ: mã sau đây thêm các giá trị của các biến `num1` và` num2` và lưu trữ kết quả trong biến `sum`:

`` `
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
`` `

## Đầu vào và đầu ra

Bạn có thể sử dụng các đối tượng `cin` và` cout` để đọc đầu vào từ bàn phím và ghi đầu ra vào bảng điều khiển.Bảng sau liệt kê các toán tử đầu vào và đầu ra phổ biến nhất:

|Nhà điều hành |Mô tả |
| --- | --- |
|`CIN >>` |Đọc đầu vào từ bàn phím |
|`cout <<` |Viết đầu ra vào bảng điều khiển |

Ví dụ: mã sau đọc giá trị của một số từ bàn phím và in giá trị vào bảng điều khiển:

`` `
int num;
cin >> num;
cout << num << endl;
`` `

## Bài tập

1. Khai báo một biến có tên `num` và gán giá trị` 10` cho nó.
2. Khai báo một biến có tên `name` và gán giá trị` "John Doe" `cho nó.
3. Thêm các giá trị của các biến `num1` và` num2` và lưu trữ kết quả trong biến `sum`.
4. In giá trị của biến `sum` vào bảng điều khiển.
5. Viết một chương trình yêu cầu người dùng nhập tên của họ và sau đó in tên vào bảng điều khiển.
=======================================
#C++, #C++Tutorial, #C++Programming, #learnC++, #Programming ### C++ Lesson 2: Variables and Data Types

In the previous lesson, we learned about the basics of C++ programming. In this lesson, we will learn about variables and data types.

## Variables

A variable is a named location in memory that stores a value. The value of a variable can be changed during the execution of a program.

To declare a variable, you use the following syntax:

```
<data type> <variable name>;
```

For example, the following code declares a variable named `num` of type `int`:

```
int num;
```

## Data Types

There are many different data types in C++. The most common data types are:

* `int`: An integer data type stores whole numbers.
* `float`: A floating-point data type stores numbers with decimal points.
* `char`: A character data type stores a single character.
* `bool`: A Boolean data type stores a value of either `true` or `false`.

You can specify the data type of a variable when you declare it. For example, the following code declares a variable named `num` of type `int`:

```
int num;
```

You can also specify the data type of a variable when you assign a value to it. For example, the following code declares a variable named `num` of type `int` and assigns the value `10` to it:

```
int num = 10;
```

## Arithmetic Operators

You can use arithmetic operators to perform mathematical operations on variables. The following table lists the most common arithmetic operators:

| Operator | Description |
|---|---|
| `+` | Addition |
| `-` | Subtraction |
| `*` | Multiplication |
| `/` | Division |
| `%` | Modulus |

For example, the following code adds the values of the variables `num1` and `num2` and stores the result in the variable `sum`:

```
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
```

## Input and Output

You can use the `cin` and `cout` objects to read input from the keyboard and write output to the console. The following table lists the most common input and output operators:

| Operator | Description |
|---|---|
| `cin >>` | Read input from the keyboard |
| `cout <<` | Write output to the console |

For example, the following code reads the value of a number from the keyboard and prints the value to the console:

```
int num;
cin >> num;
cout << num << endl;
```

## Exercises

1. Declare a variable named `num` and assign the value `10` to it.
2. Declare a variable named `name` and assign the value `"John Doe"` to it.
3. Add the values of the variables `num1` and `num2` and store the result in the variable `sum`.
4. Print the value of the variable `sum` to the console.
5. Write a program that asks the user to enter their name and then prints the name to the console.
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top