Cpp class exercise 3:修订间差异
外观
| 第244行: | 第244行: | ||
cout << "退出主程序!" <<endl; | cout << "退出主程序!" <<endl; | ||
return 0; | |||
} | |||
</syntaxhighlight> | |||
=== continue 语句 === | |||
【例3.5】2层for循环,查找1-100内的质数 | |||
<syntaxhighlight lang="C++" line> | |||
#include <iostream> | |||
using namespace std; | |||
int main() | |||
{ | |||
int n =0; | |||
int count=0;// (内层)循环计数 | |||
cout << "查找1-100内的质数" <<endl; | |||
for(int n=2;n<=100;n++) | |||
{ | |||
int flag = 1; //1: 是质数 0:不是质数 | |||
for(int i=2;i<n;i++) | |||
{ | |||
count++; | |||
if(n % i == 0) | |||
{ | |||
flag = 0; | |||
break; | |||
} | |||
} | |||
if(flag == 0) | |||
continue; | |||
cout << n << "是质数" <<endl; | |||
} | |||
cout << "运行了" << count << "次判断" <<endl; | |||
return 0; | return 0; | ||
} | } | ||