#include <iostream>
usingnamespace 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: 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.
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.
@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";
elseif (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.
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.
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!
#include <iostream>
usingnamespace 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?
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 ;)
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.