Post: failed the class :(
12-02-2015, 05:32 PM #1
HeroWin
Java Developer
(adsbygoogle = window.adsbygoogle || []).push({}); i failed a class because i was one question off and this was the question... please explain the answer as to why its D... i dont get why

ITS IN JAVA

    Exactly one of these code fragments calculates the sum of the integers from 1 through 9. Which one?
A
int n = 9;
int sum = 0;

while ( n > 1 )
{
sum += n;
}
B
int n = 9;
int sum = 0;

while ( n > 1 )
{
n--;
sum += n;
}
C
int n = 0;
int sum = 0;

while ( n < 9 )
{
sum += n;
n++;
}
D
int n = 9;
int sum = 0;

while ( n > 0 )
{
sum += n;
n--;
}
E
int n = 0;
int sum = 0;

while ( n <= 9 )
{
n++;
sum += n;
}
12-03-2015, 12:38 AM #2
Trefad
I defeated!
Originally posted by HeroWin View Post
i failed a class because i was one question off and this was the question... please explain the answer as to why its D... i dont get why

ITS IN JAVA

    Exactly one of these code fragments calculates the sum of the integers from 1 through 9. Which one?
A
int n = 9;
int sum = 0;

while ( n > 1 )
{
sum += n;
}
B
int n = 9;
int sum = 0;

while ( n > 1 )
{
n--;
sum += n;
}
C
int n = 0;
int sum = 0;

while ( n < 9 )
{
sum += n;
n++;
}
D
int n = 9;
int sum = 0;

while ( n > 0 )
{
sum += n;
n--;
}
E
int n = 0;
int sum = 0;

while ( n <= 9 )
{
n++;
sum += n;
}





Since sum's value is 0, += n makes sum add to 9 since n = 9. Then n is subtracted by one by using n-- (I'm not sure why it's subtracted). This is a loop. While n is less than 0 it loops.
12-04-2015, 09:59 AM #3
Winter
Purple God
Originally posted by Trefad View Post
Since sum's value is 0, += n makes sum add to 9 since n = 9. Then n is subtracted by one by using n-- (I'm not sure why it's subtracted). This is a loop. While n is less than 0 it loops.

You answered your own question within your answer. It's subtracted because the loop would go on forever otherwise.
Originally posted by HeroWin View Post
i failed a class because i was one question off and this was the question... please explain the answer as to why its D... i dont get why

ITS IN JAVA

    Exactly one of these code fragments calculates the sum of the integers from 1 through 9. Which one?
A
int n = 9;
int sum = 0;

while ( n > 1 )
{
sum += n;
}
B
int n = 9;
int sum = 0;

while ( n > 1 )
{
n--;
sum += n;
}
C
int n = 0;
int sum = 0;

while ( n < 9 )
{
sum += n;
n++;
}
D
int n = 9;
int sum = 0;

while ( n > 0 )
{
sum += n;
n--;
}
E
int n = 0;
int sum = 0;

while ( n <= 9 )
{
n++;
sum += n;
}


    
N equals 9
SUM equals 0

while N does NOT equal 0
add N to SUM
subtract 1 from N


code-wording helps people who can't visualize the effect of code.

The following user thanked Winter for this useful post:

HeroWin
12-04-2015, 08:47 PM #4
vRice
Haxor!
Originally posted by Trefad View Post
Since sum's value is 0, += n makes sum add to 9 since n = 9. Then n is subtracted by one by using n-- (I'm not sure why it's subtracted). This is a loop. While n is less than 0 it loops.


I would say it's because it's less stress on the CPU/more efficient to decrement, but I'm not sure if that's only for loops

The following user thanked vRice for this useful post:

Trefad
12-05-2015, 06:23 PM #5
Originally posted by HeroWin View Post
i failed a class because i was one question off and this was the question... please explain the answer as to why its D... i dont get why

ITS IN JAVA

    Exactly one of these code fragments calculates the sum of the integers from 1 through 9. Which one?
A
int n = 9;
int sum = 0;

while ( n > 1 )
{
sum += n;
}
B
int n = 9;
int sum = 0;

while ( n > 1 )
{
n--;
sum += n;
}
C
int n = 0;
int sum = 0;

while ( n < 9 )
{
sum += n;
n++;
}
D
int n = 9;
int sum = 0;

while ( n > 0 )
{
sum += n;
n--;
}
E
int n = 0;
int sum = 0;

while ( n <= 9 )
{
n++;
sum += n;
}


The rest of the loops are starting from different integers, just remember that it always start from 0. Otherwise, it would be wrong.
12-05-2015, 08:53 PM #6
Trefad
I defeated!
Oh okay.
12-06-2015, 02:25 AM #7
HeroWin
Java Developer
thank you for the clarification people

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo