 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

April 21st, 2010, 04:34 PM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 18
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
Ch 5 P 163 - Using the ! operator
Hi Imar,
I'm having a little problem understanding the use of the ! operator in the following expression...
bool success = false;
while (!success)
{
success = SendEmailMessage();
}
My understanding is that the While loop is used to repeat a statement while a condition is true.
Therefore if the SendEmailMessage(); method fails it will return false so success will be false. When success is tested in the While loop it will be set to true and therefore will drop out of the loop.
Ahhh this is doing my head in! Could you step me through this please. I know that I am not grasping something that is fundamental.
Thank you
Bill
|
|

April 23rd, 2010, 06:13 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
Hello there,
Well the concept is very simple and sweet. Let me explain it.
Quote:
|
My understanding is that the While loop is used to repeat a statement while a condition is true.
|
Yes you are right. While loop continues until condition is false.
So this is the base for the while loop. What actually is going there is that until a message is sent successfully, the code keep trying to send the message again and again.
Quote:
bool success = false;
while (!success)
{
success = SendEmailMessage();
}
|
now as success is initialized with false (bool variable) so !suceess will become true and hence the while loop will execute for the first time and the function SendEmailMessage() is called.
You must know that a function returns the value where it was called from so SendEmailMessage() function will return the value true if it sends messages successfully otherwise false and will assign it to success variable in loop body.
If messages are sent successfully then it does not make sense to recall the loop so as now the success contains true, it will be inverted and will become false and hence loop will not execute any longer. And if messages sending fails then function will return false and hence success contains false which will be inverted again to true and loop starts executing again and will continue until it sends messages successfully.
Quote:
|
When success is tested in the While loop it will be set to true and therefore will drop out of the loop.
|
I think you know now where you were wrong to understand it.
Hope this helps.
|
|
The Following User Says Thank You to jack_hilary For This Useful Post:
|
|
|

April 23rd, 2010, 08:45 AM
|
|
Authorized User
|
|
Join Date: Apr 2010
Posts: 18
Thanks: 7
Thanked 0 Times in 0 Posts
|
|
Ch 5 P 163 - Using the ! operator
Many thanks for that Jack. I understand it perfectly now.
Regards Bill
|
|

April 23rd, 2010, 09:55 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
Hello Bill,
Glad to know it helped you. You may also use the following code.
Code:
bool success = true;
while (success)
{
success = !SendEmailMessage();
}
Both code (this one and the previous one) will work same. And hope this time you are familiar with this one and can easily understand what is going on behind scene.
Cheers,
Jack
|
|

April 23rd, 2010, 11:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
Both code (this one and the previous one) will work same.
|
But the second one is very confusing to read. Don't use in production code please....
Imar
|
|

April 23rd, 2010, 11:56 AM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
Quote:
|
But the second one is very confusing to read. Don't use in production code please....
|
what makes it confusing and why not to use in production. Please let me know the reason behind scene.
Regards,
Jack
|
|

April 23rd, 2010, 12:02 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Success implies you're done.
Code:
bool success = true;
while (success)
So while we're having success, we sending a message? And not being able to send a message is a success? And then when we can send the message we call it a failure and exit the loop?
Pretty confusing if you ask me.
Imar
|
|

April 23rd, 2010, 12:12 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
OK I see.
The way we think it may be really confusing. For the first time we read the code it may completely confuse us that and reason being is really straight forward that you have explained. Really thanks for that.
But the code I wrote just because it is programeticlly correct and implements the same functionality.
Any ways thank you again.
Regards,
Jack
|
|

April 23rd, 2010, 12:31 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I know. But writing correct code is not enough. Writing correct and understandable code is what makes you a better prgrammer.
Imar
|
|

April 23rd, 2010, 12:34 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
|
|
Quote:
|
Writing correct and understandable code is what makes you a better prgrammer.
|
I do agree with you. Many thanks for that.
Regards,
Jack
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| how to use like operator |
thillaiarasu |
SQL Server 2000 |
4 |
April 23rd, 2010 09:47 AM |
| Search ch 13, ch 16 |
sporik |
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 |
0 |
October 27th, 2009 04:44 PM |
| === operator in c# |
surendraparashar |
C# 2005 |
8 |
November 8th, 2007 05:14 AM |
| Invalid operator for data type. Operator equals di |
Pusstiu |
SQL Server 2000 |
2 |
August 10th, 2007 04:51 AM |
| Ch. 4 & Ch. 12 |
athena |
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 |
0 |
July 23rd, 2004 10:54 AM |
|
 |