7 Replies - 522 Views - Last Post: 20 February 2011 - 04:10 PM Rate Topic: -----

#1 crayon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Loop Style Question

Posted 20 February 2011 - 08:59 AM

I've been reading about the basics of C# for a while, but this is day 1 of writing code. I've got a simple question on style, readability, and efficiency.

I've got a method, called GameLoop();. At the moment, it only contains a loop that prints a string (so that I know the loop is still occurring), and an if statement that determines if we are going to break the loop or not.

Here is the code as I have it written currently:
static void GameLoop()
        {
            bool running = true;
 
            do
            {
                Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
            
                if (Console.ReadLine().ToUpper() == "Q")
                {
                    running = false;
                }
            } while (running);
        }



I could also create a new variable, called input, and do this:
static void GameLoop()
        {
            bool running = true;
            string input;
 
            do
            {
                Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
                input = Console.ReadLine();
            
                if (input.ToUpper() == "Q")
                {
                    running = false;
                }
            } while (running);
        }



Obviously, I'm not worried about user interface or anything like that at the moment. :) But from a style standpoint, which version of the loop would be better? From an efficiency standpoint, is there a difference between the two?

Thanks for the advice!

Is This A Good Question/Topic? 0
  • +

Replies To: Loop Style Question

#2 mouse88  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 192
  • Joined: 27-July 10

Re: Loop Style Question

Posted 20 February 2011 - 09:04 AM

I dont think this will make a great deal of difference as it is only a small amount of code but the second method holds an additional variable so it will take up more resource.

Matt
Was This Post Helpful? 1
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Loop Style Question

Posted 20 February 2011 - 09:27 AM

Hi,

I agree with mouse88. It is generally just a preference thing. It really isn't going to make a difference either way. I would personally shorten it to something like this:

do Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
while (!Console.ReadLine().ToUpper().Equals("Q"));



but that's just my style really. Both your loops are perfectly valid, but I personally tend to try not introduce new variables unless they are necessary, just because I see extra variables (particuarly global variables) as extra oportunities for bugs to creep in. It'll have no real effect here though :).

Having said that, if you find yourself making multiple calls to a method, just to get the same output (because you want to use the output multiple times), it is better to declare a variable, call the method once and store the value in a variable, then use that variable when necessary. That way, you only make one call to the method, rather than multiple unnecessary ones. The aim is to reduce repetition and duplicate code, while keeping the code nice and tight and secure :)

This post has been edited by CodingSup3rnatur@l-360: 20 February 2011 - 10:00 AM

Was This Post Helpful? 0
  • +
  • -

#4 crayon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Re: Loop Style Question

Posted 20 February 2011 - 09:44 AM

View PostCodingSup3rnatur@l-360, on 20 February 2011 - 09:27 AM, said:

do Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
while (!Console.ReadLine().ToUpper().Equals("Q"));

.


CodingSup3rnatur@l-360,

Your example is much more concise than anything I could have imagined. Although it is more efficient, at my current level, it is difficult to decipher. I am sure that as I grow as a programmer, I will come to use more concise code and understand it better, but for now, that code does not seem "clear" to me if you understand.

Thank you for your responses!
Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Loop Style Question

Posted 20 February 2011 - 09:48 AM

Yep, that's perfectly fine. I can totally understand that when your learning particuarly, it can help alot to really spell what your doing out, and declare plenty of variables so you can really analyse step by step what's going on. I remember I was just like that when I first started, and there is nothing wrong with that if that's what makes it clear for you. In fact, that's the way you should go when your first starting out :).

There really is little/no difference in efficiency between my code and your code. Mine is just more concise, that's all. Like you said though, you'll be able to understand/write more concise code as you learn more if you wish to.

This post has been edited by CodingSup3rnatur@l-360: 20 February 2011 - 10:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Loop Style Question

Posted 20 February 2011 - 09:49 AM

It's basically saying, run the code in the do {} block, as long as the entered key is not Q. Pretty much that.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5481
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Loop Style Question

Posted 20 February 2011 - 12:12 PM

Just a tip: Read the code out loud. C# isn't that far off from English. Often hearing it helps it make sense.

do Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
while (!Console.ReadLine().ToUpper().Equals("Q"));



Another way to do this that 'reads' more like plain English would be
do Console.WriteLine("Greetings.  This is the game loop.  If you want, you can exit by pressing \"Q.\"");
while (Console.ReadLine().ToUpper() != "Q");



If you read this as English you have something that makes sense:
Do Write {a} line "blah blah"
while Readline to upper{case} {is} not equal to "Q"
Was This Post Helpful? 0
  • +
  • -

#8 crayon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-February 11

Re: Loop Style Question

Posted 20 February 2011 - 04:10 PM

It is not difficult to understand. I was able to understand it perfectly. My point was more that I do not have an immediate understanding of the code just from looking at it. I have to think about it. This is not a bad thing, and I am not complaining. I am just pointing out that at my present level of skill, although I can easily decipher each version of the loop as well as identify exactly what makes them different, it is easier to decipher at first glance the first two versions of the loop.

I'm simply not at the level necessary to write code that elegant and concise as a natural instinct. I would have to first write out what is natural to me, then go back and think about how I could make it more concise. Yes, it does make perfect sense. Yes, I do understand it perfectly. No, I am not at the level where it is natural to write code that beautifully.

Thanks again, guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1