C++ Boolean variable - clarity sought

  • C/C++
  • Thread starter Lord Anoobis
  • Start date
  • Tags
    C++ Variable
In summary, this code checks for prime numbers by dividing y by x. If the result is not a prime number, then factorFound is set to true. If the result is a prime number, then factorFound is false.
  • #1
Lord Anoobis
131
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
  • #2
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.
 
  • #3
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.
 
  • #4
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?
 
  • #5
Yes.
 
  • #6
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.
 
  • #7
rootone said:
Yes.
Got it. That was causing a bit of a headache. Thanks.
 
  • #8
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.
 
  • #9
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.
 

Related to C++ Boolean variable - clarity sought

1. What is a Boolean variable in C++?

A Boolean variable in C++ is a data type that can hold two values: true or false. It is used to represent logical values in programming and is often used in conditional statements and loops.

2. How do you declare a Boolean variable in C++?

To declare a Boolean variable in C++, you use the keyword "bool" followed by the variable name and an optional initial value. For example:
bool isSunny = true;
This declares a Boolean variable named "isSunny" and initializes it with a value of true.

3. How do you change the value of a Boolean variable in C++?

To change the value of a Boolean variable in C++, you use the assignment operator (=) followed by the new value. For example:
isSunny = false;
This will change the value of the "isSunny" variable from true to false.

4. Can you use a Boolean variable in an if statement in C++?

Yes, you can use a Boolean variable in an if statement in C++. The condition for the if statement must be a Boolean expression, which means it must evaluate to either true or false. For example:
if (isSunny) {
//code to execute if isSunny is true
}

5. What happens if you try to assign a non-Boolean value to a Boolean variable in C++?

If you try to assign a non-Boolean value to a Boolean variable in C++, the compiler will throw an error. Boolean variables can only hold the values of true or false, so assigning any other type of value will result in a type mismatch error.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
779
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
4K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
803
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top