📊 基础知识地图
🔄
L10-L13
循环结构 while/for/do-while
学习路径回顾
从L1到L13,我们已经学习了C++编程的核心基础知识:
✅ 输入输出 :cin/cout的使用
✅ 变量管理 :声明、初始化、作用域
✅ 数据类型 :int、double、char、bool
✅ 运算符 :算术、关系、逻辑、赋值
✅ 条件判断 :if、switch及其嵌套
✅ 循环控制 :while、for、do-while
✅ 代码规范 :完整程序结构、注释
完整程序结构
#include <iostream>
using namespace std;
int main () {
return 0 ;
}
💡 记住这个模板!
每个C++程序都必须包含这四个部分。
这是我们编写所有代码的基础框架。
🎯 核心概念速查
变量与数据类型
#include <iostream>
using namespace std;
int main () {
int age = 18 ;
double score = 95.5 ;
char grade = 'A' ;
bool passed = true ;
const double PI = 3.14159 ;
cout << "年龄:" << age << endl;
cin >> age;
return 0 ;
}
运算符汇总
#include <iostream>
using namespace std;
int main () {
int a = 10 , b = 3 ;
cout << a + b << endl;
cout << a - b << endl;
cout << a * b << endl;
cout << a / b << endl;
cout << a % b << endl;
cout << (a > b) << endl;
cout << (a == b) << endl;
cout << (a > 5 && b < 5 ) << endl;
cout << (a > 15 || b < 5 ) << endl;
a++;
++b;
return 0 ;
}
条件判断对比
#include <iostream>
using namespace std;
int main () {
int score = 85 ;
if (score >= 90 ) {
cout << "优秀" << endl;
} else if (score >= 80 ) {
cout << "良好" << endl;
} else {
cout << "加油" << endl;
}
char grade = 'B' ;
switch (grade) {
case 'A' :
cout << "优秀" << endl;
break ;
case 'B' :
cout << "良好" << endl;
break ;
default :
cout << "其他" << endl;
}
return 0 ;
}
三种循环对比
#include <iostream>
using namespace std;
int main () {
for (int i = 1 ; i <= 5 ; i++) {
cout << i << " " ;
}
cout << endl;
int j = 1 ;
while (j <= 5 ) {
cout << j << " " ;
j++;
}
cout << endl;
int k = 1 ;
do {
cout << k << " " ;
k++;
} while (k <= 5 );
cout << endl;
return 0 ;
}
💡 综合应用示例
学生成绩管理系统(综合示例)
这个程序综合运用了变量、输入输出、条件判断、循环等所有基础知识。
#include <iostream>
using namespace std;
int main () {
int studentCount;
double totalScore = 0 ;
double maxScore = 0 ;
double minScore = 100 ;
cout << "请输入学生人数:" ;
cin >> studentCount;
for (int i = 1 ; i <= studentCount; i++) {
double score;
cout << "请输入第" << i << "个学生的成绩:" ;
cin >> score;
while (score < 0 || score > 100 ) {
cout << "成绩无效!请重新输入:" ;
cin >> score;
}
totalScore += score;
if (score > maxScore) {
maxScore = score;
}
if (score < minScore) {
minScore = score;
}
}
double average = totalScore / studentCount;
cout << "\n=== 统计结果 ===" << endl;
cout << "平均分:" << average << endl;
cout << "最高分:" << maxScore << endl;
cout << "最低分:" << minScore << endl;
return 0 ;
}
🎮 互动实验:成绩计算器
输入多个成绩,自动计算统计数据
成绩(用逗号分隔):
计算
输入成绩后点击"计算"...
⚠️ 常见易错点
1. 忘记分号
cout << "Hello" << endl
cout << "Hello" << endl;
2. 混淆 = 和 ==
int x = 5 ;
if (x = 10 ) { ... }
if (x == 10 ) { ... }
3. switch忘记break
switch (x) {
case 1 :
cout << "一" ;
break ;
case 2 :
cout << "二" ;
break ;
}
4. 死循环
int i = 1 ;
while (i <= 10 ) {
cout << i << endl;
}
while (i <= 10 ) {
cout << i << endl;
i++;
}
5. 数组越界
int arr[5 ] = {1 , 2 , 3 , 4 , 5 };
cout << arr[5 ] << endl;
for (int i = 0 ; i < 5 ; i++) {
cout << arr[i] << " " ;
}
📝 自我测试
📝 综合测试题
以下程序的输出是什么?
#include <iostream>
using namespace std;
int main () {
int sum = 0 ;
for (int i = 1 ; i <= 5 ; i++) {
if (i % 2 == 0 ) {
sum += i;
}
}
cout << sum << endl;
return 0 ;
}
A. 15
B. 6
C. 9
D. 10
选择答案查看解析...