C++ Boolean variable - clarity sought

  • Context: C/C++ 
  • Thread starter Thread starter Lord Anoobis
  • Start date Start date
  • Tags Tags
    C++ Variable
Click For Summary

Discussion Overview

The discussion revolves around the use of a Boolean variable in a C++ program designed to determine whether a number is prime. Participants seek clarity on the initialization and functionality of the Boolean variable `factorFound`, particularly its role in the loop condition and overall program logic.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants confirm that `factorFound` is a Boolean variable initialized to false, which changes to true when a divisor of the input number is found.
  • Others explain that the loop continues until either a divisor is found or the loop counter reaches the input number.
  • A participant notes that the `!` operator reverses the Boolean value, affecting the loop condition.
  • One participant expresses confusion about the program's structure and suggests that using `factorFound` directly in the output logic could simplify the code.
  • Another participant provides details about the memory size of a Boolean variable in Visual Studio 2013, including its uninitialized state and specific values for true and false.

Areas of Agreement / Disagreement

Participants generally agree on the role of `factorFound` as a Boolean variable and its impact on the loop condition. However, there is a disagreement regarding the efficiency of the program's structure and whether the current implementation is optimal.

Contextual Notes

Some participants mention limitations in their understanding of the program's logic and the implications of using `factorFound` in different parts of the code. There are also references to specific behaviors of the Boolean type in Visual Studio that may not apply universally across different compilers.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
10K