前两天有人问if和while的区别,当时只说了while用于循环语句,而if用于判断和分支语句。 我们看两段程序
#include<iostream>
using namespace std;
int main()
{
int b = 6;
int c = 0;
while (b != 0){
c++;
}
cout << c;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int b = 6;
int c = 0;
if(b != 0){
c++;
}
cout << c;
return 0;
}
第一个会陷入死循环中,第二个会输出c为1。也就是if不带循环的性质!