Do if(){ } while() statement
Asked Answered
E

5

7

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.

Emylee answered 29/7, 2015 at 10:32 Comment(1)
"I thought that in a do{}while() statement the do part had to be followed by the bracket" is incorrect.Phillisphilly
R
9

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());
Recommend answered 29/7, 2015 at 10:36 Comment(0)
A
5

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
Aubree answered 29/7, 2015 at 10:36 Comment(0)
I
2

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.

Imaginable answered 29/7, 2015 at 11:5 Comment(0)
R
0

Grammar of do while statement is:

do statement while ( expression ) ;

Essentially you can put any statement after the do keyword and before the while keyword. In common use we follow the do with a curly bracket to allow grouping of multiple statements(compound statement) but if you want to put only one statement after the do keyword, curly brace is optional. Although I would strongly recommend to use curly bracket to aid readability.

In your case, if block is one complete statement and it is perfectly fine.

Here is another example of a do statement that contains a single statement.

int i = 0;

do std::cout<<i<<std::endl;
while (i++ < 5);
Rouen answered 29/7, 2015 at 10:45 Comment(1)
The C++ grammar is not described by Backus-Naur Form. Just call it the grammar.Llewellyn
L
0

I think that this kind of indent makes some confusion.

I would personally write this:

if(x.start())
    do
        if(y.foo(x)) {
            // Do things
        }
    while(x.inc())

The statement after do doesn't need brackets if it's only one "line". As same as if, you only use brackets when you have more instructions. In this case brackets are for the if and because the do-while has only one instruction he doesn't use brackets.

This is what I think, if someone can confirm would be better.

Laundrywoman answered 29/7, 2015 at 10:50 Comment(1)
Your habit of dropping braces is going to get you into serious trouble one day.Llewellyn

© 2022 - 2024 — McMap. All rights reserved.