Compound conditions using while loop in C. -


the program ignoring stop when amt 0 until after 10 numbers have been entered. program doesn't stop after 10 numbers have been entered. error?

main() { int amt; int tot = 0; /* running total */ int = 0;   /* counts number of times in loop */ while (amt!=0 || < 10)     {      printf("enter number (enter 0 stop): ");      scanf("%d", &amt);      tot = tot + amt;      i++;     } printf("the sum of %d number %d.\n", i, tot);  } 

your test happening before amt assigned. results undefined. test should moved end of iteration, i.e. do/while. whilst assign amt non-zero value feels untidy me.

and surely mean use logical , rather logical or? want continue iterating if both amt non-zero , i<10.

of course, if did move test end of iteration have account fact i had been incremented inside loop.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -