DO-WHILE LOOP

Hello
First of all i am new to c++ im using c++ 4.5 or turbo c++ 4.5 (borland)
because i want to learn the old c++ before the new c++ so i can make a thesis about it on how it evolves and etc..
_____________________________________________________________________________

So now my problem is im having a trouble making this pattern using do while statements

1234
123
12
1
this is the code i have:
#include<iostream.h>
int x;
main()
{
do
{
cout<<x;
}
while(x>10)
return 0;
}
PLEASE HELP :'(
x never gets changed and has no value at the start
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream.h>

int main()
{
   int x = 1234;
 
   do
   {
       cout << x << endl;
   } while( ( x /= 10 ) != 0 );

   return 0;
}
Last edited on
Thanks Vlad! :)
Really help me there! :)

and Darkmaster : Oh yes i remember! :) THANKS ALSO! :)
Really helped me out there guys! :)
You can write the condition more simpler

while ( x /= 10 );
but that only works if x is int
Yeah vlad :)
I already make it short Thanks anyways! ;)
THANKS! :)
It works for any arithmetic type (long long, int, char, double ...and even bool). As far as I know other types except special user defined types have no operator /=:)
Last edited on
the problem with float is: 1/10 isn't 0
so the while statement which stops with int, doesn't stop here.
That's a problem with your code then, not vlad's. It should be while (x /= 10 > 0). But that's irrelevant. The code supplied suits the needs of the OP.
Last edited on
By the way in C# you shall write while ( ( x /= 10 ) != 0 ); If you will write while ( x /= 10 ); the compiler issues an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;

class EntryPoint
{
    static void Main()
    {
        int x = 1234;

        do
        {
            Console.WriteLine(x);
        } while ((x /= 10) != 0);
    }
}
@Darkmaster
the problem with float is: 1/10 isn't 0
so the while statement which stops with int, doesn't stop here.


There is no any sense to use float because even 12 / 10 will give result 1.2 instead of 1
Last edited on
It works for any arithmetic type (long long, int, char, double ...and even bool)

that was not my idea.

but i guess you meant

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream.h>

int main()
{
   int x = 1234; //this datatype
 
   do
   {
       cout << x << endl;
   } while( ( x /= 10 )>= 0 ); //this statement

   return 0;
}

while i meant
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main()
{
   int x = 1234; //this datatype
 
   do
   {
      std:: cout << x << std::endl;
   } while(x /= 10); //this statement
}

Last edited on
the x vlaue ,where..
Topic archived. No new replies allowed.