Mail Archives: djgpp/1999/01/11/17:20:43
Jason Mullins wrote:
>
> Hello -
> Im new to programming. I wondering why I can't get this code to
> count backwards form 6 to 0 and exit. I will start at 6, and go down to
> negitives (becuase it's a "int"). I was told that it could be done in a
> while loop. not a do while. Any suggestions?
>
> Thanx in advance.
>
> Jason Mullins
I don't know what you want to do exactely, so I'll correct some things
in your code and write another one that works.
> #include <stdio.h>
>
> int main()
> {
> int count;
> int cycle;
> cycle = 0;
> count = 6;
> while (count <= 6, cycle = 6)
^^^^^^^^^^^^^^^^^^^^^
what you are doing here is this:
count will ALLWAYS be <= than 6 because you start at 6 and then
decrease. second, with cycle=6 you ASSIGN 6 to cycle evey loop. I don't
think this is what you want. try cycle <=6 or cycle!=6
> {
> printf("The value of count is %d\n");
^^^^^^^^
you must give the variable to be printed here. something like
printf("The value of count is %d\n",count);
EVERYBODY: even though he doesn't put any variable in the code the
program still prints something that appears to be "cycle" (in the
beginning it's just 6, if you modify cycle=6 in the while it will print
something from 0 to 6.) WHY??
> count = count--;
> cycle = cycle++;
variable-- or variable++ increments it automatically.
you can say var++ or var=var+1, it's the same thing.
that code should be count-- or count=count-1, not count=count--;
the same goes for cycle.
> }
> return 0;
> }
a code that works is this:
#include <stdio.h>
int main()
{
int count;
count = 6;
while (count>=0)
{
printf("The value of count is %d\n",count);
count--;
}
return 0;
}
- Raw text -