Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   loop problem (http://www.velocityreviews.com/forums/t755033-loop-problem.html)

fattaneh 10-20-2011 03:18 PM

loop problem
 
hi all ,
i have written this code , and it should be in while if num is not
zero
so it shouldn't print error , but num becomes zero and it wont come
out of while
i have written this code in visual studio c++

#include "stdafx.h"
#include <stdio.h>
using namespace System;

int main(array<System::String ^> ^args)
{
int num =5 ;
while (num !=0)
{
num-- ;
if(num ==0)
{
printf("error") ; // it should never write this but it
will write .
}
}
return 0;
}


could you help me to filnd out what the problem is ?

thanks of all . . .

fattaneh 10-20-2011 03:39 PM

Re: loop problem
 
On Oct 20, 6:34*pm, Leigh Johnston <le...@i42.co.uk> wrote:
> On 20/10/2011 16:18, fattaneh wrote:
>
>
>
>
>
>
>
>
>
> > hi all ,
> > i have written this code , and it should be in while if num is not
> > zero
> > so it shouldn't print error , but num becomes zero and it wont come
> > out of while
> > i have written this code in visual studio c++

>
> > #include "stdafx.h"
> > #include<stdio.h>
> > using namespace System;

>
> > int main(array<System::String ^> *^args)
> > {
> > * * *int num =5 ;
> > * * *while (num !=0)
> > * * *{
> > * * * * *num-- ;
> > * * * * *if(num ==0)
> > * * * * *{
> > * * * * * * *printf("error") ; * // it should never write this but it
> > will write .
> > * * * * *}
> > * * *}
> > * * *return 0;
> > }

>
> > could you help me to filnd out what the problem is ?

>
> > thanks of all . . .

>
> The while condition is only tested once at the start of each iteration
> of the loop; it is not tested after every statement inside the loop.
>
> /Leigh


Thank you :)

Markus Wichmann 10-20-2011 04:20 PM

Re: loop problem
 
On 20.10.2011 17:18, fattaneh wrote:
> hi all ,
> i have written this code , and it should be in while if num is not
> zero
> so it shouldn't print error , but num becomes zero and it wont come
> out of while
> i have written this code in visual studio c++
>
> #include "stdafx.h"

Doesn't look much like standard...

> #include <stdio.h>
> using namespace System;
>
> int main(array<System::String ^> ^args)

^^^^^^^^^^^^^^^^^^^^^^^^^
Definitely _not_ standard. The standard is one of:

int main()
int main(int argc, char* argv[])

or equivalent. (There are two other forms for argv that mean exactly the
same.)

> {
> int num =5 ;
> while (num !=0)
> {
> num-- ;
> if(num ==0)
> {
> printf("error") ; // it should never write this but it
> will write .
> }
> }
> return 0;
> }
>
>
> could you help me to filnd out what the problem is ?
>
> thanks of all . . .


Short: Move the decrement instruction to the other side of the loop!

Long: The condition of a while-loop is checked only on entry and on
repeat, not after every instruction inside the loop. Thus, if num is 1
at the beginning of the loop, it is decremented to 0, then the if fires.

HTH,
Markus


All times are GMT. The time now is 02:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57