C++ 入门速通-第2章【黑马】
内容来源于:黑马
集成开发环境:CLion
先前学习完了C++第1章的内容:
C++ 入门速通-第1章【黑马】-CSDN博客
下面继续学习第2章:
if 逻辑判断:

简单案例:
// if
#include "iostream"
using namespace std;
int main()
{
/*
if (判断)
{
code;
code;
...
}
*/
cout << "今天发工资了" << endl;
int money;
cout << "请输入小明发的工资: " << endl;
cin >> money;
if (money >= 1000)
{
cout << "现在可以买新玩具了" << endl;
money -= 500;
}
// else
// {
// cout << "继续吃泡面" << endl;
// }
cout << "总工资余额还有: " << money << "元" << endl;
return 0;
}
终端输出:
switch控制语句:

简单案例:
// switch control
#include "iostream"
using namespace std;
int main()
{
// 输入数字1 - 7, 输出对应的星期几
int i;
cout << "请输入数字1 - 7: ";
cin >> i;
switch (i) {
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;
default:
cout << "星期日" << endl;
}
return 0;
}
枚举类型:

while 循环语句:

简单案例:
// while control
#include "iostream"
using namespace std;
int main()
{
int i = 1;
int num = 0;
while (i <= 100)
{
// cout << i << endl;
num = num + i;
i++;
}
cout << num << endl;
return 0;
}
for 循环语句:
变量的作用域:

循环中断语句:
goto 语句:

C++一套通关系列课程在线笔记:https://www.yuque.com/bigdata-caoyu/newcp
参考:
第二章-01_if逻辑判断语句_哔哩哔哩_bilibili