•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,829 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 155 | Replies: 2 | Solved
![]() |
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
I have some questions about the logical not operator in C++ and Visual C++. I have posted another question relating to the logical not operator. I just can't seem to wrap my head around the uses of this operator. From what I have read so far the logical not operator can switch the value of bool variable and this in particular I don't seem to understand. Consider the code below.
The part I don't understand is the line
if(minimum == 0 && !used[i])
{
.....
}
the !used[i] is "notting" an array value that are all initially set to false so the !used[i] should set it to true or rather this line is saying something like do this if minimum is equal to zero and element i is true or flagged as used in the used array. But this doesn't make sense because if that is true then this condition will never evaluate to true because the array has all false values initially. But the code works and does what it is supposed to which, is to sort the unsorted random array. So obviously I am missing a fundamental part of the logical not operator. I would assume that I am inferring the opposite meaning of that particular line because if I read it as false instead of true the code seems to work from logical standpoint. So... What does this !used[i] mean if used[] is an array of all bool values all set to false by the clear function????
// Soln4_4.cpp : main project file.
#include "stdafx.h"
using namespace System;
// This uses an array of bool values to record which data values
// have been output at any given time. A value that has been output
// is marked as true in the used array, and only values that have a
// corresponding true value in the used array are checked each time.
int main(array<System::String ^> ^args)
{
Random^ generator = gcnew Random;
array<int>^ values = gcnew array<int>(generator->Next(10,20));
// Initialize the array of data value
for (int i = 0 ; i < values->Length ; i++)
values[i] = generator->Next(100,1000);
// Create array to identify elements that have been output
array<bool>^ used = gcnew array<bool>(values->Length);
Array::Clear(used, 0, used->Length); // Set elements false
Console::WriteLine(L"There are {0} values.", values->Length);
int minimum = 0; // Current minimum
int minIndex = 0; // Index of current minimum
// Output the values in ascending sequence
for(int count = 0 ; count<values->Length ; count++)
{
// Find first candidate for minimum
for(int i = 0 ; i<values->Length ; i++)
if(minimum == 0 && !used[i])
{
minimum = values[i];
minIndex = i;
break;
}
// Look for a lower minimum from remaining elements
for(int i = minIndex+1 ; i<values->Length ; i++)
if(minimum>values[i] && !used[i])
{
minimum = values[i];
minIndex = i;
}
used[minIndex] = true; // Record minimum as used
Console::Write(L"{0,10}", minimum); // Write the minimum
if((count+1)%5 == 0) // If it's multiple of 5
Console::WriteLine(); // Write a newline
minimum = 0; // Reset minimum to 0
}
Console::WriteLine();
return 0;
}The part I don't understand is the line
if(minimum == 0 && !used[i])
{
.....
}
the !used[i] is "notting" an array value that are all initially set to false so the !used[i] should set it to true or rather this line is saying something like do this if minimum is equal to zero and element i is true or flagged as used in the used array. But this doesn't make sense because if that is true then this condition will never evaluate to true because the array has all false values initially. But the code works and does what it is supposed to which, is to sort the unsorted random array. So obviously I am missing a fundamental part of the logical not operator. I would assume that I am inferring the opposite meaning of that particular line because if I read it as false instead of true the code seems to work from logical standpoint. So... What does this !used[i] mean if used[] is an array of all bool values all set to false by the clear function????
>So obviously I am missing a fundamental part of the logical not operator.
Let's break it down, starting with the if statement:
If the condition is true, execute the block. Simple, right? Now let's add a boolean test:
If the result of x < y is true, execute the block. Or in more typical terms, if x < y, execute the block. Still simple, no? What about if we put the result of that boolean test in a boolean variable?
If the condition evaluates to true, execute the block. The condition is an expression containing only a boolean variable whose value is the result of the test x < y. The current value of the variable acts as the condition. If the variable is true (ie. x < y), the block will be executed, otherwise it won't. Still simple, right? Now let's start negating:
If the condition evaluates to true, execute the block. The condition is still an expression containing only a boolean variable whose value is the result of the test x < y. But now we've negated that value, so if x < y, the block won't be executed because x < y is true, and negated it becomes false. In other words, the block will only be executed if the value of the condition is false before being negated. It's easier to see by returning to the basic expression:
It all comes down to the values. Even a variable by itself is an expression, and that expression has a value. Put the values together and you can figure out even the most convoluted of expressions.
There's a pattern you'll see often when it comes to relational operators. Negating the result of < gives you >= and negating >= gives you <. So the above if statement could be written identically as:
All relational expressions can be reduced to either true or false in this way. Of course, once you get into the logical ANDs and ORs, short circuiting can throw you off, but that's a lesson for another day.
Let's break it down, starting with the if statement:
cplusplus Syntax (Toggle Plain Text)
if ( ... ) { ... }
cplusplus Syntax (Toggle Plain Text)
if ( x < y ) { ... }
cplusplus Syntax (Toggle Plain Text)
bool test = x < y; if ( test ) { ... }
cplusplus Syntax (Toggle Plain Text)
bool test = x < y; if ( !test ) { ... }
cplusplus Syntax (Toggle Plain Text)
if ( !( x < y ) { ... }
There's a pattern you'll see often when it comes to relational operators. Negating the result of < gives you >= and negating >= gives you <. So the above if statement could be written identically as:
cplusplus Syntax (Toggle Plain Text)
if ( x >= y ) { ... }
Member of: Beautiful Code Club.
•
•
Join Date: Jun 2008
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 0
God, this is why there are forums. I spent a quite a few minutes last night, trying to hash out what you explained there. Why couldn't the book have given me those three examples when explaining the logical not operator? That was the most clear-cut and precise answer to my question that I was able to find anywhere on the net. Thank you! I now, perfectly understand how to use the operator. Now I can add that to my arsenal and use if with confidence.
Last edited by Vallnerik25 : 30 Days Ago at 5:24 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- define operator ovrloading and its types (C++)
- How to use Logical OR operator in C- Shell (Shell Scripting)
- Logical operations and data-checks (Java)
- Python Logical "OR": (Python)
- Logical Operator help (Java)
- Need help writing a program (C++)
Other Threads in the C++ Forum
- Previous Thread: linker error
- Next Thread: I know Java, but GUI programming in C++?



Linear Mode