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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tom MohanRank #912 · 2.3K reputationPosted Mar 7, 2016, 2:19 PM EST
Ify Nonyelu7 reputationPosted Mar 7, 2016, 3:05 PM EST
ali tuncerRank #1,498 · 1.1K reputationPosted Mar 7, 2016, 3:18 PM EST
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 Nonyelu7 reputationPosted Mar 7, 2016, 3:31 PM EST
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 tuncerRank #1,498 · 1.1K reputationPosted Mar 7, 2016, 3:42 PM EST
int[] x = { 4, 7, 6, 5 };
while (a < x.Length)
{
sum += x[a];
a = a + 1;
}
Console.WriteLine("Sum from whileloop = {0}", sum);
Ify Nonyelu7 reputationPosted Mar 7, 2016, 4:24 PM EST