Syntax for checking if an element exists in a list
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 5 05:36:17 PST 2015
On Thursday, 5 February 2015 at 13:31:21 UTC, Nicholas Wilson
wrote:
> On Thursday, 5 February 2015 at 12:31:31 UTC, Stéphane wrote:
>> Syntax for checking if an element exists in a list
>>
>> Hello,
>>
>> I would like to know if there is a better (easier to wite,
>> easier to read, easier to understand) way to check if an
>> element
>> (string) is
>> in a list of strings.
>>
>> Here are the possibilities I see today, as someone who is new
>> to D:
>>
>> 1) using a switch
>>
>> char [] myopt=get_an_option();
>>
>> switch(myopt) {
>> case "opt1", "opt2", "opt3":
>> do_A();
>> break;
>> case "opt4":
>> do_B();
>> break;
>> default:
>> do_C();
>> }
>>
> Note that D does NOT have default fall through. i.e.
> switch(myopt)
> {
> case "opt1", "opt2", "opt3":
> do_A();
> break;
> case "opt4":
> do_B();
> break;
> default:
> do_C();
> }
> is equivalent to
> switch(myopt)
> {
> case "opt1", "opt2", "opt3":
> do_A();
> case "opt4":
> do_B();
> default:
> do_C();
> }
> to enable fall through use goto case; / goto case n; to fall
> through once (to the next case) and to case n (where n is the
> case label identifier i.e. 5 or "foo" or mylabel: )
> switch(myopt)
> {
> case "opt1", "opt2", "opt3":
> do_A();
> goto case;
> case "opt4":
> do_B();
> default:
> do_C();
> }
>>
>> PPS: Where should I post, when I have such questions and
>> problems
>> as the one in my previous paragraph? I did not find any "meta"
>> forum.
>
> learn is the correct place for such questions and in general
> for questions about how to do something or enquire as to why
> something is not working or not working the way you think it
> should.
>
> Announcement is for announcements.
> debuggers and ide are for debuggers and ide.
>
> digitalmars.D is for discussion about the language the
> libraries and links to interesting stuff (for some value of
> stuff)
>
> the D.gnu and D.ldc are for discussions/queries about gdc and
> ldc (the gcc and llvm backend compilers)
addendum
case n:
...
break;
exists only for the ease of transliteration of C(++) where the
break statement is required.
How ever
break label;
is still useful and used in D. usually used to break from
multiple in a single statement.
More information about the Digitalmars-d-learn
mailing list