Velocity Reviews > I have a question

# I have a question

Guest
Posts: n/a

 05-21-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have a question:
>
> What will be the value of j in the following code? and why is it so?
>
> int i;
> for( i = 0; i < 1; i++ )
> {
> switch( i )
> {
> case 0: i += 5;
> case 3: i += 3;
> case 5: i += 5;
> break;
> }
> }
> int j = i;

For C90, nothing: it is an error. For C99, what do you think?

--

manuthomas23@gmail.com
Guest
Posts: n/a

 05-21-2007

I have a question:

What will be the value of j in the following code? and why is it so?

int i;
for( i = 0; i < 1; i++ )
{
switch( i )
{
case 0: i += 5;
case 3: i += 3;
case 5: i += 5;
break;
}
}
int j = i;

mdler
Guest
Posts: n/a

 05-21-2007
On 21 mei, 08:03, (E-Mail Removed) wrote:
> I have a question:
>
> What will be the value of j in the following code? and why is it so?
>
> int i;
> for( i = 0; i < 1; i++ )
> {
> switch( i )
> {
> case 0: i += 5;

i = 5 here (no break)
> case 3: i += 3;

i = 8 here (no break)
> case 5: i += 5;

i = 13 here
> break;
> }
> }

i = 14 here (13 + 1)
> int j = i;

so j = 14

Greetings Olaf

manuthomas23@gmail.com
Guest
Posts: n/a

 05-21-2007
On May 21, 11:30 am, mdler <(E-Mail Removed)> wrote:
> On 21 mei, 08:03, (E-Mail Removed) wrote:> I have a question:
>
> > What will be the value of j in the following code? and why is it so?

>
> > int i;
> > for( i = 0; i < 1; i++ )
> > {
> > switch( i )
> > {
> > case 0: i += 5;

>
> i = 5 here (no break)> case 3: i += 3;
>
> i = 8 here (no break)> case 5: i += 5;
> i = 13 here
> > break;
> > }
> > }

>
> i = 14 here (13 + 1)
>
> > int j = i;

>
> so j = 14
>
> Greetings Olaf

what i was asking is when it falls through, how can case 3: execute?
value of i is 5 at that time. Or it just executes all statements until
a next break is seen, regardless of the case statements? I know its a
basic question. I just want an explanation.

Thanks.

manuthomas23@gmail.com
Guest
Posts: n/a

 05-21-2007
On May 21, 11:30 am, mdler <(E-Mail Removed)> wrote:
> On 21 mei, 08:03, (E-Mail Removed) wrote:> I have a question:
>
> > What will be the value of j in the following code? and why is it so?

>
> > int i;
> > for( i = 0; i < 1; i++ )
> > {
> > switch( i )
> > {
> > case 0: i += 5;

>
> i = 5 here (no break)> case 3: i += 3;
>
> i = 8 here (no break)> case 5: i += 5;
> i = 13 here
> > break;
> > }
> > }

>
> i = 14 here (13 + 1)
>
> > int j = i;

>
> so j = 14
>
> Greetings Olaf

what i was asking is when it falls through, how can case 3: execute?
value of i is 5 at that time. Or it just executes all statements until
a next break is seen, regardless of the case statements? I know its a
basic question. I just want an explanation.

Thanks.

none
Guest
Posts: n/a

 05-21-2007
(E-Mail Removed) wrote:
> I have a question:
>
> What will be the value of j in the following code? and why is it so?
>
> int i;
> for( i = 0; i < 1; i++ )
> {
> switch( i )
> {
> case 0: i += 5;
> case 3: i += 3;
> case 5: i += 5;
> break;
> }
> }
> int j = i;
>

13.

none
Guest
Posts: n/a

 05-21-2007
(E-Mail Removed) wrote:
> I have a question:
>
> What will be the value of j in the following code? and why is it so?
>
> int i;
> for( i = 0; i < 1; i++ )
> {
> switch( i )
> {
> case 0: i += 5;
> case 3: i += 3;
> case 5: i += 5;
> break;
> }
> }
> int j = i;
>

Ooops. Try 14.

Martin Ambuhl
Guest
Posts: n/a

 05-21-2007
(E-Mail Removed) wrote:
> I have a question:
>
> What will be the value of j in the following code? and why is it so?
>
> int i;
> for( i = 0; i < 1; i++ )
> {
> switch( i )
> {
> case 0: i += 5;
> case 3: i += 3;
> case 5: i += 5;
> break;
> }
> }
> int j = i;
>

It is considered a good thing to post compilable code. It would not
have been difficult for you to turn the above snippet into such by
simply embedding the above with in an
int main(void)
{
/* the above */
return 0;
}
It is also considered a good thing to have code that is compilable with
a C89 compiler, since very few C compilers are actually C99 compilers.
That means declaring variables at the top of a block and not after
executable statements. This could have been easily done by declaring j
near the declaration of i.

i is set equal to 0 at the top of the loop.
It then becomes
5 (0 + 5)
8 (5 + 3)
13 (8 + 5)
14 (13 + 1)
and j is set to 14.
Only a Pascal programmer would be confused.

none
Guest
Posts: n/a

 05-21-2007
(E-Mail Removed) wrote:
> On May 21, 11:30 am, mdler <(E-Mail Removed)> wrote:
>> On 21 mei, 08:03, (E-Mail Removed) wrote:> I have a question:
>>
>>> What will be the value of j in the following code? and why is it so?
>>> int i;
>>> for( i = 0; i < 1; i++ )
>>> {
>>> switch( i )
>>> {
>>> case 0: i += 5;

>> i = 5 here (no break)> case 3: i += 3;
>>
>> i = 8 here (no break)> case 5: i += 5;
>> i = 13 here
>>> break;
>>> }
>>> }

>> i = 14 here (13 + 1)
>>
>>> int j = i;

>> so j = 14
>>
>> Greetings Olaf

>
> what i was asking is when it falls through, how can case 3: execute?
> value of i is 5 at that time. Or it just executes all statements until
> a next break is seen, regardless of the case statements? I know its a
> basic question. I just want an explanation.
>

Yes, it just executes all statements until the next break is seen. This
is, for example, how Duff's device works. Duff has observed that his
device "forms some sort of argument in [the debate over C's default
fall-through behaviour], but I'm not sure whether it's for or against".

You can also use this behaviour in your own code when you have identical
action to be performed for a number of different cases.

Cheers,
mvdw

Martin Ambuhl
Guest
Posts: n/a

 05-21-2007
none wrote:
> (E-Mail Removed) wrote:
>> I have a question:
>>
>> What will be the value of j in the following code? and why is it so?
>>
>> int i;
>> for( i = 0; i < 1; i++ )
>> {
>> switch( i )
>> {
>> case 0: i += 5;
>> case 3: i += 3;
>> case 5: i += 5;
>> break;
>> }
>> }
>> int j = i;
>>

>
> 13.

Bzzt! Better luck next time.