Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 13th, 2011, 01:46 AM
Registered User
Join Date: May 2011
Location: in the Matrix
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question Chapter 4 - Summary Excercises

Hey all!

Got a quick question and a sneeky neenjuh feeling that I already have the answer.

For the excercise:

2. Write an application that includes the logic from Exercise 1, obtains two numbers from the user, and displays them, but rejects any input where both numbers are greater than 10 and asks for two new numbers.
Now, I did this. But when I compared my code to the Appendix code - I was all .... yea, I thunk I didded it wrong... Funny part? My code works.

So, answering my own question, there are different ways to code - just some is more efficient?

Their Code (example 2):

bool numbersOK = false;
double var1, var2;
var1 = 0;
var2 = 0;
while (!numbersOK)
Console.WriteLine("Give me a number:");
var1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Give me another number:");
var2 = Convert.ToDouble(Console.ReadLine());
if ((var1 > 10) && (var2 > 10))
Console.WriteLine("Only one number may be greater than 10.");
numbersOK = true;
Console.WriteLine("You entered {0} and {1}.", var1, var2);
My Code:

double firstNum, secNum;
            Console.WriteLine("This program decides which one of 2 numbers is greater than 10.");
            Console.WriteLine("What is your first number?");
            firstNum = Convert.ToDouble(Console.ReadLine()); //defines firstNum
            Console.WriteLine("What is your second number?");
            secNum = Convert.ToDouble(Console.ReadLine()); //defines secNum
            if ((firstNum > 10) && (secNum > 10))  //evaluates both numbers to be greater than
                Console.WriteLine("Both of your numbers are greater than 10.  Please try again.");
            else if ((firstNum > 10) && (secNum < 10))  //evaluates firstNum to be greater
                Console.WriteLine("Your first number {0} is greater than 10 and your second number {1} is less than 10.", firstNum, secNum);
            else if ((firstNum < 10) && (secNum > 10))  // evaluates secNum to be greater
                Console.WriteLine("Your first number {0} is less than 10 and your second number {1} is greater.", firstNum, secNum);
            else  // just in case both numbers are less than 10
                Console.WriteLine("Both of your numbers are less than 10!");
            Console.WriteLine("Press any key to quit");
Obviously I could've used bool and some other techniques now seeing their code. I guess the shortcuts come with time? Or, did I miss an important lesson while taking notes?
  #2 (permalink)  
Old May 23rd, 2011, 07:11 AM
Registered User
Join Date: Apr 2011
Posts: 7
Thanks: 3
Thanked 0 Times in 0 Posts


There are always more than one way to write code that will perform a task. Efficiency/brevity comes with looking at how more experienced coders achieve the same sort of outcome and using their techniques in your code.

In general, if you find yourself using a bunch of sequential "if else" statements you should be using a loop and/or a switch statement instead.

From a functional viewpoint, the main difference I see between your code and the text book code is that yours will terminate after each execution whereas the text book one keeps looping until the user quits. Nothing wrong with termination if that's what you want to do at that point.

  #3 (permalink)  
Old May 23rd, 2011, 10:13 AM
Registered User
Join Date: May 2011
Location: in the Matrix
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Actually forgot I posted this...

I mean, don't get me wrong and I'm appreciative you responded. But I guess the short answer is "Experience"? I mean, all I know about C# was thru Chapter 4 when I wrote that.

My code may not have been ideal, and sure I coulda copied or downloaded the answer....but for a guy to sit down and write his own code from scratch without help - and it function?

Yea, using the loops and whatnot comes from "Experience".

Even Super Man had to learn how to fly :)

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
Beginning Visual C# - Answers to Excercises Christopher BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 5 November 4th, 2009 04:34 PM
Why are there no formal excercises in this book? misom99 BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 October 3rd, 2007 09:44 AM
validation summary MunishBhatia ASP.NET 2.0 Professional 1 May 11th, 2007 08:00 AM
Summary in footer Blueman137 ASP.NET 1.0 and 1.1 Basics 1 March 30th, 2004 06:48 PM

All times are GMT -4. The time now is 11:27 PM.

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