views:

164

answers:

5
main()
{
    int i=0;
    for(i=0;i<20;i++)
    {
        switch(i)

        case 0:
            i+=5;
        case 1:
            i+=2;
        case 5:
            i+=5;
        default
            i+=4;
            break;
    }
    printf("%d,",i);

}

}

this is frm aptitude question....plz explain hw this works???

+3  A: 

Existing program does not work. It has some syntax errors:

  • No { after switch
  • No : after default

.

                                   //Iteration-0       Iteration-1
for(i=0;i<20;i++) {                //   i is 0          inc i, i is 17           
            switch(i) {            //     0              17
                    case 0:i+=5;   //     5              skip case
                    case 1:i+=2;   //     7              skip case
                    case 5:i+=5;   //     12             skip case
                    default:i+=4;  //     16              21
                            break; //     break now       break now
            }
            printf("%d,",i);       //     print 16,       print 21,
    }

Final output is 16,21,

Since there is no break after your cases the control falls through.

codaddict
Also, it needs an int before main
Etienne de Martel
@Etienne: The return type is assumed to be `int` if nothing is specified.
casablanca
@casablanca : `The return type is assumed to be int if nothing is specified.` **No**, `implicit int` is not a part of C99.
Prasoon Saurav
@casablanca: Even in ansi c the assumed typing of main was a sop to legacy code. Relying on it was bad practice. Unless you're playing code golf type what you mean.
dmckee
A: 

Starting from zero and continuing until i is 20 or more, examine i and perform the following operations:

if i==0, add 5, then 2, then 5, then 4.

else if i==1, add 2, then 5, then 4.

else if i==5, add 5, then 4.

else, add 4 to i.

display the result to the console.

increment i by 1.

KeithS
After the first iteration, i is equal to 16, as the first number in your answer shows. Why do you think 'i' magically becomes 1 on the next iteration?
Sjoerd
Edited. Now please remove your downvote.
KeithS
+3  A: 

Possibly you are being testing to see if you understand the timing of the evaluation of the various bits of the for(...;...;...) expression.

Short version:

  1. The first expression is evaluated only once before any code in the loop is executed
  2. the middle expression is evaulated on each iteration before any code from the loop is executed and the loop will terminated if the expression evaluates as false
  3. The last expression is evaluated on each iteration after the code in the loop is performed, but before the middle expression is next evaluated.

Try it from there.

dmckee
Thanks for giving the only answer here that is reasonable for a "Homework" answer. I'd like to vote all the explicit answers into the ground but it seems like people just don't get the homework tag so it's kinda pointless. +1 for you anyway.
Bill K
+1  A: 

After you correct the syntax like this:

int i=0;
    for(i=0;i<20;i++)
    {
        switch(i)
    {
        case 0:
            i+=5;
        case 1:
            i+=2;
        case 5:
            i+=5;
    default:
            i+=4;
            break;
    }
    printf("%d,",i);}

iteration 1:

  • i = 0
  • case 0 -> i = 5 (i+=5)
  • case 1 (there's no break after case 0)-> i = 7 (i+=2)
  • case 5 (there's no break after case 1)->i=12 (i+=5)
  • default (there's no break after case 5)->i=16 (i+=4)
  • print -> 16

iteration 2:

  • i = 17 (because of the i++)
  • default (no other case matched before) -> i = 21 (i+=4)
  • print -> 21

iteration 3:

  • stop (because 21 > 20)

so the program will show you 16,21,

Alin
yaa this exactly what i thought .i also thought that there will be syntex error.but given answer is 16,21.thats why i asked....
alka pandey
A: 

Syntax errors aside: because there are no breaks on the individual case statements in the switch clause, the case bodies of the first matching line and all subsequent case statements will be executed in turn.

i = 0 : matches case 0, does i+=5, then i+=2, then i+=5, then i+=4 - prints 16

i is then compared to 20 as part of the for loop, it's less so it continues.

i is then incremented to 17 as part of the for loop.

i = 17 : matches default, so i+=4 - prints 21

i is then compared to 20 as part of the for loop, it's greater so the loop exits.

Trevor Tippins