Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > loop problem

Reply
Thread Tools

loop problem

 
 
fattaneh
Guest
Posts: n/a
 
      10-20-2011
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 . . .
 
Reply With Quote
 
 
 
 
fattaneh
Guest
Posts: n/a
 
      10-20-2011
On Oct 20, 6:34*pm, Leigh Johnston <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Markus Wichmann
Guest
Posts: n/a
 
      10-20-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Getting a loop to activate a loop above it Byte Python 4 03-24-2006 03:04 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments