Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11016 > unrolled thread

loop problem

Started byfattaneh <fattaneh.69@gmail.com>
First post2011-10-20 08:18 -0700
Last post2011-10-20 18:20 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  loop problem fattaneh <fattaneh.69@gmail.com> - 2011-10-20 08:18 -0700
    Re: loop problem Leigh Johnston <leigh@i42.co.uk> - 2011-10-20 16:34 +0100
      Re: loop problem fattaneh <fattaneh.69@gmail.com> - 2011-10-20 08:39 -0700
    Re: loop problem Markus Wichmann <nullplan@gmx.net> - 2011-10-20 18:20 +0200

#11016 — loop problem

Fromfattaneh <fattaneh.69@gmail.com>
Date2011-10-20 08:18 -0700
Subjectloop problem
Message-ID<24519ed1-c869-41ee-931a-3896cca4db97@gk10g2000vbb.googlegroups.com>
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 . . .

[toc] | [next] | [standalone]


#11017

FromLeigh Johnston <leigh@i42.co.uk>
Date2011-10-20 16:34 +0100
Message-ID<mOOdnc_pMpI9oj3TnZ2dnUVZ7v2dnZ2d@giganews.com>
In reply to#11016
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

[toc] | [prev] | [next] | [standalone]


#11018

Fromfattaneh <fattaneh.69@gmail.com>
Date2011-10-20 08:39 -0700
Message-ID<9fbfeb56-f18e-4015-a7ab-4a3d2cb180fe@a12g2000vbz.googlegroups.com>
In reply to#11017
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  :)

[toc] | [prev] | [next] | [standalone]


#11024

FromMarkus Wichmann <nullplan@gmx.net>
Date2011-10-20 18:20 +0200
Message-ID<qbu5n8-e32.ln1@voyager.wichi.de.vu>
In reply to#11016
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web