- #1
Lord Anoobis
- 131
- 22
This is an example from a study guide I'm using at the moment in learning C++.
I hope this doesn't end up looking awful. Anyway, the book is rather vague about happening here. Does the variable factorFound represent a Boolean false when initialised? Also, I don't see how it works in the condition in the while loop. How does !factorFound affect things? I understand how the program works as a whole, but these details elude me. In the dark and on need of light to be shed on this.
Code:
//Test whether a number is prime
#include <iostream>
using namespace std;
int main ()
{
int x, y;
bool factorFound = false;
cout << "Enter a positive integer: ";
cin >> y;
x = 2;
while (x != y && !factorFound)
{
if (y % x ==0)
factorFound = true;
x++;
}
if ( x == y)
cout << y << " is prime." << endl;
else
cout << y << " is not prime." << endl;
return 0;
}
I hope this doesn't end up looking awful. Anyway, the book is rather vague about happening here. Does the variable factorFound represent a Boolean false when initialised? Also, I don't see how it works in the condition in the while loop. How does !factorFound affect things? I understand how the program works as a whole, but these details elude me. In the dark and on need of light to be shed on this.