Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 21st, 2010, 04:34 PM
Authorized User
 
Join Date: Apr 2010
Posts: 18
Thanks: 7
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old April 23rd, 2010, 06:13 AM
Friend of Wrox
Points: 752, Level: 10
Points: 752, Level: 10 Points: 752, Level: 10 Points: 752, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

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.
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
Reply With Quote
The Following User Says Thank You to jack_hilary For This Useful Post:
flapjack (April 23rd, 2010)
  #3 (permalink)  
Old April 23rd, 2010, 08:45 AM
Authorized User
 
Join Date: Apr 2010
Posts: 18
Thanks: 7
Thanked 0 Times in 0 Posts
Default Ch 5 P 163 - Using the ! operator

Many thanks for that Jack. I understand it perfectly now.

Regards Bill
Reply With Quote
  #4 (permalink)  
Old April 23rd, 2010, 09:55 AM
Friend of Wrox
Points: 752, Level: 10
Points: 752, Level: 10 Points: 752, Level: 10 Points: 752, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

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
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
Reply With Quote
  #5 (permalink)  
Old April 23rd, 2010, 11:46 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #6 (permalink)  
Old April 23rd, 2010, 11:56 AM
Friend of Wrox
Points: 752, Level: 10
Points: 752, Level: 10 Points: 752, Level: 10 Points: 752, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

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
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
Reply With Quote
  #7 (permalink)  
Old April 23rd, 2010, 12:02 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #8 (permalink)  
Old April 23rd, 2010, 12:12 PM
Friend of Wrox
Points: 752, Level: 10
Points: 752, Level: 10 Points: 752, Level: 10 Points: 752, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Default

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
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
Reply With Quote
  #9 (permalink)  
Old April 23rd, 2010, 12:31 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I know. But writing correct code is not enough. Writing correct and understandable code is what makes you a better prgrammer.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #10 (permalink)  
Old April 23rd, 2010, 12:34 PM
Friend of Wrox
Points: 752, Level: 10
Points: 752, Level: 10 Points: 752, Level: 10 Points: 752, Level: 10
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 165
Thanks: 5
Thanked 6 Times in 6 Posts
Arrow

Quote:
Writing correct and understandable code is what makes you a better prgrammer.
I do agree with you. Many thanks for that.

Regards,

Jack
__________________
Jack: Founder, Developer & Owner Of JackAndGenieForever.Com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:28 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.