1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Could someone explain this to me?

Discussion in 'Code Snippets and Tutorials' started by Hally, Oct 11, 2009.

  1. Hally

    Hally Level IV

    Joined:
    Nov 16, 2006
    Messages:
    1,184
    Likes Received:
    34
    So I'm taking a programming class for first year Engineering. We're coding in C#. I'm understanding everything pretty much perfectly, doing really well. But we had a quiz a few days ago and I got this question wrong. Even after looking over it again and again, I don't understand what I did wrong.

    What value does count hold when the following code fragment is done executing?
    Code (Text):
    1. int count = 0;
    2. int i = 0;
    3. while (i <= 5)
    4. {
    5.    i = i + 1;
    6.    int j = 0;
    7.    while (j <= 3)
    8.    {
    9.       count = count + 1;
    10.       j = j + 1;
    11.    }
    12. }
    13.  
     
  2. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    Ewww, C#... But prejudice aside...

    I think the answer is 24. The first while loop executes 6 times (i = 0... 5) and the inside while loop executes 4 times (j = 0... 3). Therefore the "count=count+1" statement is executed a grand total of 24 times.
     
  3. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    I'm supporting Zer0 with this answer.
    Unless I'm missing something cause it's coded in C#
     
  4. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    C# = Vb.net with C syntax. But yeah, the answer should be 24, as everyone else is saying.
     
  5. Macdoodles

    Macdoodles Level II

    Joined:
    Oct 10, 2009
    Messages:
    222
    Likes Received:
    0
    are you going to like...use that to make a neopet aber ?
     
  6. Hally

    Hally Level IV

    Joined:
    Nov 16, 2006
    Messages:
    1,184
    Likes Received:
    34

    Did you even read what I wrote? Silly :p

    The answer they gave was either 3 or 24. It's weird. No idea how it could be 3. 24 makes sense now that it's explained. I said 15, because I thought it was 5x3, rather than 6x4. That's where I went wrong. Thanks guys.
     
  7. chelsea1

    chelsea1 Level IV

    Joined:
    Nov 26, 2006
    Messages:
    2,538
    Likes Received:
    31
    Location:
    London
    edit: silly me don't worry, just deleted all the rubbish i wrote

    no idea how you could get 3, sorry