Reputation:
I know that while loops have a body part with two curly braces { }
. And I know that how while loops work. But when I was reading a book on C++, I found this code:
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value)
sum += value; // equivalent to sum = sum + value
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
But there are no curly braces { }
and these code is working same as:
#include <iostream>
int main()
{
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value){
sum += value; // equivalent to sum = sum + value
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
So how can it be possible when I omitted curly braces { }
in While Loops and how does it work when it omit the curly braces in while loops?
Upvotes: 1
Views: 992
Reputation: 4064
The while
's syntax is of the following form (simplified, check the link for more info):
while ( condition ) statement
Therefore, we need to supply it a statement after the condition part.
Curly braces denote a compound statement (a.k.a block, block statement). It is a group of zero or more statements that is treated by the compiler as if it were a single statement.
If you don't need a compound statement, you use an expression statements (a.k.a. one-liner). We terminate expression statements with a semicolon ;
, while a block statement is terminated by closing curly brace.
In the example we can get away with an expression statement:
while (std::cin >> vInside curly braces wealue)
sum += value;
In some cases, it is necessary to use curly braces to specify the flow of logic in program even if we use only expression statements. Also, some consider it to be a good practice to routinely use curly braces to explicitly denote boundaries every time.
Upvotes: 5
Reputation: 1
Beacuse while loop in your example just has only 1 statement and then it needn't have { } but still is correct. IF while loop has 2 or more statements, it has to have { } or else this loop will excute only your first statement . You can refer this while loop in C++
Upvotes: 0
Reputation: 311088
The while statement is defined the following way for example in the C++ 14 Standard
while ( condition ) statement
As you see there can be used any statement. For example a null statement like
while ( condition );
^
|
null statement
or a compound statement (that even may contain no statements) like
while ( condition ) {}
because the null statement and the compound statement are statements and there is no restriction on the kind of the statement. Also in C++ (opposite to C) declarations are also statements.
What you should take into account is that (The C++ 14 Standard, 6.5 Iteration statements)
2 The substatement in an iteration-statement implicitly defines a block scope (3.3) which is entered and exited each time through the loop. If the substatement in an iteration-statement is a single statement and not a compound-statement, it is as if it was rewritten to be a compound-statement containing the original statement. [ Example:
while (--x >= 0)
int i;
can be equivalently rewritten as
while (--x >= 0) {
int i;
}
Upvotes: 3
Reputation: 4743
Curly braces for if
while
and for
are only neccesary when statement body contains more than one statement, but it's common to use it always as it protects from introducing bug by adding statement to body (see defensive programming practices).
For example there was vulnarability in SSH library recently like this:
if (condition)
statement1;
statement2;
statement2
intended to be executend only under condition, but it was executed unconditionally.
Probably it was only statement1
originally, but someone added statement2
and forgot to add braces. And somehow code passed review and get to production and cause some damage.
Upvotes: 2
Reputation: 63142
The grammar is while (condition) statement
.
{ /* seqence-of-statements */ }
is itself a statement.
A style guide might mandate {}
for all if
, for
, while
for the benefit of the humans who have to read the code.
Upvotes: 1
Reputation: 510
when you have only one statement it does not matter if you put curly braces or not. and this also applied to if, while, for, else, ...
but when you have more than one statement and if you did not put curly braces only first statement will execute during loop.
Upvotes: 2
Reputation: 11
Both of code is right if you just write one line of code no need at all to put curly braces.
Upvotes: -2
Reputation: 4765
omitting the curly braces makes the loop work on the first statement after the loop expression. so in your case, it will loop sum+=value
.
This also works with if
statements:
if(boolExpression)
doSomething();
doSomethingNotDependendOnBoolExpr();
It is recommended to keep the curly braces. Or - if your codestyle omits them - keep the indendation intact so it is visible what's inside the loop and what is not.
Upvotes: -1
Reputation: 1286
When you omit the curly braces, only the statement right after while
is executed in a loop.
My recommendation is to always use curly braces, as omitting them can create confusion, and lead to unexpected bugs, which can be difficult to notice.
Upvotes: 2