hey so i just started programming (C) and i wanna know the difference between while and for loops, so i did a program to sum natural numbers with a for loop and it worked:
int sum = 0;
int count;
int num = 11;
for (count = 1; count <= num; count++){
sum += count;
}
printf("Sum of numbers is: %d\n", sum);
Sum came out as 66 and count comes out as 11, but when i tried it in a while loop it came out wrong:
int kount = 1;
int ssum = 0;
int number = 11;
while(kount <= number){
++kount;
ssum += kount;
}
printf("Ssum is: %d \n", ssum);
printf("Kount is %d \n", kount);
Here ssum comes out as 77 and kount comes out as 12. Can anyone explain why to a beginner like me?