简体   繁体   中英

Loop_expression in 'for' statement in Objective-C

Is it possible to put 'x/10' as a loop_expression in 'for' statement as following:

for ( ; numberD != 0 ; x / 10) {
    resultNumber = numberD / x;
    NSLog (@"Display %i", resultNumber);
    numberD /= 10;
}

I tried to run this way, but it fails to do it. Any suggestions of how to do it, if I need the loop to divide x by 10 every time?

Untested, but I believe your operator is wrong.

for ( ; numberD != 0 ; x /= 10) {
    resultNumber = numberD / x;
    NSLog (@"Display %i", resultNumber);
    numberD /= 10;
}

I would say better use a while loop instead if for loop.

 while(numberD != 0) {
   resultNumber = numberD / x;
   NSLog (@"Display %i", resultNumber);
   numberD /= 10;
   x /= 10;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM