I have a question

T

Thad Smith

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

manuthomas23

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;
 
M

mdler

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
 
M

manuthomas23

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

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

i = 14 here (13 + 1)


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.
 
M

manuthomas23

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

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

i = 14 here (13 + 1)


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.
 
N

none

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.
 
N

none

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.
 
M

Martin Ambuhl

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.

The answer to your question is, of course, trivially obvious.
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.
 
N

none

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
 
M

manuthomas23

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 :)
 
M

mark_bluemel

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.
 
R

Richard

Thad Smith said:
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.
 
K

Keith Thompson

Richard said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
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.)
 
J

jaysome

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
beginning. Just follow the operation.

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

BiGYaN

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top