Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 19, 2015 foreach loop | ||||
---|---|---|---|---|
| ||||
Is it possible to create a foreach loop with a breakstetemen? I mean something like that for the second loop where i want to break if element from: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; } |
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Monday, 19 October 2015 at 14:28:06 UTC, Namal wrote:
> Is it possible to create a foreach loop with a breakstetemen?
>
> I mean something like that for the second loop where i want to break if element from:
>
> int [] g = [9,15,21];
> int [] v = [2,3,5,7,8,9,11,13,17,19];
>
> foreach(j;1..10)
> for(int i = 0; v[i]<j;++i){
> do something;
> }
Of course. D uses even the same keyword for this as C does: break.
|
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | V Mon, 19 Oct 2015 14:28:03 +0000 Namal via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > Is it possible to create a foreach loop with a breakstetemen? > > I mean something like that for the second loop where i want to break if element from: > > int [] g = [9,15,21]; > int [] v = [2,3,5,7,8,9,11,13,17,19]; > > foreach(j;1..10) > for(int i = 0; v[i]<j;++i){ > do something; > } I am not sure what you exactly want, but you can break foreach statement: int [] g = [9,15,21]; int [] v = [2,3,5,7,8,9,11,13,17,19]; outerfor: foreach(j;1..10) for(int i = 0; v[i]<j;++i){ do something; if (iWantToBreak) break outerfor; } |
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On 20/10/15 3:28 AM, Namal wrote:
> Is it possible to create a foreach loop with a breakstetemen?
>
> I mean something like that for the second loop where i want to break if
> element from:
>
> int [] g = [9,15,21];
> int [] v = [2,3,5,7,8,9,11,13,17,19];
>
> foreach(j;1..10)
> for(int i = 0; v[i]<j;++i){
> do something;
> }
If you want to break a specific loop, use labels.
L1:
foreach(i; 0 .. 10) {
L2: foreach(j; 0 .. 10) {
// break L1;
// break L2;
// or
// break;
}
}
|
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rikki Cattermole | On Monday, 19 October 2015 at 14:43:04 UTC, Rikki Cattermole wrote:
> On 20/10/15 3:28 AM, Namal wrote:
>> Is it possible to create a foreach loop with a breakstetemen?
>>
>> I mean something like that for the second loop where i want to break if
>> element from:
>>
>> int [] g = [9,15,21];
>> int [] v = [2,3,5,7,8,9,11,13,17,19];
>>
>> foreach(j;1..10)
>> for(int i = 0; v[i]<j;++i){
>> do something;
>> }
>
> If you want to break a specific loop, use labels.
>
> L1:
> foreach(i; 0 .. 10) {
>
> L2: foreach(j; 0 .. 10) {
> // break L1;
>
> // break L2;
> // or
> // break;
> }
> }
Hey thanks, until now I just used bool variables as break flags.
|
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | Is it possible to use foreach backwards? foreach(int i;20..1) writeln(i); compiles but I get nothing. |
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote: > Is it possible to use foreach backwards? > yes http://dlang.org/statement.html#ForeachStatement http://dpaste.dzfl.pl/cf847a9e1595 |
October 19, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Monday, 19 October 2015 at 15:56:00 UTC, Namal wrote:
> Is it possible to use foreach backwards?
>
> foreach(int i;20..1)
> writeln(i);
>
> compiles but I get nothing.
foreach_reverse(i; 1 .. 20)
writeln(i);
Or:
import std.range : iota, retro;
foreach(i; iota(1, 20).retro)
writeln(i);
But if you really want the numbers 1 to 20, you should use 21 as the end value in both cases, as it's exclusive.
|
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | Hello guys, I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx. |
November 03, 2015 Re: foreach loop | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Tuesday, 3 November 2015 at 14:47:14 UTC, Namal wrote:
> Hello guys,
>
> I remember it is possible to get the index for each element in the foreach loop, but I forgot how to do it. Can you help me out please. Thx.
auto arr = ["Hello", "World"];
foreach(int idx, string str; arr){
writefln("%s = %s", idx, str);
}
|
Copyright © 1999-2021 by the D Language Foundation