C++基础语法¶
📖 章节简介¶
本章将介绍C++的基础语法,包括变量、数据类型、运算符和控制流程。
上图为C++语言标识,作为本章语法入门的视觉锚点。
📦 变量与数据类型¶
1. 基本数据类型¶
C++
// 基本数据类型
#include <iostream>
using namespace std;
int main() {
// 整数类型
int age = 25;
short s = 10;
long l = 100000;
long long ll = 10000000000;
// 浮点类型
float f = 3.14f;
double d = 3.141592653589793;
long double ld = 3.14159265358979323846;
// 字符类型
char c = 'A';
wchar_t wc = L'中';
// 布尔类型
bool flag = true;
// 输出
cout << "整数: " << age << endl;
cout << "浮点数: " << d << endl;
cout << "字符: " << c << endl;
cout << "布尔值: " << flag << endl;
return 0;
}
2. 常量与变量¶
C++
// 常量与变量
#include <iostream>
#include <string>
using namespace std;
int main() {
// 常量定义
const int MAX_AGE = 120;
const double PI = 3.141592653589793;
// 宏定义常量
#define MAX_SCORE 100
// 变量声明和初始化
int score = 95;
string name = "张三";
double price = 99.99;
// 多个变量声明
int x = 10, y = 20, z = 30;
// 输出
cout << "最大年龄: " << MAX_AGE << endl;
cout << "圆周率: " << PI << endl;
cout << "最高分: " << MAX_SCORE << endl;
cout << "姓名: " << name << endl;
cout << "分数: " << score << endl;
return 0;
}
🔢 运算符¶
1. 算术运算符¶
C++
// 算术运算符
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
// 基本运算
cout << "a + b = " << (a + b) << endl; // 13
cout << "a - b = " << (a - b) << endl; // 7
cout << "a * b = " << (a * b) << endl; // 30
cout << "a / b = " << (a / b) << endl; // 3(整数除法)
cout << "a % b = " << (a % b) << endl; // 1(取余)
// 自增自减
int x = 5;
cout << "x++ = " << x++ << endl; // 5(先使用后增加)
cout << "++x = " << ++x << endl; // 7(先增加后使用)
cout << "x-- = " << x-- << endl; // 7(先使用后减少)
cout << "--x = " << --x << endl; // 5(先减少后使用)
return 0;
}
2. 关系运算符¶
C++
// 关系运算符
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
// 关系运算
cout << "a == b: " << (a == b) << endl; // 0 (false)
cout << "a != b: " << (a != b) << endl; // 1 (true)
cout << "a > b: " << (a > b) << endl; // 0 (false)
cout << "a < b: " << (a < b) << endl; // 1 (true)
cout << "a >= b: " << (a >= b) << endl; // 0 (false)
cout << "a <= b: " << (a <= b) << endl; // 1 (true)
return 0;
}
3. 逻辑运算符¶
C++
// 逻辑运算符
#include <iostream>
using namespace std;
int main() {
bool a = true, b = false;
// 逻辑运算
cout << "a && b: " << (a && b) << endl; // 0 (false)
cout << "a || b: " << (a || b) << endl; // 1 (true)
cout << "!a: " << (!a) << endl; // 0 (false)
// 短路求值
int x = 10;
bool result = (x > 5) && (++x < 20);
cout << "result: " << result << endl; // 1 (true)
cout << "x: " << x << endl; // 11(由于第一个条件为真,++x被执行了)
return 0;
}
🔄 控制流程¶
1. if-else语句¶
C++
// if-else语句
#include <iostream>
using namespace std;
int main() {
int score = 85;
// 简单if语句
if (score >= 60) {
cout << "及格" << endl;
}
// if-else语句
if (score >= 90) {
cout << "优秀" << endl;
} else {
cout << "良好" << endl;
}
// if-else if-else语句
if (score >= 90) {
cout << "A" << endl;
} else if (score >= 80) {
cout << "B" << endl;
} else if (score >= 70) {
cout << "C" << endl;
} else if (score >= 60) {
cout << "D" << endl;
} else {
cout << "F" << endl;
}
return 0;
}
2. 循环语句¶
C++
// 循环语句
#include <iostream>
using namespace std;
int main() {
// for循环
cout << "for循环:" << endl;
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
cout << endl << endl;
// while循环
cout << "while循环:" << endl;
int j = 1;
while (j <= 10) {
cout << j << " ";
j++;
}
cout << endl << endl;
// do-while循环
cout << "do-while循环:" << endl;
int k = 1;
do {
cout << k << " ";
k++;
} while (k <= 10);
cout << endl << endl;
// 嵌套循环(九九乘法表)
cout << "九九乘法表:" << endl;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
cout << j << "×" << i << "=" << (i * j) << "\t";
}
cout << endl;
}
return 0;
}
3. break和continue¶
C++
// break和continue
#include <iostream>
using namespace std;
int main() {
// break语句
cout << "break语句:" << endl;
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // 跳出循环
}
cout << i << " ";
}
cout << endl << endl;
// continue语句
cout << "continue语句:" << endl;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // 跳过本次迭代
}
cout << i << " ";
}
cout << endl;
return 0;
}
💡 最佳实践¶
1. 命名规范¶
C++
// 命名规范
#include <iostream>
#include <string>
using namespace std;
int main() {
// ✅ 好的命名
int studentAge = 20;
string userName = "张三";
bool isVip = true;
const int MAX_COUNT = 100;
// ❌ 不好的命名
int a = 20; // 不明确
string s = "张三"; // 太短
bool flag = true; // 不具体
const int n = 100; // 无意义
return 0;
}
2. 常量使用¶
C++
// 常量使用
#include <iostream>
using namespace std;
int main() {
// ✅ 好的做法:使用const定义常量
const int PASSING_SCORE = 60;
const double PI = 3.141592653589793;
int score = 85;
if (score >= PASSING_SCORE) {
cout << "及格" << endl;
}
// ❌ 不好的做法:使用魔法数字
if (score >= 60) { // 魔法数字
cout << "及格" << endl;
}
return 0;
}
📝 练习题¶
基础题¶
- C++有哪些基本数据类型?
- 如何声明和初始化变量?
- C++的运算符有哪些?
进阶题¶
- 理解运算符的优先级。
- 使用循环实现特定功能。
- 控制流程的嵌套使用。
实践题¶
- 编写一个计算器程序。
- 实现一个简单的猜数字游戏。
- 创建一个成绩统计程序。
📚 推荐阅读¶
🔗 下一章¶
函数与指针 - 学习C++的函数和指针。