473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

I have a question


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 '07 #1
17 1595
ma**********@gmail.com 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?

--
Thad
May 21 '07 #2
On 21 mei, 08:03, manuthoma...@gmail.com 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
May 21 '07 #3
On May 21, 11:30 am, mdler <olaf.giezen...@gmail.comwrote:
On 21 mei, 08:03, manuthoma...@gmail.com 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 '07 #4
On May 21, 11:30 am, mdler <olaf.giezen...@gmail.comwrote:
On 21 mei, 08:03, manuthoma...@gmail.com 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 '07 #5
ma**********@gmail.com 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.
May 21 '07 #6
ma**********@gmail.com 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.
May 21 '07 #7
ma**********@gmail.com 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.

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.
May 21 '07 #8
ma**********@gmail.com wrote:
On May 21, 11:30 am, mdler <olaf.giezen...@gmail.comwrote:
>On 21 mei, 08:03, manuthoma...@gmail.com 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
May 21 '07 #9
none wrote:
ma**********@gmail.com 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.

May 21 '07 #10
On May 21, 12:43 pm, none <""matt\"@(none)"wrote:
manuthoma...@gmail.com wrote:
On May 21, 11:30 am, mdler <olaf.giezen...@gmail.comwrote:
On 21 mei, 08:03, manuthoma...@gmail.com 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 '07 #11
On 21 May, 07:03, manuthoma...@gmail.com 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 '07 #12
Thad Smith <Th*******@acm.orgwrites:
ma**********@gmail.com 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.
May 21 '07 #13
Richard <rg****@gmail.comwrites:
Thad Smith <Th*******@acm.orgwrites:
>ma**********@gmail.com 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) ks***@mib.org <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"
May 21 '07 #14
Martin Ambuhl <ma*****@earthlink.netwrites:
ma**********@gmail.com 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) ks***@mib.org <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"
May 21 '07 #15
Richard wrote:
Thad Smith <Th*******@acm.orgwrites:
>ma**********@gmail.com 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
beginning. Just follow the operation.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 22 '07 #16
On Mon, 21 May 2007 22:54:10 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>Richard wrote:
>Thad Smith <Th*******@acm.orgwrites:
>>ma**********@gmail.com 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
beginning. Just follow the operation.
I think you meant "j" instead of "i".

--
jay
May 22 '07 #17
On May 21, 11:42 am, manuthoma...@gmail.com wrote:
On May 21, 11:30 am, mdler <olaf.giezen...@gmail.comwrote:
On 21 mei, 08:03, manuthoma...@gmail.com 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.

May 22 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.