# I have a question

Discussion in 'C Programming' started by Thad Smith, May 21, 2007.

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?

--

2. ### Guest

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;

, May 21, 2007

3. ### mdlerGuest

On 21 mei, 08:03, 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

mdler, May 21, 2007
4. ### Guest

On May 21, 11:30 am, mdler <> wrote:
> On 21 mei, 08:03, 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.

, May 21, 2007
5. ### Guest

On May 21, 11:30 am, mdler <> wrote:
> On 21 mei, 08:03, 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.

, May 21, 2007
6. ### noneGuest

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, May 21, 2007
7. ### noneGuest

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.

none, May 21, 2007
8. ### Martin AmbuhlGuest

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.

Martin Ambuhl, May 21, 2007
9. ### noneGuest

wrote:
> On May 21, 11:30 am, mdler <> wrote:
>> On 21 mei, 08:03, 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

none, May 21, 2007
10. ### Martin AmbuhlGuest

none wrote:
> 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.

Martin Ambuhl, May 21, 2007
11. ### Guest

On May 21, 12:43 pm, none <""matt\"@(none)"> wrote:
> wrote:
> > On May 21, 11:30 am, mdler <> wrote:
> >> On 21 mei, 08:03, 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- Hide quoted text -
>
> - Show quoted text -

Thank you guys

, May 21, 2007
12. ### Guest

On 21 May, 07:03, 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;

I'm surprised so many people answered. This looks rather like homework
to me...

A little time spent with a decent reference text (for example K&R2)
should have answered this for the OP.

, May 21, 2007
13. ### RichardGuest

> 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?

This not a c90 group. It is a C group. Not even the std group.

Clearly it is C99 example in this case.

Why not explain to him how the result is calculated? This is a help
group after all. If he knew he wouldn't have asked. Sigh. Why are so
many people in this group so keen on showing off and being complete and
utter arrogant twits?

To the op : look up switch statements and the concept of "break". Other
posts have told you the answer.

Richard, May 21, 2007
14. ### Keith ThompsonGuest

Richard <> writes:
>> 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?

>
> This not a c90 group. It is a C group. Not even the std group.
>
> Clearly it is C99 example in this case.

It's either a C99 example or an incorrect C90 example (since a number
of non-C99 compilers support some C99 features as extensions, the
latter is a real possibility). Nobody said this was a C90 group. And
Thad merely pointed out that it's an error in C90; what's wrong with
that?

> Why not explain to him how the result is calculated? This is a help
> group after all. If he knew he wouldn't have asked. Sigh. Why are so
> many people in this group so keen on showing off and being complete and
> utter arrogant twits?

This is a help group, not a do-my-homework group. The OP's question
looks very much like a homework problem. In any case, it can be
answered either by trying the code (assuming an unbroken
implementation), or by manually tracing it with an understanding of
how for and switch statements work. The latter is the best way to
actually understand what's going on, and to learn the lessons that the
question tries to teach.

> To the op : look up switch statements and the concept of "break". Other
> posts have told you the answer.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 21, 2007
15. ### Keith ThompsonGuest

Martin Ambuhl <> writes:
> 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;
> }

[snip]

It can also be helpful to add output statements so you can see what's
going on as the program runs.

For the purposes of this exercise, it would be best to trace through
the original code manually, and perhaps to verify the answer by adding
a printf("j = %d\n", j) statement after the end of the code fragment.
This problem is really simple enough that more intrusive methods
aren't necessary.

But since the answer has already been posted, here's a version of the
code as a complete program that shows what happens at each step:

#include <stdio.h>
int main(void)
{
int i, j;
for( printf("loop1, i = 0\n"), i = 0;
printf(i < 1 ? "loop2, continuing\n" : "loop2, quitting\n"), i < 1;
printf("loop3, i++\n"), i++ )
{
printf("Top of loop, i = %d\n", i);
switch( i )
{
case 0: i += 5; printf("i += 5, fall through\n");
case 3: i += 3; printf("i += 3, fall through\n");
case 5: i += 5; printf("i += 5, break out of switch\n");
break;
}
printf("Bottom of loop, i = %d\n", i);
}
j = i;
printf("j = %d\n", j);
return 0;
}

I've also made the code C90-compliant by moving the declaration of j.

Each of the three parts between the parentheses of a for statement is
an expression. A function call is also an expression. Here, I use
the comma operator to insert a printf() call into each expression; the
value returned by printf() is discarded, and the new expression yields
the same result as the original one would have.

(Using a debugger would be another way to trace the execution of the
code, but different debuggers work differently, and the details are
off-topic.)

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 21, 2007
16. ### jaysomeGuest

On Mon, 21 May 2007 22:54:10 -0400, CBFalconer <>
wrote:

>Richard wrote:
>>> wrote:
>>>>
>>>> What will be the value of j in the following code? and why is
>>>> it so?
>>>>
>>>> int i;
>>>> for (i = 0; i < 1; i++) { /* modified, unchanged */
>>>> 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?

>>
>> This not a c90 group. It is a C group. Not even the std group.
>>
>> Clearly it is C99 example in this case.
>>
>> Why not explain to him how the result is calculated? This is a
>> help group after all. If he knew he wouldn't have asked. Sigh.
>> Why are so many people in this group so keen on showing off and
>> being complete and utter arrogant twits?
>>
>> To the op : look up switch statements and the concept of "break".
>> Other posts have told you the answer.

>
>Regardless, the end value of i will be 14, and the same for j. The
>code is C90 compliant if the declaration of i is placed at the

I think you meant "j" instead of "i".

--
jay

jaysome, May 22, 2007
17. ### BiGYaNGuest

On May 21, 11:42 am, wrote:
> On May 21, 11:30 am, mdler <> wrote:
>
>
>
> > On 21 mei, 08:03, 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.

It just executes all statements until a next break is seen, regardless
of the case statements? .... yup you are correct .... it does just
that.

BiGYaN, May 22, 2007