# Question about the examples from the book

Page 1 of 1

## 4 Replies - 948 Views - Last Post: 03 September 2009 - 08:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=123630&amp;s=69d59762c1985f7d20e68f8ecf1e672f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

# Question about the examples from the book

Posted 02 September 2009 - 06:42 PM

Well...I have a question about the implication of logical operation.

Here is what it is

If P is true, Q is true, P implies Q = true
If P is true, Q is false, P implies Q = false
If P is False, Q is false, P implies Q = true
If P is False, Q is true, P implies Q = true.

Q: What are these really about? What is implication of logical operation, it isn't mentioned in the book itself.

Implication operation can be constructed ! and l, mentioned by the book. !p l q

Q: What does this really do?

Here is the code

```using system;

class implication {
static void main () {
bool p = false; q = false;
int i, j;

for(i = 0; i <2; i++) {
for(j = 0; j < 2; j++){
if(i == 0) p = true;
if(i == 1) p = false;
if(j == 0) p = true;
if(j == 1) p = false;

Console.WriteLine("p is " + p + ", q is " q);
if(!p l q) Console.WriteLine(p + " implies " + q + " is " + true);
Console.WriteLine();
}
}
}
}

Here are the outputs

p is true, q is true
true implies true is true

p is true, q is false

p is false, q is true
false implies true is true

p is false, q is false
false implies false is true
```

Q: how does this program work?
Q: Do we really have to initialize bool p and q to false when declared? Because they are reinitialized when they work within the block of code of the for loop. To me they are unnecessary.
Q: How does this block of code work?
```for(i = 0; i <2; i++) {
for(j = 0; j < 2; j++){
if(i == 0) p = true;
if(i == 1) p = false;
if(j == 0) p = true;
if(j == 1) p = false;
```

Is This A Good Question/Topic? 0

## Replies To: Question about the examples from the book

### #2 SixOfEleven

• Planeswalker

Reputation: 1055
• Posts: 6,643
• Joined: 18-October 08

## Re: Question about the examples from the book

Posted 02 September 2009 - 07:18 PM

kenryuakuma, on 2 Sep, 2009 - 07:42 PM, said:

Well...I have a question about the implication of logical operation.

Here is what it is

If P is true, Q is true, P implies Q = true
If P is true, Q is false, P implies Q = false
If P is False, Q is false, P implies Q = true
If P is False, Q is true, P implies Q = true.

Q: What are these really about? What is implication of logical operation, it isn't mentioned in the book itself.

Implication operation can be constructed ! and l, mentioned by the book. !p l q

Q: What does this really do?

This is an example of Boolean logic. Boolean logic is used for testing conditions, an important part of if statements and loops. !p means that you want to reverse of p. If p's value is true the reverse will be false. If p's value is true the reverse will be false. The other operator | is a logical OR operator. What that means is if one part is true, the other part is true or both are true the result will be true.

Quote

Here is the code

```using system;

class implication {
static void main () {
bool p = false; q = false;
int i, j;

for(i = 0; i <2; i++) {
for(j = 0; j < 2; j++){
if(i == 0) p = true;
if(i == 1) p = false;
if(j == 0) p = true;
if(j == 1) p = false;

Console.WriteLine("p is " + p + ", q is " q);
if(!p l q) Console.WriteLine(p + " implies " + q + " is " + true);
Console.WriteLine();
}
}
}
}

Here are the outputs

p is true, q is true
true implies true is true

p is true, q is false

p is false, q is true
false implies true is true

p is false, q is false
false implies false is true
```

Q: how does this program work?

Boolean values have two values true or false. The nested loop loops between all of the value of the above logic statement. In the first iteration p's value is true and q's value is true. The if part gives the value of statement !p | q, which is true. It then loops through all of the other possible Boolean values.

Quote

Q: Do we really have to initialize bool p and q to false when declared? Because they are reinitialized when they work within the block of code of the for loop. To me they are unnecessary.

It is usually good programming practice to initialize variables before using them.

### #3 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

## Re: Question about the examples from the book

Posted 02 September 2009 - 11:05 PM

SixOfEleven, thanks a lot for being the first one to answer my question. Your name is amusing! Anyway, putting this aside, I still wanna ask to verify something so no initialization of the bool p and q is still ok? Will that pose any problem?

The nested loop is kind of like real hard for beginner and I am still having a hard time understanding them.

### #4 Jay93

Reputation: 7
• Posts: 93
• Joined: 04-August 09

## Re: Question about the examples from the book

Posted 03 September 2009 - 01:13 AM

You could get away with not initializing p and q, however, as SixofEleven said this is not good practice and it can cause unexpected results.

### #5 SixOfEleven

• Planeswalker

Reputation: 1055
• Posts: 6,643
• Joined: 18-October 08

## Re: Question about the examples from the book

Posted 03 September 2009 - 08:45 AM

In this case you could get away with it. There are cases that you will have to assign them though so it would be a good idea to always do it. (I know I'm guilty of not doing it all the time, bad programmer.) Nested loops can take a bit to understand. I would suggest using the debugger and stepping through each line of code, by pressing F11 to see how the program runs.