在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第四篇博客。
本篇博客介绍了C++中的循环语句。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
循环语句
while循环
while语句的格式是: while(循环条件){循环语句}
首先判断是否满足循环条件,满足就执行一次循环语句,不满足就跳过循环语句。执行完循环语句后再次检验是否满足循环条件,满足就再执行循环语句,直至不满足条件时结束循环。
如果一直满足循环条件,会发生死循环,导致程序一直执行,要避免死循环。
#include<iostream>
using namespace std;
int main(void)
{
int num = 0;
while (num < 10) {
cout << num << endl;
num += 1;
}
return 0;
}
程序依次输出0-9。
猜数字案例
C++生成伪随机数用rand(),rand() % 100 + 1得到1-100的数。但是如果只用rand(),那么生成的数比较固定。如果想让数字更随机就需要不断改变随机数种子,比如利用系统时间。此时需要用srand(),用系统时间作为种子的方法是srand((unsigned int) time (NULL))。用time需要包含头文件ctime。
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int number = rand() % 100 + 1;
int value;
cout << "Please enter the number :";
cin >> value;
while (1) {
if (value > number) {
cout << "It is bigger!" << endl;
}
else if (value < number) {
cout << "It is smaller!" << endl;
}
else {
break;
}
cout << "Please enter again :";
cin >> value;
}
cout << "You are right!" << endl;
return 0;
}
这段代码生成随机数,并要求用户输入一个数进行猜测,并且要求用户一直输入,直至输入结果与随机数一致为止(break退出循环)
下面是一个运行示例
Please enter the number :50
It is smaller!
Please enter again :80
It is bigger!
Please enter again :66
You are right!
下面是一个运行示例
Please enter the number :30
It is bigger!
Please enter again :16
It is smaller!
Please enter again :22
It is smaller!
Please enter again :26
It is bigger!
Please enter again :24
You are right!
这段代码也是猜数,但是限制最多猜5次。
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int number = rand() % 100 + 1;
int value;
int time = 0;
cout << "Please enter the number :";
cin >> value;
while (1) {
if (value > number) {
cout << "It is bigger!" << endl;
}
else if (value < number) {
cout << "It is smaller!" << endl;
}
else {
cout << "You are right!" << endl;
break;
}
time += 1;
if (time >= 5) {
cout << "You have failed!" << endl;
cout << "The answer is " << number << endl;
break;
}
cout << "Please enter again :";
cin >> value;
}
return 0;
}
下面是一个运行示例
Please enter the number :80
It is bigger!
Please enter again :60
It is bigger!
Please enter again :20
It is smaller!
Please enter again :42
It is bigger!
Please enter again :33
It is smaller!
You have failed!
The answer is 41
do while循环
do while语句的格式是 do{循环语句}while(循环条件);
do while会先执行一次循环语句,再判断是否满足循环条件,不满足会结束循环,满足条件会继续执行循环,每次循环后比较是否满足,不满足条件后结束循环。
#include<iostream>
using namespace std;
int main()
{
int num = 0;
do {
cout << "num = " << num << endl;
num += 1;
} while (num < 10);
return 0;
}
这段代码依次输出num = 0至num = 9。
水仙花数
水仙花数是个三位数,并且个位数的三次方,十位数的三次方,百位数的三次方和等于它本身。这段代码求水仙花数。
#include<iostream>
using namespace std;
int main()
{
int num = 100;
do {
int a = num % 10;
int b = num / 10 % 10;
int c = num / 100;
if (a * a * a + b * b * b + c * c * c == num) {
cout << num << endl;
}
num += 1;
} while (num < 1000);
return 0;
}
程序输出153 370 371 407四个数,与水仙花数一致。
for循环
for循环的格式是:for(起始表达式;条件表达式;末尾循环体){循环语句}
首先执行起始表达式,然后进行条件表达式,如果满足条件就进入循环语句,不满足就执行循环后面的语句。进入循环语句后,完成循环语句就执行末尾循环体,然后判断是否满足条件,满足就重复执行循环语句和末尾循环语句,直至不满足退出为止。
#include<iostream>
using namespace std;
int main()
{
int num = 0;
for (num = 0; num < 10; num++) {
cout << "num = " << num << endl;
}
return 0;
}
这段代码输出num = 0至num = 9
敲桌子
从1开始,累增至100,如果数是7的倍数、个位数是7、或十位数是7,就输出敲桌子,其余情况下输出数字。
#include<iostream>
using namespace std;
int main()
{
int num = 1;
for (num = 1; num <= 100; num += 1) {
if (num % 7 == 0 || num % 10 == 7 || num / 10 == 7) {
cout << "Knock the table" << endl;
}
else {
cout << num << endl;
}
}
return 0;
}
当num值为7,14,17,21,27,28,35,37,42,47,49,56,57,63,67,70,71,72,73,74,75,76,77,78,79,84,87,91,97,98时输出Knock the table,其余条件下输出num的值。
嵌套循环
循环语句内可以再放一个循环。
#include<iostream>
using namespace std;
int main()
{
int i, j;
for (i = 0; i < 10; i += 1) {
for (j = 0; j < 10; j += 1) {
cout << "* ";
}
cout << endl;
}
return 0;
}
输出十行,每行十个*,中间用空格分隔。
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
乘法口诀表
这段代码输出乘法口诀表
#include<iostream>
using namespace std;
int main()
{
int i, j;
for (i = 1; i <= 9; i += 1) {
for (j = 1; j <= i; j += 1) {
cout << i << " * " << j << " = " << i * j << '\t';
}
cout << endl;
}
return 0;
}
输出结果为
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81