#|【C++】学习笔记[二]
文章目录
- 一、运算符
- 二、程序流程结构
-
- (一)选择结构
-
- if语句
- switch语句
- (二)循环结构
-
- while循环
- do...while循环
- for循环
- 嵌套循环
- 三、跳转语句
-
- (一)break语句
- (二)continue语句
- (三)goto语句
一、运算符
- 加减乘除取模运算的练习代码
#include
#include
using namespace std;
int main()
{ //加减乘除取余运算
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1 * b1 << endl;
//两个整数相除结果依然是整数,将小数直接丢掉了
cout << a1 / b1 << endl;
//取模运算的本质就是求余数
cout << a1 % b1 << endl;
cout << 10 % 20 << endl;
//10
//10%0报错,两数相除除数不为0,所以也做不了取模运算
//10/0会报错,除数不为0 double d1 = 0.5;
doubled2 = 0.25;
double d3 = 0.22;
cout << d1 / d2 << endl;
cout << d1 / d3 << endl;
//cout << d1 % d2 << endl;
//小数不能进行%运算
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/79c19c179616405da0bbe05ff162f958.jpg)
文章图片
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/a127ee773f8149ab8921e02756c16954.jpg)
文章图片
- 前置++、后置++、前置–、后置–练习代码如下
#include
#include
using namespace std;
int main()
{ //前置递增
int a = 10;
++a;
//让a加一
cout << "a:" << a << endl;
//后置递增
int b = 10;
b++;
cout << "b:" << b << endl;
//前置和后置区别
//前置递增:先让变量+1,然后进行表达式计算
//后置递增:先进行表达式计算,然后让变量+1
int a2 = 10;
int b2 = ++a2 * 10;
cout << "b2" << b2 << endl;
//110 int a3 = 10;
int b3 = a3++ * 10;
cout << "b3:" << b3 << endl;
//100
return 0;
}
三目运算符练习代码:
#include
#include
using namespace std;
int main()
{ int a = 10;
int b = 20;
//如果a>b成立,则输出a,否则输出b
int c = a > b ? a : b;
cout << c << endl;
//20 int d = a < b ? a : b;
cout << d << endl;
//10
//三目运算符返回一个变量
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/6f1f37f8d1eb4a6fafaca4afb4bf0dd5.jpg)
文章图片
- 逻辑运算符【&& || !】
#include
using namespace std;
int main()
{ int a = 10;
int b = 0;
cout << !a << endl;
//0
cout << (a && b) << endl;
//0
cout << (a || b) << endl;
//1
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/e1aea2f6f33e4d7fa6c5c09ba501b70b.jpg)
文章图片
- && :同真为真,其余为假
- || : 同假为假,其余为真
(一)选择结构 if语句
作用:执行满足条件的语句
if语句的三种形式:
- 单行格式
- 多行格式
- 多条件的
- 单行格式if语句
#include
using namespace std;
int main()
{ //单行格式if语句
//用户输入一个分数,分数》600,考上一本大学 int score = 0;
cout << "请输入分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{cout << "恭喜考上一本大学" << endl;
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/241234293bc04b13a765ee5777a6ab50.jpg)
文章图片
- 多行格式if语句
#include
using namespace std;
int main()
{ //多行格式if语句
//用户输入一个分数,分数》600,考上一本大学 int score = 0;
cout << "请输入分数:" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{cout << "恭喜考上一本大学" << endl;
}
else
{cout << "很遗憾,您没有达到一本分数线" << endl;
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/132723c2281e4239843bbe10acf00463.jpg)
文章图片
- 多条件的if语句
#include
using namespace std;
int main()
{ //多条件的if语句
//输入一个分数,如果>600,视为考上一本
//如果>500,视为考上二本
//如果>400,视为考上三本
//如果<=400,视为没有考上大学 int score = 0;
cout << "请输入您的分数" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{cout << "恭喜考上一本大学" << endl;
}
else if (score > 500)
{cout << "恭喜考上二本大学" << endl;
}
else if (score > 400)
{cout << "恭喜考上三本大学" << endl;
}
else
{cout << "很遗憾您没有考上大学!" << endl;
} return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/b0eb87c3006b4c969a1009ea9f4f3cba.jpg)
文章图片
- 嵌套if语句练习代码如下:
#include
using namespace std;
int main()
{ int score = 0;
cout << "请输入您的分数" << endl;
cin >> score;
cout << "您输入的分数为:" << score << endl;
if (score > 600)
{cout << "恭喜考上一本大学" << endl;
if (score > 700)
{cout << "恭喜考入北京大学" << endl;
}
else if (score > 650)
{cout << "恭喜考入清华大学" << endl;
}
else
{cout << "恭喜考入人民大学" << endl;
}
}
else if (score > 500)
{cout << "恭喜考上二本大学" << endl;
}
else if (score > 400)
{cout << "恭喜考上三本大学" << endl;
}
else
{cout << "很遗憾您没有考上大学!" << endl;
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/83795bebab7e49cf984460223d16d60a.jpg)
文章图片
案例练习:三只小猪称体重?
有三只小猪ABC,请分别输入三只小猪的体重,并且判断哪只小猪最重?
代码如下:
#include
using namespace std;
int main()
{ //三只小猪称体重 //创建三只猪的体重变量
int num1 = 0;
int num2 = 0;
int num3 = 0;
//输入三只猪的重量
cout << "请输入A的重量:" << endl;
cin >> num1;
cout << "请输入B的重量:" << endl;
cin >> num2;
cout << "请输入C的重量:" << endl;
cin >> num3;
cout << "A的体重为:" << num1 << endl;
cout << "B的体重为:" << num2 << endl;
cout << "C的体重为:" << num3 << endl;
//判断哪只最重?
if (num1 > num2)//A比B重
{if (num1 > num3)//A比C重
{cout << "小猪A最重" << endl;
}
else//C比A重
{cout << "小猪C最重" << endl;
}
}
else//B比A重
{if (num1 > num3)//A比C重
{cout << "小猪B最重" << endl;
}
else//C比A重
{cout << "小猪C最重" << endl;
}
} return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/7ac0ee5311e9450586be9e51efa5413f.jpg)
文章图片
switch语句
作用:执行多条件分支语句
练习代码如下:
#include
using namespace std;
int main()
{ //给一个电影进行评分
//10 9 经典
// 7 8 非常好
//6 5 一般
//5分以下烂片 int score = 0;
cout << "请输入你电影的评分:" << endl;
cin >> score;
cout << "你打的分数为:" << score << endl;
switch (score)//依据score来打分
{ case 10:
cout << "你认为该电影是经典电影" << endl;
break;
//退出当前分支
case 9:
cout << "你认为该电影是经典电影" << endl;
break;
case 8:
cout << "你认为该电影非常好" << endl;
break;
case 7:
cout << "你认为该电影非常好" << endl;
break;
case 6:
cout << "你认为该电影一般好" << endl;
break;
case 5:
cout << "你认为该电影一般好" << endl;
break;
default:
cout << "你认为这是烂片" << endl;
break;
} //ifswitch语句有什么区别?
//switch不可以判断区间的范围,表达式类型只能是整数或者字符型
//但是switch结构非常清晰,执行效率高
//通常情况下,switch执行效率比if高一些
return 0;
}
(二)循环结构 while循环
作用:满足循环条件,执行循环语句
练习代码如下:
#include
using namespace std;
int main()
{ //while循环,在写循环的时候一定要避免死循环
//在屏幕中打印0-9十个数字 int num = 0;
while (num < 10)
{cout << num << endl;
num++;
}
return 0;
}
案例练习:系统随机生成一个1-100之间的数字,玩家进行猜测,提示玩家数字过大或过小,如果猜对恭喜玩家胜利,并退出游戏
案例流程图如下:
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/64ec10ce89e244bbb7ff5758709bca5b.jpg)
文章图片
案例代码如下:
#include
#include
using namespace std;
int main()
{ //添加随机数种子,保证每次随机数不同
srand((unsigned int)time(NULL));
//rand()%100生成一个0-99的随机数 int num = rand() % 100 + 1;
cout << num << endl;
int val = 0;
//代表玩家输入的数据 while (1)
{cin >> val;
if (val > num)
{cout << "您猜测的值过大" << endl;
}
else if (val < num)
{cout << "您猜测的值过小" << endl;
}
else
{cout << "恭喜您猜测正确" << endl;
break;
}
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/81960c8ac30d4509940d89008acd1963.jpg)
文章图片
do…while循环
- 与while的区别在于do…while先执行一次循环语句,再判断循环条件
#include
using namespace std;
int main()
{ //给屏幕打印0-9
int num = 0;
do
{cout << num << endl;
num++;
} while (num < 10);
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/94427fd5f1d14dbc8836172d17c1c0eb.jpg)
文章图片
案例:水仙花数
水仙花数是指一个三位数,它的每个位上的数字的三次幂之和等于它本身,利用do…while语句,求出所有3位数中的水仙花数
案例代码如下:
#include
using namespace std;
int main()
{ //将所有的三位数输出100-999
//在所有的三位数中找到水仙花数 int num = 100;
do
{int a = 0;
//个位
int b = 0;
//十位
int c = 0;
//百位a = num % 10;
b = num / 10 % 10;
c = num / 100;
if (a * a * a + b * b * b + c * c * c == num)//判断是不是水仙花数
{cout << num << endl;
}
num++;
} while (num < 1000);
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/2180fb836d804ec1bfb286110cfae4b2.jpg)
文章图片
for循环
作用:满足循环条件,执行条件语句
练习代码如下:
#include
using namespace std;
int main()
{ //打印0-9
int i = 0;
for (i;
i < 10;
i++)
{cout << i << endl;
} for (int i = 0;
i < 10;
i++)
{cout << i << endl;
}
return 0;
}
练习案例:从1-100,如果数字个位含有7,或者数字十位含有7,或者数字是7的倍数,我们打印敲桌子,其余数字直接打印输出
案例代码如下:
#include
using namespace std;
int main()
{ for (int i = 1;
i <= 100;
i++)
{if (i % 10 == 7 || i / 10 % 10 == 7 || i % 7 == 0)
{cout << "敲桌子" << endl;
}
else
{cout << i << endl;
}
}
return 0;
}
嵌套循环
作用:在循环中再嵌套一层循环,通常用来解决一些实际性问题
练习代码如下:
#include
using namespace std;
int main()
{ //在屏幕上打印一个8*8的星号矩阵 for (int i = 0;
i < 8;
i++)
{for (int j = 0;
j < 8;
j++)
{cout << "*" << " ";
}
cout << endl;
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/210fee6095074081a77b91d92db27e3d.jpg)
文章图片
案例:乘法口诀表
案例代码如下:
#include
using namespace std;
int main()
{ //打印九九乘法表
for (int i = 1;
i <= 9;
i++)
{for (int j = 1;
j <= i;
j++)
{cout << j << "*" << i << "=" << i * j << " ";
}
cout << endl;
}
return 0;
}
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/c365999b14364a01964176a75c6afc17.jpg)
文章图片
三、跳转语句 (一)break语句 练习代码如下:
#include
using namespace std;
int main()
{ //出现在switch语句中
cout << "请选择副本难度" << endl;
cout << "1、普通" << endl;
cout << "2、中等" << endl;
cout << "3、困难" << endl;
int select = 0;
cin >> select;
switch (select)
{ case 1:
cout << "选择了普通难度" << endl;
break;
case 2:
cout << "选择了中等难度" << endl;
break;
case 3:
cout << "选择了困难难度" << endl;
break;
default:
break;
//这个break用来退出switch语句
} //出现在循环语句中
for (int i = 0;
i < 10;
i++)
{if (i == 5)
{break;
//此时break可以退出该循环
}
cout << i << endl;
} //出现在嵌套循环中
for (int i = 0;
i < 10;
i++)
{for (int j = 0;
j < 10;
j++)
{if (j == 5)
{break;
//退出内层循环(根据就近原则)
}
cout << "*" << " ";
}
cout << endl;
} return 0;
}
(二)continue语句 作用:在循环语句中,跳过本次循环中余下的尚未执行的语句,执行下一次循环
练习代码如下:
#include
using namespace std;
int main()
{ for (int i = 0;
i <= 100;
i++)
{//如果是奇数输出,偶数不输出
if (i % 2 == 0)
{continue;
//遇到continue,该次循环中该条语句以后的语句不执行,重新进行新一轮循环
}
cout << i << endl;
}
return 0;
}
(三)goto语句 作用:可以无条件跳转语句
练习代码如下:
#include
using namespace std;
int main()
{ cout << "1" << endl;
cout << "2" << endl;
cout << "3" << endl;
cout << "4" << endl;
cout << "5" << endl;
cout << "6" << endl;
//goto语句
cout << "1" << endl;
cout << "2" << endl;
goto FLAG;
cout << "3" << endl;
cout << "4" << endl;
cout << "5" << endl;
FLAG:
cout << "6" << endl;
return 0;
}
【#|【C++】学习笔记[二]】
![#|【C++】学习笔记[二]](https://img.it610.com/image/info8/82da2c0878644829a848befd0bb365fe.jpg)
文章图片
???
推荐阅读
- 宽容谁
- 我要做大厨
- 增长黑客的海盗法则
- 画画吗()
- 2019-02-13——今天谈梦想()
- 远去的风筝
- 三十年后的广场舞大爷
- 叙述作文
- 20190302|20190302 复盘翻盘
- 学无止境,人生还很长