In C++ the variable definitions became an operation, which they weren't in C up until that point. That change was made so that you could place the loop variable definition inside the for loop, e.g.
for (int i = 0; i < N; i++) {
printf("%d", i);
}
My question is what is the value of the variable definition operation, e.g. in which case what conditional statement will be executed in this example:
if (int i = N) {
printf("yes");
} else {
printf("no");
}
If the value of i after the initialization is not equal to zero then the if substetement will be executed. Otherwise the else substatement will be executed.
More precisely (the C++ Standard, 6.4 Selection statements)
4 The value of a condition that is an initialized declaration in a
statement other than a switch statement is the value of the declared
variable contextually converted to bool
And (4.12 Boolean conversions)
1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
to member type can be converted to a prvalue of type bool. A zero
value, null pointer value, or null member pointer value is converted
to false; any other value is converted to true.
Consider a simple example
#include <iostream>
#include <cstring>
//...
char nickname[] = "mgn1993";
if ( char *p = std::strchr( nickname, 'm' ) ) *p = 'M';
std::cout << nickname << std::endl;
In this code fragment variable p is only needed inside the substatement of the if statement. There is no great sense to declare the variable in the outer scope.
You can use this as a shorthand for evaluating an expression, and use its return value inside the if block. e.g.
if (int i = calculateSomething()) {
// do something with i
}
which is equivalent to
int i = calculateSomething();
if (i) {
// do something with i
}
except that i's scope is restricted to the if block
The value of a variable definition is the variable itself.
int i = 0; // is 0, and is therefore false
int j = 5; // is 5, and is therefore true
The scope of a variable definition is the block it applies to.
So:
if(int i = returnSomething()) {
// This point is reached if returnSomething() did not return 0
// i is defined in this block and can be used.
}
// i is not defined at that point, its scope being limited to the if block above
In the provided example, the output would be "yes", if N has a non-zero value (which would evaluate to a boolean true in C++). There is no real value to the given example, as you can very easily substitute the entire assignment with 'N' and achieve the same effect.
Maybe there is some strange fringe case where we very much need to both use and be able to adjust the value contained in N ONLY if N is non-zero and we simultaneously need to be absolutely assured the scope is restricted to the if-statement, but this seems an unlikely scenario.
That said, declaring a variable for instance in a for loop certainly has its advantages. For starters the scope is limited to the loop, some compilers optimize specifically for it, potentially cleaner code, etc.
Related
I am playing around a little with for loops , tried the following code and got an infinite loop.
#include<iostream>
int main(){
int i {0};
bool condition = i < 5;
for ( ; condition ; ){
std::cout << "Hello World!" << std::endl;
i++;
}
}
Can someone explain why ?
bool condition = i < 5;
This line defines a variable named condition which has the value true from this line onwards.
It does not bind the expression from the right side, but only copies the result at the time of assignment.
What you intended is more complicated:
auto condition = [&i](){ return i < 5; };
for ( ; condition() ; )
Now condition is a function object which can be evaluated repeatedly.
The right hand of the assignment is called a lambda expression, and follows the form [capture scope](parameters){ body with return statement }.
In the capture scope, you can list variables either by value (without &) in which case they get copied once when the lambda is declared, or by reference (with leading &) in which case they don't get copied but the variable inside the lambda is a reference to the variable of the same name outside the lambda. There is also the short form [&] which captures all variables in the parent scope by reference, and [=] which captures everything by value.
auto can be used for brevity in combined declarations + assignments, and automatically resolves the type of the variable from the right hand side.
The closest compatible type you could explicitly specify would be std::function<bool(void)> (generic container for functions with that signature), the actual type of that object is some internal type representing the naked lambda which you can't write explicitly. So if you can't know the exact type, and you don't want to use a generic container type, auto is occasionally even necessary.
Variables in C++ store values. You seem to be under the impression that condition somehow remains connected to the expression i < 5. It is not. Once you set the value which is true at the time of the assignment it will keep that value until you change it. You never change it again so the value of condition is forever true.
citing cppref:
Executes init-statement once, then executes statement and iteration-expression repeatedly, until the value of condition becomes false. The test takes place before each iteration.
Now, your condition evaluates to true:
bool condition = i < 5; // true
hence, the loop will continue until false, which does not happen in the loop body of your code; condition is not written to after initialization, and therefore the loop will not stop.
The for loop runs on a condition, such as while i is below a specified value, and will keep running until this is no longer satisfied. In your case, it is never satisfied, guaranteeing an infinite loop.
In the following code, why is the variable i not assigned the value 1?
#include <stdio.h>
int main(void)
{
int val = 0;
switch (val) {
int i = 1; //i is defined here
case 0:
printf("value: %d\n", i);
break;
default:
printf("value: %d\n", i);
break;
}
return 0;
}
When I compile, I get a warning about i not being initialized despite int i = 1; that clearly initializes it
$ gcc -Wall test.c
warning: āiā is used uninitialized in this function [-Wuninitialized]
printf("value %d\n", i);
^
If val = 0, then the output is 0.
If val = 1 or anything else, then the output is also 0.
Please explain to me why the variable i is declared but not defined inside the switch. The object whose identifier is i exists with automatic storage duration (within the block) but is never initialized. Why?
According to the C standard (6.8 Statements and blocks), emphasis mine:
3 A block allows a set of declarations and statements to be grouped
into one syntactic unit. The initializers of objects that have
automatic storage duration, and the variable length array declarators
of ordinary identifiers with block scope, are evaluated and the values
are stored in the objects (including storing an indeterminate value
in objects without an initializer) each time the declaration is
reached in the order of execution, as if it were a statement, and
within each declaration in the order that declarators appear.
And (6.8.4.2 The switch statement)
4 A switch statement causes control to jump to, into, or past the
statement that is the switch body, depending on the value of a
controlling expression, and on the presence of a default label and the
values of any case labels on or in the switch body. A case or default
label is accessible only within the closest enclosing switch
statement.
Thus the initializer of variable i is never evaluated because the declaration
switch (val) {
int i = 1; //i is defined here
//...
is not reached in the order of execution due to jumps to case labels and like any variable with the automatic storage duration has indeterminate value.
See also this normative example from 6.8.4.2/7:
EXAMPLE In the artificial program fragment
switch (expr)
{
int i = 4;
f(i);
case 0:
i = 17; /* falls through into default code */
default:
printf("%d\n", i);
}
the object whose identifier is i exists with
automatic storage duration (within the block) but is never
initialized, and thus if the controlling expression has a nonzero
value, the call to the printf function will access an indeterminate
value. Similarly, the call to the function f cannot be reached.
In the case when val is not zero, the execution jumps directly to the label default. This means that the variable i, while defined in the block, isn't initialized and its value is indeterminate.
6.8.2.4 The switch statement
A switch statement causes control to jump to, into, or past the statement that is the
switch body, depending on the value of a controlling expression, and on the presence of a
default label and the values of any case labels on or in the switch body. A case or
default label is accessible only within the closest enclosing switch statement.
Indeed, your i is declared inside the switch block, so it only exists inside the switch. However, its initialization is never reached, so it stays uninitialized when val is not 0.
It is a bit like the following code:
{
int i;
if (val==0) goto zerovalued;
else goto nonzerovalued;
i=1; // statement never reached
zerovalued:
i = 10;
printf("value:%d\n",i);
goto next;
nonzerovalued:
printf("value:%d\n",i);
goto next;
next:
return 0;
}
Intuitively, think of raw declaration like asking the compiler for some location (on the call frame in your call stack, or in a register, or whatever), and think of initialization as an assignment statement. Both are separate steps, and you could look at an initializing declaration in C like int i=1; as syntactic sugar for the raw declaration int i; followed by the initializing assignment i=1;.
(actually, things are slightly more complex e.g. with int i= i!=i; and even more complex in C++)
Line for initialization of i variable int i = 1; is never called because it does not belong to any of available cases.
The initialization of variables with automatic storage durations is detailed in C11 6.2.4p6:
For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration or compound literal is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.
I.e. the lifetime of i in
switch(a) {
int i = 2;
case 1: printf("%d",i);
break;
default: printf("Hello\n");
}
is from { to }. Its value is indeterminate, unless the declaration int i = 2; is reached in the execution of the block. Since the declaration is before any case label, the declaration cannot be ever reached, since the switch jumps to the corresponding case label - and over the initialization.
Therefore i remains uninitialized. And since it does, and since it has its address never taken, the use of the uninitialized value to undefined behaviour C11 6.3.2.1p2:
[...] If the lvalue designates an object of automatic storage duration that could have been declared with the register storage class (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined.
(Notice that the standard itself here words the contents in the clarifying parenthesis incorrectly - it is declared with an initializer but the initializer is not executed).
The compiler throws no warnings or errors for the following code. Is the meaning of the const qualifier being abused? Obviously I cannot reassign it later in the same loop iteration but it does seem to reassign it after each iteration.
Sample code:
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
You aren't re-initializing it, you're just initializing it in each loop iteration*. Formally there is a new int being created and destroyed in each loop iteration, although the compiler can do whatever it wants as long as it seems to behave that way.
* You can't really "re-initialize" things in C++, initialization only happens once in the lifetime of an object
If to follow the C Standard then (6.2.4 Storage durations of objects)
1 An object has a storage duration that determines its lifetime.
There are four storage durations: static, thread, automatic, and
allocated. Allocated storage is described in 7.22.3.
and
5 An object whose identifier is declared with no linkage and without
the storage-class specifier static has automatic storage duration,
as do some compound literals. The result of attempting to indirectly
access an object with automatic storage duration from a thread other
than the one with which the object is associated is
implementation-defined.
6 For such an object that does not have a variable length array type,
its lifetime extends from entry into the block with which it is
associated until execution of that block ends in any way. (Entering an
enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered
recursively, a new instance of the object is created each time. The
initial value of the object is indeterminate. If an initialization is
specified for the object, it is performed each time the declaration or
compound literal is reached in the execution of the block; otherwise,
the value becomes indeterminate each time the declaration is reached
And at last (6.8.5 Iteration statements)
5 An iteration statement is a block whose scope is a strict subset of
the scope of its enclosing block. The loop body is also a block
whose scope is a strict subset of the scope of the iteration
statement.
Thus in this loop statement
for(int i = 0; i < 10; ++i)
{
const int constant = i;
}
the body of the loop is a block. The variable constant has the automatic storage duration. A new instance of the variable is created each time the block is executed recursively.
In C++ you may add storage class specifier static. In this case the variable indeed will be initialized only once because it has the static storage duration ( In C you may not do the same because the variable must be initialized by a constant expression).
Here is a demonstrative program
#include <iostream>
int main()
{
for ( int i = 0; i < 10; ++i )
{
static const int constant = i;
std::cout << "constant = " << constant << std::endl;
}
return 0;
}
Its output is
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
constant = 0
You're not actually reinitializing here. You're creating a new variable each time through the loop.
constant is local to the block inside of the loop. When the block finishes on a given iteration and control goes back to the for, constant goes out of scope and therefore no longer exists. When the for starts the next iteration of the loop, a new instance of constant is created and initialized.
This variable gets initialized and destroyed on each iteration because it's local to the loop.
The following syntax is valid:
while (int i = get_data())
{
}
But the following is not:
do
{
} while (int i = get_data());
We can see why via the draft standard N4140 section 6.4:
1 [...]
condition:
expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
2 The rules for conditions apply both to selection-statements and
to the for and while statements (6.5). [...]
and section 6.5
1 Iteration statements specify looping.
iteration-statement:
while ( condition ) statement
do statement while ( expression ) ;
Instead, you're forced to do something ugly like:
int i = get_data();
do
{
} while ((i = get_data())); // double parentheses sic
What is the rationale for this?
It seems like scoping would be the issue, what would be the scope of i declared in the while portion of a do while statement? It would seem rather unnatural to have a variable available within the loop when the declaration is actually below the loop itself. You don't have this issue with the other loops since the declarations comes before the body of the loop.
If we look at the draft C++ standard section [stmt.while]p2 we see that for the while statement that:
while (T t = x) statement
is equivalent to:
label:
{ // start of condition scope
T t = x;
if (t) {
statement
goto label;
}
} // end of condition scope
and:
The variable created in a condition is destroyed and created with each iteration of the loop.
How would we formulate this for the do while case?
and as cdhowie points out if we look at section [stmt.do]p2 it says (emphasis mine):
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.
which means the body of the loop is evaluated before we would even reach the declaration.
While we could create an exception for this case it would violate our intuitive sense that in general the point of declaration for a name is after we see the complete declaration(with some exceptions for example class member variables) with unclear benefits. Point of declaration is covered in section 3.3.2.
There are several reasons for why it would be difficult to allow.
The language sticks to the general rule that everything should be declared above the point of usage. In this case the variable declared in do-while would be declared below its expected natural scope (the cycle body). Making this variable accessible inside the cycle would've required a special treatment for do-while cycles. Even though we know examples of such special treatment (e.g. in-class member function bodies can see all class members, including the ones declared below), there's probably not much practical sense in doing it for do-while cycles.
In case of do-while these special treatment rules would also require finding a meaningful way of handling initialization of variables declared in this fashion. Note that in C++ language the lifetime of such variable is limited to one iteration of the loop, i.e. the variable is created and destroyed on each iteration. That means that for do-while cycle the variable will always remain uninitialized, unless you introduce some rule that would somehow move the initialization to the beginning of the loop body. That would be quite confusing in my opinion.
It would be very unnatural to have a declaration of i after the block and to then be able to access it in the block. Declaration in for and while are nice short-hands that give limited-scope use to a variable that is needed in the loop logic.
Cleaner to do it this way:
int i;
do {
i = get_data();
// whatever you want to do with i;
} while (i != 0);
This is because everything else follows the practice of declaring variables before you use them, eg:
public static void main(String[] args){
// scope of args
}
for(int i=1; i<10; i++){
// scope of i
}
{
...
int somevar;
//begin scope of var
...
//end of scope of var
}
This is because things are parsed top down, and because following this convention keeps things intuitive, thus why you can declare a while(int var < 10) because the scope of that var will be the area inside the loop, after the declaration.
The do while doesn't make any sense to declare a variable because the scope would end at the same time it would be checked because that's when that block is finished.
Add this
#define do(cond) switch (cond) do default:
at the beginning of your code.
Now, you can write
do (int i = get_data())
{
// your code
} while ((i = get_data()));
It is important that this #define does not break the original syntax of the do keyword in do-while loop.
However, I admit that it is obscure.
Your first syntax is valid while the second is not.
However, your while loop will loop forever, even if your function get_data() returns 0.
Not sure if that's exactly what you want to happen.
Is there a way to declare, assign and compare a variable to an expression in an if construction in such a way that it is only defined in the scope of the if construction?
This works (declare and assign, but of course the condition is only the return value of function f being equal to zero or not):
int main()
{
if(int i = f())
{
printf("%d", i);
// i is defined here!
}
else
{
// Here too!
}
// But not here!
}
But when I try to compare the value of i with an actual expression I run into trouble:
int main()
{
// Does not compile because "int i = f()" is not a primary expression?
if((int i = f()) == 3)
{
printf("%d", i);
}
}
Creating a scope around the whole if construction kind of does the trick, from a behavior point of view, but it looks ugly in my opinion:
int main()
{
{
int i = f();
if(i == 3)
{
printf("%d", i);
// i is defined here!
}
// here too!
}
// i is not defined here!
}
I'm comparing with the look and feel of for-loops and switches where it's so neat to declare and assign a variable so it is only defined in the scope in question. Of course the value of the variable is not compared to anything there and i'm not saying it's the same thing:
for(int i = 0;;)
{
break;
}
// i is not defined here
switch(int i = f())
{
default: break;
}
// i is not defined here!
So to sum it up, is there a way to tie the definition of a variable to the scope of "if" in a similar way as it's commonly tied to the scope of for and (perhaps not as commonly) switch, and where you actually compare the value of the variable to an expression as the condition of the if-statement?
As far as I can tell there is no way to have both a declaration and an expression within the condition of an if statement. If we look at the draft C++ standard section 6.4 Selection statements the grammar for if is as follows:
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement
condition:
expression
attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
So you either can use an expression or a declaration and I don't see any obvious ways around that.
What you proposed in the alternative, declaring i before the if statement seems like the best option. Although using an enclosing block does not seem necessary:
int i = f();
if(i == 3)
You can declare the variable in the if, but if you do so, the
condition depends on the implicit conversion of the type to
bool, which is very limiting and a bit obfuscating. The
definition in the for works because the first part of the
for isn't part of the condition. Just write:
int i = f();
if ( i == 3 ) {
// ...
} else {
// ...
}
This leaves the variable in scope until the end of the enclosing
block, but the enclosing block can't be bigger than the entire
function; if leaving it in scope is a problem, then your
functions are too large and complex, and need to be refactored.
You can only do a declaration OR boolean logic in an if statement. The C++ spec says so somewhere, forgot where exactly. Therefore a code like:
if (int x = 3 && x == 3) {} will never compile because it will also throw the error that x is used uninitialized
The first snippet works because you are declaring and defining i in if's conditional expression. It will always evaluated to true.
The second snippet does not compile because you are comparing a non-primary (because i is declared and defined here) expression in conditional expression with a primary expression.