Well, in Python, you can attach an ELSE to a WHILE statement like so:

# python
while oi.hasNext() :
  print oi.value
else :
  print 'oi had no values'

You can’t do that in C++, but you can do this instead:

// C
if( ! oi.hasNext() )
  print 'oi has no values' ;
else do
{
  print oi.value ;
} while( oi.hasNext() ) ;

I used else do instead of


if( ! oi.hasNext() )
  print 'oi has no values' ;
else while( oi.hasNext() )
{
  print oi.value ;
} 

In case that oi.hasNext() has a side effect (such as advancing an iterator).. in this case, oi.hasNext() gets called twice before we ever try to read oi.value.

Post a Comment