# Thread: What boolean operation will suit this need in C++?

1. anyway, my code is
Code:
```bool checker(void){
​         bool result=0;
for(int y=0;y<5;y++)
{result^=(box[y]!=pill&&box[y]!=pill);}
return result;
}```
While looping, i want 'result' to be, and only be 1 if all variables in the array box[] contain either pill or pill

i used ^= but it seems to not work.

all help deeply apprecieated    2.

3. I would code it as something like
Code:
```bool result=true;
for (int i = 0; i < N; i++) {
if (box[y] != pill) && (box[y] != pill) {
result = false;
// note: you could break out of the loop at this point to save time
}
}```
Have you learnt de Morgan's theorem? It can be useful when sorting out the logic for complicated conditions like this.

It is more intuitive, perhaps, to write the condition as
Code:
`! ( (box[y] == pill) || (box[y] == pill) )`
which is, by de Morgan's theorem, the same as above. (I hope!)  4. i actually have done what i needed, actually i was coding in a 2d array....
my current code that works its kinda long, i cant find a way to shorten it.
Code:
```bool drawfind (void){
bool allfilled=1;
for(int x=0;x<3;x++)
{for(int y=0;y<3;y++){
if(allfilled==1)
{allfilled=(box[x][y]==sym||box[x][y]==sym);}
}
}
return(allfilled&&!winfind());
}```
any way to shorten this long code?  5. Originally Posted by RamenNoodles any way to shorten this long code?
I would go for clarity rather than brevity. Every time. Much, much more important. (Wait until you have to go back and modify some code you wrote a year ago.) And it won't make any difference to the compiler; the output will probably be pretty much identical. You have to work quite hard to write code that a good compiler can't optimise.

In fact, it is so important, I'm going to repeat it: I would always go for clarity rather than brevity.  6. i dont get what you meant by "Brevity"
• brev·i·ty/ˈbrevitē/

Noun
 Concise and exact use of words in writing or speech.Shortness of time.
Synonyms
 briefness - conciseness - shortness - concision - short

i still dont get what it means   7. Make your code clear. Not short. If writing 3 lines instead of a single (complex) expression makes it easier to understand then that is fine.  8. Originally Posted by Strange In fact, it is so important, I'm going to repeat it: I would always go for clarity rather than brevity.
And if I could 'Like' your post twice, I would.  9. Hey, i have a problem about variable initialization.
what i want is, for example,
Code:
```void main (void){int
a=1,
b=2,
sum=a+b;
cout<<sum;
cin>>a>>b;
cout<<sum;}```
i want sum to be always a+b, instead of always typing sum=a+b before everytime i need to use sum
(by the way, a and b will keep changing, in the program i am doing)

will const sum=a+b; make sum be permanently at a+b at the moment it was defined, or keep changing whenever a or b changes?  10. Originally Posted by RamenNoodles i want sum to be always a+b, instead of always typing sum=a+b before everytime i need to use sum
Then you are using the wrong language. This would be trivial in a functional programming language, such as ML or Haskell. I don't think there is a way of doing this in C++. (Note: I am deliberately not mentioning macro definitions as they are evil and against the spirit of C++. But they would allow you to do what you want. At the cost of making your program less clear. Which Is A Very Bad Thing.)  11. what if i use...
Code:
` const int sum=a+b;`
1) will the value of sum be constanted of the sum of values a and b, with the values a and b only at the moment sum is defined?

2) or will the value of sum change whenever a or b changes?  12. 1.

Because const tells the compiler that the value of a variable will not be changed (and, therefore allows it to make various optimizations).  13. so will

const int sum = a + b;

does it mean that const will always be a + b,
or is the calculation a + b; done at the moment sum is initialized, and remains constant at that value throughout the program, no matter if a is changed, or b is changed?

when you said
1.
then can c++ support my request?
any class or any function in any libraries?  14. Originally Posted by RamenNoodles so will

const int sum = a + b;

does it mean that const will always be a + b,
or is the calculation a + b; done at the moment sum is initialized, and remains constant at that value throughout the program, no matter if a is changed, or b is changed?
The value of sum is fixed at the time it is defined and will not depend on any later changes to a or b. That is because C++ is a procedural language where the order and timing of statements is what defines the behaviour.

then can c++ support my request?
any class or any function in any libraries?
No. And it would be confusing if it could because someone looking at the code would expect the value of sum to reflect the last value assigned to it (and not to change because some other variable changes).

Perhaps you could explain why you want to do this. It may be that there is a better way of changing the same thing. For example, say you had a variable called area which you wanted to be automatically updated when you change the value of width or length, then one way of doing this would be to define a class which implements this behaviour.  Bookmarks
 Posting Permissions
 You may not post new threads You may not post replies You may not post attachments You may not edit your posts   BB code is On Smilies are On [IMG] code is On [VIDEO] code is On HTML code is Off Trackbacks are Off Pingbacks are Off Refbacks are On Terms of Use Agreement