Boolean Statements

Pages: 12

Missed the lecture on boolean statements, can someone help me out with this:


Write an if-else statement to set a boolean flag bIsPositive to true if a given integer x is positive and false otherwise.
http://www.cplusplus.com/doc/tutorial/control/

Hint: start with bool bIsPositive;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{
bool bIsPositive=false;

for (int x=-10;x<10;x++){
if (x>0) {bIsPositive=true;}        //  (X is greater than zero (positive)  set bIsPositive to true;)
else {bIsPositive=false;};           // else set to false
    cout << "x=  "<< x << "    bIsPositive is  " << bIsPositive << endl;
}
    return 0;
}
@pearlyman: Please do not submit solutions to homework problems.
@LB: I looked through that link, couldn't make any sense of it.
Did you look through it, or did you read it? There is actually a section on that page that explains exactly how to do what you are required to do.
@pearlyman: Thank you! You're coding will go along way to helping me figure out boolean statements as it relates to the question.

Also, in regards to LB's comments:

First, you have no way of determining this is a homework question. In fact, it's not. It's a question from my textbook that I'm trying (and failing) to learn from BECAUSE I MISSED THE LECTURE.

Second, looking at proper code and dissecting it is hands down the easiest way for me to learn.

Finally, no harm no foul.

@peralyman This bit of code:

1
2
3

for (int x=-10;x<10;x++)


What exactly is going on there? I've yet to come across a "for" statement.
Homework or not, we discourage posting entire solutions like this because seeing is not doing. We are here to help you learn how to write code, not help you learn how to request code be written for you. We would prefer you to learn rather than simply move on to the next question. You may have integrity, but not everyone does.

I'm not sure why the for loop was used in the posted code - it is not required by your assignment. If you want to learn more about it, it's further down on the page I already linked you to.
Last edited on
@LB I admit, I looked through it. However, the section you're referring to pertaining exactly to my question is in regards to a simple if-else statement:

1
2
3
4
5
6
7
8

if (x > 0)
  cout << "x is positive";
else if (x < 0)
  cout << "x is negative";
else
  cout << "x is 0";


Correct me if I'm wrong, but there's no use of boolean indicators in this code, which doesn't help me at all with the question.

Look, I could write a simple if-else statement like the above no problem. However, it's when you throwin the use of boolean that my understanding gets murky, hence me seeking help.
Last edited on
In fact, the page you linked me to doesn't have a single mention of the word "bool" or "boolean" anywhere.
Sorry, I misunderstood your original post - "boolean statements" are what you put inside the parenthesis of if-statements. I think you're actually talking about the bool data type?

The bool data type is a primitive type which can only hold the values true or false - these values are also the result of the expressions inside if statements.
1
2
3
4
5
6
7
8
9
10
11
12
bool hello = true;
hello = false;
hello = (x + 1 == 7);
if(hello)
{
    //...
}

if(!hello && x == 55)
{
    //...
}
If that doesn't clear things up, Boo Radley gives a more in-depth explanation below.
Last edited on
there's no use of boolean indicators in this code

Ah, but there is. x > 0, for example, is a boolean expression.

Surely you've seen code like this:
1
2
3
int x = 2;
int y = 3;
int z = x + y;


Compare it to code like this:
1
2
3
int x = 2;
int y = 3;
? z = x < y;


x + y makes sense... a sum of two ints returns an int. What does an expression like x < y return? It returns true or it returns false. This is what the boolean type is for.
1
2
3
int x = 2;
int y = 3;
bool z = x < y;


So a boolean variable is just something that holds true or false.

set a boolean flag bIsPositive to true if a given integer x is positive and false otherwise.

"Flag" is not some special word in C++, it just means "variable that indicates some sort of state".

If you already know how to write if-else statements, then using them to set the value of a boolean should be simple.

1
2
3
4
5
6
int b = 15;
bool bIsPositive;
if (b >= 0)
    bIsPositive = true;
else
    bIsPositive = false;


Once you see that bools can hold the values returned from boolean expressions (just like ints can hold the values returned from integer expressions), you can cut out the middleman of the if-else business altogether!

1
2
int b = 15;
bool bIsPositive = (b >= 0);
Thank you, booradley and thank you, LB, both of you have been great help.

Much appreciated!!
Here's the code I've written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>
using namespace std;

int x = -15;

bool bIsPositive;

int main() {

if (x >= 0)
	bIsPositive = true;
else
	bIsPositive = false;

cout << bIsPositive << endl;

system("PAUSE");
return 0;

}


This prints to the screen a 0 for "false" (if the int x were negative) and a 1 for "true" (if the int x were positive). I understand why that is. However, if I wanted it to actually print the words "true" or "false" instead of their corresponding integer, how would I go about doing that?
Last edited on
There is a stream manipulator for forcing boolean output as strings:
http://www.cplusplus.com/reference/ios/boolalpha/
http://en.cppreference.com/w/cpp/io/manip/boolalpha

By the way, lines 5 and 7 should be inside your main function - it's good practice.

Line 18: consider reading http://www.cplusplus.com/articles/j3wTURfi/ when you have time.
Last edited on
You know, I've seen variables set inside and outside the main function. I guess I just thought putting them outside cleaned things up a little bit. Out of pure curiosity, what exactly is the difference?
When you declare variables not inside any function, they are in the global scope - we call them global variables. Global variables can be accessed from anywhere in the program, meaning that you have no idea how they are used or where they are accessed from without looking through the entire program. Global variables are considered bad practice for this reason - they result in code that is hard to maintain.

Many people new to programming try to use global variables to avoid learning how to pass parameters to functions - it's a bad idea ;)
hmm... ok, interesting. Thanks, again!
In regards to line 18:

For some crazy reason, when I try to "start without debugging" (using Visual Studio 2010 and yes I have given it administrative privileges), I get "Access Denied" printed to the screen. My proff couldn't tell me exactly why that is, he only suggested that it may be because of the security software I'm running (ESET AV & COMODO Firewall). Anyways, when I "start with debugging", the program runs, and then the console closes itself, which can be very irritating. Thus, I include the system("PAUSE"); to stop the console from closing itself upon the program completing.

I'm open to any other suggestions/workarounds, as after reading the article you linked, it seems that it may come back to haunt me as I get deeper into this programming course.
Last edited on
Pages: 12