it keeps looping and I don't know why. Can someone help?
int a = 0, sum = 0;
int[] x = { 4, 7, 6, 5 };
while (a < x.Length)
{
Console.WriteLine("Sum from whileloop = {0}", sum);
sum += x[a];
}it keeps looping and I don't know why. Can someone help?
int[] x = { 4, 7, 6, 5 };
while (a < x.Length)
{
Console.WriteLine("Sum from whileloop = {0}", sum);
sum += x[a];
}Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ify NonyeluPosted Mar 7, 2016, 9:24 PM
ali tuncerPosted Mar 7, 2016, 8:42 PM
int[] x = { 4, 7, 6, 5 };
while (a < x.Length)
{
sum += x[a];
a = a + 1;
}
Console.WriteLine("Sum from whileloop = {0}", sum);
Ify NonyeluPosted Mar 7, 2016, 8:31 PM
This is the output. This doesn't look right.
Sum from whileloop = 0
Sum from whileloop = 4
Sum from whileloop = 11
Sum from whileloop = 17
Press any key to continue . . .
ali tuncerPosted Mar 7, 2016, 8:18 PM
It will run but you are creating infinite loop, increment "a" at the end of while..
int a = 0, sum = 0;
int[] x = { 4, 7, 6, 5 };
while (a < x.Length)
{
Console.WriteLine("Sum from whileloop = {0}", sum);
sum += x[a];
a = a + 1; //increment a
}
Ify NonyeluPosted Mar 7, 2016, 8:05 PM
Tom MohanPosted Mar 7, 2016, 7:19 PM