Post: [C#] Could someone explain this answer?
07-12-2013, 05:26 AM #1
Dan
I'm a god.
(adsbygoogle = window.adsbygoogle || []).push({}); I have been learning C# and one of the things said to do the this problem, and I was wanting to see if someone would explain to me how 5 comes out of this?

    
int count = 0;

while (count < 10)
{
count = count + 1;
}

for (int i = 0; i < 5; i++)
{
count = count - 1;
}

MessageBox.Show("The answer is " + count);
07-12-2013, 06:08 AM #2
Jango
I love my kitteh
Fuck if I know.

The following user thanked Jango for this useful post:

TheUnexpected
07-12-2013, 07:07 AM #3
Pichu
RIP PICHU.
Originally posted by Dan View Post
I have been learning C# and one of the things said to do the this problem, and I was wanting to see if someone would explain to me how 5 comes out of this?

    
int count = 0;

while (count < 10)
{
count = count + 1;
}

for (int i = 0; i < 5; i++)
{
count = count - 1;
}

MessageBox.Show("The answer is " + count);


Better way to write it:

You must login or register to view this content.

It starts at value 0.

It increments to the value 10. (9 is less than 10 so increments. 10 is not less than 10 so count = 10).

It then runs through the for loop 5 times (0, 1, 2, 3, 4) while subtracting 1 from count.

0,1,2,3,4, = 5 times

10 - 5 = 5

Simple as that.
Last edited by Pichu ; 07-12-2013 at 07:10 AM.

The following user thanked Pichu for this useful post:

Dan
07-13-2013, 04:25 PM #4
John-Martin 1
Brutal fighter !!
Originally posted by Dan View Post
I have been learning C# and one of the things said to do the this problem, and I was wanting to see if someone would explain to me how 5 comes out of this?

    
int count = 0;

while (count < 10)
{
count = count + 1;
}

for (int i = 0; i < 5; i++)
{
count = count - 1;
}

MessageBox.Show("The answer is " + count);


You get the answer 5 because 4 is less than 5 therefore the increment loop adds on one but the other loops stops taking 1 off because 5 is not less than 5...

4 < 5
count = count + 1

5 /= <5
"the answer is" + count (which is now 5)

Does this help any...? Smile
07-13-2013, 05:10 PM #5
Read the comments I added.

Originally posted by Dan View Post
I have been learning C# and one of the things said to do the this problem, and I was wanting to see if someone would explain to me how 5 comes out of this?

    
// Declares int count and sets it value to 0
int count = 0;

// Loops while count is less than 10
while (count < 10)
{
// Adds 1 to count on each loop (Will end with 10)
count = count + 1;
}

// Declares int i, sets its value to 0, loops while i is less than 5 and adds 1 to i on each loop
for (int i = 0; i < 5; i++)
{
// Decreases value of count by 1 on each loop (loops 5 times = output is 10 - 5)
count = count - 1;
}

// Shows message box with the end value (5).
MessageBox.Show("The answer is " + count);


In short, the first loop (while) sets the value of count to 10 and the second loop (for) decreases the value by 5
07-13-2013, 07:12 PM #6
Dan
I'm a god.
Thank you all for your help. :p
07-20-2013, 07:14 PM #7
Originally posted by Hitman
I have been learning C# and one of the things said to do the this problem, and I was wanting to see if someone would explain to me how 5 comes out of this?

    
int count = 0;

while (count < 10)
{
count = count + 1;
}

for (int i = 0; i < 5; i++)
{
count = count - 1;
}

MessageBox.Show("The answer is " + count);



you probably already solved this.. but here is cleaner code.

int count = 0;

while (
count < 10)
count++;

for (
int i = 0; i < 5; ++i)
count--;

MessageBox.Show("The answer is " + count);

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo