loop problem

Discussion in 'C++' started by fattaneh, Oct 20, 2011.

  1. fattaneh

    fattaneh Guest

    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, Oct 20, 2011
    #1
    1. Advertising

  2. fattaneh

    fattaneh Guest

    On Oct 20, 6:34 pm, Leigh Johnston <> 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 :)
    fattaneh, Oct 20, 2011
    #2
    1. Advertising

  3. 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
    Markus Wichmann, Oct 20, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,379
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,225
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    687
    Remon van Vliet
    Jun 15, 2005
  4. Byte
    Replies:
    4
    Views:
    413
  5. Isaac Won
    Replies:
    9
    Views:
    365
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page