Why {} while (condition); requires a semicolon at the end, but while (condition) {} not?

I always have a placement problem ; at the end of the while or not at the end of the do while loops. So what is the reason? Why

 int numItemsToProcess = 3; while(numItemsToProcess > 0) { // process an item numItemsToProcess--; } 

does not need ; at the end but

 do { numItemsToProcess --; } while (numItemsToProcess > 0); 

does? Maybe the reason is not too important. but when you know the reason, you can remember where to put it ; .

+4
source share
4 answers

You put a semicolon after all statements except the block statement. For this reason, you put it after while in do while , but not after the block in while {...} .

You can also use it to complete almost all ads. The only exceptions that I can think of at the moment are function bodies and namespace bodies in C ++.

+10
source

These are two different circuit designs. You just need to remember.

Think of them as single-line:

 do one_thing while( condition ); do { stuff } while( condition ); 

and

 while( condition ) one_thing; while( condition ) { stuff } 

These are all expressions that require a comma at the end, except for the last, where curly brackets logically indicate the end of the expression (think of if ).

+5
source

The reason for this is as follows

  • While condition : - While condition has a signature of type

     while (expression) statement 

So far, the loop is executing in accordance with the expression it provides. If the expression is true, then the statement written during {} will execute differently. You can also tag ; after while (expression); , but it will just populate the while syntax right away, since ; indicates the end of the statement. In general, whil e loop checks the expression and executes a list of instructions inside.

  1. Do While condition : - You have a signature, for example

     do statement while (expression); 

do-while is an interesting loop. It has the specialty that statements following a do will be executed at least regardless of whether the expression in while true or false. The do while thread says that the statement following the do will be executed once, and then the while condition is checked. If the while condition is true, the statements following do again. The reason it exists ; at the end of do ... while (expression); , lies in the fact that while considered here as a statement, since its body is above it. In C ++, every statement must end in ; , so do while ends with ; .

+1
source

Because it is a standard.
See ISO C ++ Standard. Page Number 128
Other than the image This is because while statements are valid in the do-while loop.

 int x = 10; int y = 10; do while(x > 0) x--; while(x = y--); 

Because you are finishing the statement.
The statement ends with either a block (limited by curly braces),
or with a semicolon. “do this while this” is the only expression and cannot end in a block (because it ends with “while”),
therefore, he needs a semicolon, like any other statement.

0
source

All Articles