Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do if(){ } while() statement

I am currently working on somebody's else code, with a statement like this

if(x.start()) do if(y.foo(x)) {

// Do things

}while(x.inc())

here x is custom class that holds information on y and allows iteration through its element in a special order. I put this info if relevant, but my question is more general:

I thought that in a do{}while() statement the do part had to be followed by the bracket, and this togheter with the while() condition at the end defines the do-while loop.

  • Why can we put an if right after the do?
  • What does it do?
  • What else can be put in between do and {?

I couldn't find other questions relating to this or on google, most stuff related to putting if statements inside while loop.

like image 533
Three Diag Avatar asked Jul 29 '15 10:07

Three Diag


Video Answer


3 Answers

The grammar permits any statement between do and while. It's just you usually see a particular form of statement there - the compound-statement, { /* statements */ }, also commonly called a block.

The do-while portion of the code is exactly equivalent to

do {
    if(y.foo(x)) {
        // Do things
    }
} while(x.inc());
like image 64
T.C. Avatar answered Sep 21 '22 17:09

T.C.


The do-while statement is defined the following way

do statement while ( expression ) ;

So between the do and while there can be any statement including the if statement.

As for your question

•What else can be put in between do and {?

According to the grammar after the do there must be a statement. So the only possibility that can look strange but is valid is to place a label. For example

do L1: { std::cout << "Hello do-while!" << std::endl; } while ( false );

because labeled statements also may be used.

For example the do-while statement from your post could look like

if(x.start()) do Come_Here: if(y.foo(x)) {

// Do things

}while(x.inc())

Take into account that you also may use an empty statement. In this case it will look like

do ; while ( false );

or

do Dummy:; while ( false );

And one more funny statement

do One: do Two: do Three:; while ( 0 ); while ( 0 ); while ( 0 );

Also in C++ declarations are also statements. So you may place a declaration between the do and while.

For example

int n = 10; 
do int i = ( std::cout << --n, n ); while ( n );

In C declarations are not statements. So you may not place a declaration between the do and while in C.

And another funny example

#include<iostream>
#include <vector>
#include <stdexcept>

int main()
{
    std::vector<int> v = { 1, 2, 3 };
    size_t i = 0;

    do try { std::cout << v.at( i ) << ' '; }  catch ( const std::out_of_range & ) 
    { std::cout << std::endl; break; } while ( ++i );

    return 0;
}

The program output is

1 2 3
like image 43
Vlad from Moscow Avatar answered Sep 21 '22 17:09

Vlad from Moscow


According to C++14 standard,

§6.5 Iteration statements:

do statement while ( expression );

Where statement can be:

§6 Statements:

labeled-statement
expression-statement
compound-statement (also, and equivalently, called “block”):   
    { statement-seq }
    statement-seq:   
        statement
        statement-seq statement
...

So you can put any valid statements in between do and while.

Note that, in the do statement the substatement is executed repeatedly until the value of the expression becomes false. The test takes place after each execution of the statement.

like image 43
rakeb.mazharul Avatar answered Sep 19 '22 17:09

rakeb.mazharul