Harry
Harry

Reputation: 2950

do while loop without {}

Is the below syntax of writing do while loop containing single statement without using braces like other loops namely while, for, etc. correct? I'm getting the required output but I want to know whether this has any undefined behavior.

int32_t i = 1;
do 
    std::cout << i << std::endl;
while(++i <= 10);

Upvotes: 0

Views: 1894

Answers (6)

Akash Bansal
Akash Bansal

Reputation: 19

No, it's completely fine! Any loop in C++ which expects curly braces and doesn't find them takes the first line into consideration and move on.

Upvotes: 0

tueda
tueda

Reputation: 982

The original resource to answer this question should be the C++ standards: http://www.open-std.org/jtc1/sc22/wg21/docs/standards. In C++17, 9.5 Iteration statements [stmt.iter] says do takes a statement:

do statement while ( expression ) ;

So this is definitely fine.

9.3 Compound statement or block [stmt.block] says

So that several statements can be used where one is expected, the compound statement (also, and equivalently, called “block”) is provided.

so people tend/like to use { ... } for do statements.

Upvotes: 3

iz_
iz_

Reputation: 16573

The body of a do-while construct must be a statement.

attr(optional) do statement while ( expression ) ;

attr(C++11) - any number of attributes

expression - any expression which is contextually convertible to bool. This expression is evaluated after each iteration, and if it yields false, the loop is exited.

statement - any statement, typically a compound statement, which is the body of the loop

cppreference notes that this statement is usually a compound statement, which is a block surrounded by curly braces.

Compound statements or blocks are brace-enclosed sequences of statements.

However, this statement can also just be an expression terminated by a semi-colon, which is the case in your example.

Upvotes: 1

cigien
cigien

Reputation: 60208

The grammar for a do-while loop is given here:

do statement while ( expression ) ;

The grammar for a statement allows for a expression-statement:

statement:

attribute-specifier-seq opt expression-statement

expression-statement:

expression opt ;

So the body of a do-while doesn't need to be inside {}, and your code is valid.

Upvotes: 2

user93353
user93353

Reputation: 14039

If you have only one statement inside the do-while or if or for, the braces aren't necessary

i.e.

do
  statement;   
while (cond)

is the same as

do   
{
  statement;   
}
while (cond)

One the other hand

Likewise

if(cond)
     statement;

is same

if(cond)
{
     statement;
}

If you write

if(cond)
     statement1;
     statement2;

Then this will considered as

if(cond)
{
     statement1;
}
statement2;

Upvotes: 1

Nathan Pierson
Nathan Pierson

Reputation: 5565

cppreference indicates that it's the same with do while as it is with while and if and so on.

The relevant syntax:

attr (optional) do statement while ( expression ) ;

  • attr(C++11) - any number of attributes
  • expression - any expression which is contextually convertible to bool. This expression is evaluated after each iteration, and if it yields false, the loop is exited.
  • statement - any statement, typically a compound statement, which is the body of the loop

The key here is that a statement is typically a compound statement enclosed in curly braces, but not necessarily so.

As with those constructions, I would tend to prefer braces even when it is only a single line, but it's good to know that it works.

Upvotes: 1

Related Questions