titli_juit wrote:
>      #include<stdio.h>
> int 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);
>                }    
>          
> return 0;
> }
> 
> the output is 16,21
> 
> can you please explain how am i getting this output? 

switch-case requires break statements before the next 'case' or the 
logic falls through to the next case statement which executes...and so on...

So:

When i = 0, i = i + 5 + 2 + 5 + 4;

Thus:
i = 0 + 5 + 2 + 5 + 4 = 16

i (16) is printed out.  The loop increments i by 1 (i.e. 17).  Then the 
switch-case executes.  Since i is 17, the default is executed and i 
becomes 21.

The actual output is '16,21,' (without quotes).  You forgot a comma.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to