| Thread overview | 
|---|
February 28, 2020 preset counter variable in a for loop | ||||
|---|---|---|---|---|
  | ||||
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 | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to Namal  | On Friday, 28 February 2020 at 12:44:52 UTC, 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
Well, that's the error. b has no effect, so there's
no need for it, and it's likely written in error. It
has no effect in C++ either, and you may get a warning:
example.cc: In function 'int main()':
example.cc:7:8: warning: statement has no effect [-Wunused-value]
  for (b; b<3; b++){
 | |||
February 28, 2020 Re: preset counter variable in a for loop | ||||
|---|---|---|---|---|
  | ||||
Posted in reply to mipri  | On Friday, 28 February 2020 at 12:48:17 UTC, mipri wrote:
> On Friday, 28 February 2020 at 12:44:52 UTC, 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
>
> Well, that's the error. b has no effect, so there's
> no need for it, and it's likely written in error. It
> has no effect in C++ either, and you may get a warning:
>
> example.cc: In function 'int main()':
> example.cc:7:8: warning: statement has no effect [-Wunused-value]
>   for (b; b<3; b++){
Thanks, I didn't know that you don't need it there and can leave it out!
 | |||
Copyright © 1999-2021 by the D Language Foundation
 
Permalink
Reply