system("pause")可以暂停一下界面,便于观察执行结果;
system("cls")功能是可以清屏;
break:
break可以用于while()、for()、do...while(),switch()
跳出while循环,当index = 0就结束循环
srand((unsigned int)time(NULL));
while (true)
{
int index = rand() % 10;
if (index == 0)
{
cout << "退出循环" << '\n';
break;
}
};
跳出switch语句:
int op;
cin >> op;
switch (op)
{
case 1:
break;
default:
break;
}
continue语句:
continue不能直接退出循环,而是只结束本次循环体的执行。
for循环5次,但当i = 3 时,就结束本次循环体的执行,所以不执行下面的代码了;
for(int i = 0; i < 5; i++)
{
if (i == 3)
{
cout << "不执行" << '\n';
continue;
}
cout << "你好" << " ";
}
随机函数rand():
头文件#include<ctime>
随机数种子 :srand((unsigned int)time(NULL))
比如:想要得到一个随机数在60 ~ 100 的数
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
srand((unsigned int)time(NULL));
int index = rand() % 40 + 60;
cout << index << '\n';
return 0;
}