+ 2

What are errors in this code segment?

while(count1=10); { count = 1; sum = sum+x; count = count+1; }

17th Feb 2018, 5:08 PM
Anshul Sharma
Anshul Sharma - avatar
2 Answers
+ 3
In your while loop, I'm not sure whether you are declaring a variable or testing equality. If you are testing equality, you should do this: while (count1 == 10) { count = 1; sum = sum + x; count = count + 1; } If you want to declare a value, do this: while ((count1 = 10)) { count = 1; sum = sum + x; count = count + 1; } You should be careful, the first loop will create an infinite loop, and the second will not start. If you want so stop the infinite loop, you should change the value of count1 in the loop. If you want the second loop to start, you should check the value of count1, for example while ((count1 = 10) == 10) { // some code }
17th Feb 2018, 5:20 PM
Cailyn Baksh
+ 3
First, what are you trying to do with this loop? 1. while (count1 != 10) or (count1 == 10) 2. You have to declare all of your variables outside of the loop int count = 1, count1, x, sum = 0; 3. Each loop you are setting count to one and then to two. This loop is infinite and will not stop
17th Feb 2018, 5:25 PM
Zeke Williams
Zeke Williams - avatar