February 28, 2020 preset counter variable in a for loop --> 'has no effect' Error | ||||
---|---|---|---|---|
| ||||
Hello, I don't understand why this simple code causes a compiler error.. import std.stdio; void main(){ int b = 0; for (b; b<3; b++){ writeln(b); } } $Error: b has no effect Same works perfectly fine in C++ #include <iostream> int main(){ int i = 0; for(i; i<3; i++) std::cout<<i<<'\n'; } Please help me find what I am doing wrong. Thx |
February 28, 2020 Re: preset counter variable in a for loop --> 'has no effect' Error | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On 2/28/20 11:48 AM, Namal wrote:
> Hello,
>
> I don't understand why this simple code causes a compiler error..
>
> import std.stdio;
>
> void main(){
>
> int b = 0;
>
> for (b; b<3; b++){
> writeln(b);
> }
> }
>
> $Error: b has no effect
>
> Same works perfectly fine in C++
>
> #include <iostream>
>
> int main(){
> int i = 0;
>
> for(i; i<3; i++)
> std::cout<<i<<'\n';
> }
>
> Please help me find what I am doing wrong. Thx
D compiler is smart enough to say that the first use of `b` in for loop is useless. Use either this variant:
```
import std.stdio;
void main(){
int b = 0;
for (; b<3; b++){
writeln(b);
}
}
```
or this:
```
import std.stdio;
void main(){
for (int b; b<3; b++){
writeln(b);
}
}
```
|
Copyright © 1999-2021 by the D Language Foundation