d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Boolean Logic > Problem
Add Reply New Topic New Poll
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Sep 30 2016 02:27am
hi!
it's rather a basic or silly request, but I don't really understand boolean logic.

I know that it's something like 'true/false', but I can't imagine an example of using it, or when can it help me.
any suggestions? :(
Member
Posts: 20,223
Joined: Apr 30 2008
Gold: 5,169.82
Sep 30 2016 06:47am
An int can be a number: 0, 1, 2, etc
A string can be text: "a", "abc", "Leevee", "d2jsp", etc
A boolean has two states: "true" or "false".

You use a boolean to contain the outcome of a logical expression, and then use it later.

Code
string name = "pzold";

bool is_me = (name == "Leevee); // this will now contain the value "false"

if (is_me) {
MessageBox.Show("Hello, my name is Leeveel"); // this will not be executed, as the variable is_me is false.
}


This post was edited by Leevee on Sep 30 2016 06:47am
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Sep 30 2016 10:09am
Quote (Leevee @ Sep 30 2016 01:47pm)
An int can be a number: 0, 1, 2, etc
A string can be text: "a", "abc", "Leevee", "d2jsp", etc
A boolean has two states: "true" or "false".

You use a boolean to contain the outcome of a logical expression, and then use it later.

Code
string name = "pzold";

bool is_me = (name == "Leevee); // this will now contain the value "false"

if (is_me) {
MessageBox.Show("Hello, my name is Leeveel"); // this will not be executed, as the variable is_me is false.
}


Thanks for such a precise explanation!

But there's still this one thing; I could write this code without using any bool.

Code
string name = "pzold";
/.../
if (name!="pzold") { /.../ }


So when could bool be really useful?

This post was edited by pzold on Sep 30 2016 10:10am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 30 2016 06:39pm
Quote (pzold @ Sep 30 2016 12:09pm)
Thanks for such a precise explanation!

But there's still this one thing; I could write this code without using any bool.

Code
string name = "pzold";
/.../
if (name!="pzold") { /.../ }


So when could bool be really useful?


Now what if you needed that boolean value in multiple places within the scope of your function? You would be at a performance loss if you tried to compute, say if two strings match, many times throughout your function. So instead store the result of the computation once.
Member
Posts: 8,992
Joined: Mar 24 2013
Gold: 18,115.00
Oct 1 2016 08:44am
Quote (AbDuCt @ Oct 1 2016 01:39am)
Now what if you needed that boolean value in multiple places within the scope of your function? You would be at a performance loss if you tried to compute, say if two strings match, many times throughout your function. So instead store the result of the computation once.


Well, I haven't done any big project yet. But for sure it seems more clear now. Thanks :thumbsup:
Member
Posts: 7,324
Joined: Dec 22 2002
Gold: 1,261.00
Oct 3 2016 10:14am
Another somewhat common use for bools is flags. Something like

Code
bool needsSorting = false;
for (Object myObj in myObjects) {
//do something;
if (myObj.isOutOfOrder())
needsSorting = true;
}

if (needsSorting)
myObjects.sort();


Or some other situation where you don't want to interrupt the flow of the application right now, but you do want to make note of some condition and act on it later.

Bools are also common as just object fields for storing any kind of binary information. Things like whether an account is currently active, whether a student is registered, whether a product is in stock, etc. Anything where the answer is just 'yes' or 'no'.
Member
Posts: 15,717
Joined: Aug 20 2007
Gold: 481.00
Oct 3 2016 10:54am
i use bool alot for class properties

class JspMember
int ID
string Name
bool DFAccess
Member
Posts: 11,288
Joined: Jan 20 2007
Gold: 20.69
Oct 4 2016 02:28am
First of all, there is a problem with your question. Boolean logic is an algebra where the values are true or false. Whenever you write a condition (if) or conditional loop (for / while), you make use of this logic. Often you want two conditions to hold at the same time or only one of them, etc. It doesn't matter if you use a bool type or not. You can also express the same logic as ints and whatnot. So if you say "I could write that without using bool", that doesn't mean that your reasoning doesn't involve boolean logic. Having bool types is never necessary but helps prevent confusions. basically the same arguments that go for and against a typesystem apply here, too.

Apart form that, it is really useful to understand boolean logic. It helps to:

1) read (and also write, but please don't) rather unintuitive code, e.g.

Code
status = noError || fixIt()


i.e. the first part gets evaluated and if noError is true, you known the disjunction has to be true as well. thus, fixit() will not run. If there is an error, though, fixIt() runs and its return value determines the value that will be stored in status.

2) bitwise logic: AND/OR/XOR/NOT on a bit level are crucial to understand if you do low level, space-efficient stuff like compressions.

3) boolean logic is "the easy version" of first (and higher) order logic. These have various uses as well: They are the foundation of static type checkers used in compilers, programming languages (e.g. prolog) that work entirely in first order logic exist, there are classification methods, i.e. a certian form of machine learning, that rely on it (FOIL, etc), and so on, and so on

tl;dr:
you NEED to understand boolean logic, especially the very basic stuff. this hasn't much to do with having bool types or not. it's also the foundation for more complicated (but also useful) stuff
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 7 2016 03:46pm
Quote (Kasiir @ Oct 4 2016 04:28am)

2) bitwise logic: AND/OR/XOR/NOT on a bit level are crucial to understand if you do low level, space-efficient stuff like compressions.


Every time someone types this I get a chuckle.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll