Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

I have a question

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

--
Thad
 
Reply With Quote
 
 
 
 
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;

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
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.


 
Reply With Quote
 
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.

 
Reply With Quote
 
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.
 
Reply With Quote
 
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.
 
Reply With Quote
 
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.

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.
 
Reply With Quote
 
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
 
Reply With Quote
 
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.

 
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