C/C++ C++ Boolean variable - clarity sought

  • Thread starter Thread starter Lord Anoobis
  • Start date Start date
  • Tags Tags
    C++ Variable
AI Thread Summary
The discussion focuses on understanding the use of the Boolean variable `factorFound` in a C++ program that checks if a number is prime. It is initialized as false and changes to true when a divisor is found, which ends the while loop. The condition `!factorFound` in the loop ensures that the loop continues only if no factors have been found. After the loop, the program checks if `x` equals `y` to determine if the number is prime. There is also a suggestion that the program could be optimized by using `factorFound` directly in the output logic.
Lord Anoobis
Messages
131
Reaction score
22
This is an example from a study guide I'm using at the moment in learning C++.
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.
 
Technology news on Phys.org
Yes, the variable factorFound is a Boolean value and it is initialised as false.
It only changes state to true, when within the loop, your input value is found to be divisible by an integer (x), which is your loop counter.
When this happens this will cause the loop to end. with a value of x which is less then y.
After the loop finishes, it tests if x is equal to y, and if it is this means the loop continued to completion without any divisible integer being found,
hence your input was a prime number.
 
Lord Anoobis said:
This is an example from a study guide I'm using at the moment in learning C++.
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.
On Visual Studio 2013, a bool variable is 1 byte. The MSDN documentation says that the size of this type is unspecified, but using the sizeof operator on a variable of type bool evaluated to 1.

Uninitialized, its value is 0xCC. When set to false, its value is 0x00, and when set to true, its value is 0x01.
 
rootone said:
Yes, the variable factorFound is a Boolean value and it is initialised as false.
It only changes state to true, when within the loop, your input value is found to be divisible by an integer (x), which is your loop counter.
When this happens this will cause the loop to end. with a value of x which is less then y.
After the loop finishes, it tests if x is equal to y, and if it is this means the loop continued to completion without any divisible integer being found,
hence your input was a prime number.
So basically, the loop condition becomes false when factorFound becomes true within the loop?
 
Yes.
 
Mark44 said:
On Visual Studio 2013, a bool variable is 1 byte. The MSDN documentation says that the size of this type is unspecified, but using the sizeof operator on a variable of type bool evaluated to 1.

Uninitialized, its value is 0xCC. When set to false, its value is 0x00, and when set to true, its value is 0x01.
What you have said seems to be a bit beyond my current capabilities. I'll have to look into it.
 
rootone said:
Yes.
Got it. That was causing a bit of a headache. Thanks.
 
Lord Anoobis said:
How does !factorFound affect things?

The ! ("not") operator reverses a boolean value. If factorFound is true, then !factorFound is false. If factorFound is false, then !factorFound is true.
 
Lord Anoobis said:
while (x != y && !factorFound)
{
if (y % x ==0)
factorFound = true;
x++;
}
if ( x == y)
I do not like this part of the program.
  1. Since factorFound is true whenever a divisor is found, I do not see why you do not use it in the printing routine (if (factorFound) /* not prime */... )
  2. You are doing a lot of unnecessary comparisons, since no factor can be greater than the square root of y.
 
Back
Top