0724 c++笔记
switch语句
1. 值班医生
#include <iostream>using namespace std;int main() {int n;cout << "请输入星期的编号";cin >> n;//#程序在设计时,会约定status 的状态值 0未预约switch (n) {case 1:cout << "星期一 李医生" << endl;break;case 2:cout << "星期二 张医生" << endl;break;case 3:cout << "星期三 孙医生" << endl;break;case 4:cout << "星期四 叶医生" << endl;break;case 5:cout << "星期五 华医生" << endl;break;case 6:cout << "星期六 葛医生" << endl;break;case 7:cout << "星期七 宋医生" << endl;break;default:cout << "你的输入有误!" << endl;}return 0;}
循环
1.不停的问候
#include <iostream>using namespace std;int main() {for (int i = 1; i <= 10; i++) {cout << "你好,欢迎光临!" << endl;}return 0;}
for循环
格式1:for(循环变量赋初值;循环条件;增量表达式)语句1;格式2:for(循环变量赋初值;循环条件;增量表达式){语句1;语句2;…………}
2.偶数求和
#include <iostream>using namespace std;int main() {int a = 0;for (int i = 2; i <= 100; i += 2) {a = a + i;}cout << a;return 0;}
```
