Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I have a question

Reply
Thread Tools

I have a question

 
 
manuthomas23@gmail.com
Guest
Posts: n/a
 
      05-21-2007
On May 21, 12:43 pm, none <""matt\"@(none)"> wrote:
> (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- Hide quoted text -
>
> - Show quoted text -



Thank you guys

 
Reply With Quote
 
 
 
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      05-21-2007
On 21 May, 07: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;
> 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.

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      05-21-2007
Thad Smith <(E-Mail Removed)> writes:

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


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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-21-2007
Richard <(E-Mail Removed)> writes:
> Thad Smith <(E-Mail Removed)> writes:
>> (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?

>
> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-21-2007
Martin Ambuhl <(E-Mail Removed)> writes:
> (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;
> }

[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) (E-Mail Removed) <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"
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      05-22-2007
On Mon, 21 May 2007 22:54:10 -0400, CBFalconer <(E-Mail Removed)>
wrote:

>Richard wrote:
>> Thad Smith <(E-Mail Removed)> writes:
>>> (E-Mail Removed) 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
 
Reply With Quote
 
BiGYaN
Guest
Posts: n/a
 
      05-22-2007
On May 21, 11:42 am, (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.
>
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there someone have written some programs about RSA-1024?I have some question. dolphin C++ 3 03-09-2007 02:47 PM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
a question: Do I have to have some work experience before taking the exam jessi MCAD 3 04-04-2005 10:04 AM
do i have to have.... =?Utf-8?B?amFrZQ==?= Wireless Networking 1 03-11-2005 06:05 PM
Warehouse Dell dimension 3000 advert. This is a question so TNO doesn't have to download this message and read it to waste his time reading messages that I could have possibly got the answer .. (damn, run out of space in the subject line!) cowboyz NZ Computing 68 12-21-2004 02:19 PM



Advertisments