简体   繁体   中英

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);

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.

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.

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.

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.

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

ie

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;

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.

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