Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39955
| From | Michael Jung <miju@golem.phantasia.org> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: switch is shared declaration |
| Date | 2023-05-28 12:57 +0200 |
| Message-ID | <87jzws7mfh.fsf@golem.phantasia.org> (permalink) |
| References | <af8aa265-141f-4887-8bae-83a9b2a0f9c1n@googlegroups.com> <u4lfmo$311sp$1@dont-email.me> <72906bb1-301c-4729-be5b-e037a71286b9n@googlegroups.com> |
"e.d.pro...@gmail.com" <e.d.programmer@gmail.com> writes:
>> > I don't use a lot of switches so I hadn't realized this but:
>> > this is invalid:
>> > if (1==1) {
>> > String s;
>> > } else {
>> > s = "1";
>> > }
>> > but this is fine:
>> > switch (1) {
>> > case -1:
>> > String s;
>> > break;
>> > case 1:
>> > s = "1";
>> > break;
>> > }
>> The entire switch sort of has to be a single scope due to
>> fall through (no break) being valid.
>> Arne
> I guess it makes sense. You could call it a soft scope? In the if
> statement you get an undeclared variable error as it knows the if
> block will never execute with the else condition. In the switch
> statement it also will never execute both the declaration and the
> assignment, unless you remove the break. Since the break it optional,
> the compile sees it as one, though it also executes fine.
I wouldn't call it anything scope. Remember that you can create a lot of
mischief with labels and continue and breaks. And that needs to remain
compatible. And just because the compiler knows (can know) a block
cannot be reached, does not allow you to add illegal, but parseable
statements, e.g. "else { 0 = 1; }".
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
switch is shared declaration "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-05-24 05:15 -0700
Re: switch is shared declaration Arne Vajhøj <arne@vajhoej.dk> - 2023-05-24 12:57 -0400
Re: switch is shared declaration Arne Vajhøj <arne@vajhoej.dk> - 2023-05-24 13:16 -0400
Re: switch is shared declaration "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-05-24 11:05 -0700
Re: switch is shared declaration Michael Jung <miju@golem.phantasia.org> - 2023-05-28 12:57 +0200
Re: switch is shared declaration Marcel Mueller <news.5.maazl@spamgourmet.org> - 2023-05-28 13:31 +0200
Re: switch is shared declaration Eric Sosman <esosman@comcast-dot-net.invalid> - 2023-05-28 08:36 -0400
Re: switch is shared declaration Marcel Mueller <news.5.maazl@spamgourmet.org> - 2023-05-28 15:16 +0200
Re: switch is shared declaration Arne Vajhøj <arne@vajhoej.dk> - 2023-05-28 13:42 -0400
Re: switch is shared declaration Arne Vajhøj <arne@vajhoej.dk> - 2023-05-28 13:43 -0400
csiph-web