Question:What will be output when you will execute following c code?#include<stdio.h>
void main(){
static int i;
int j;
for(j=0;j<=5;j+=2)
switch(j){
case 1: i++;break;
case 2: i+=2;
case 4: i%=2;j=-1;continue;
default: --i;continue;
}
printf("%d",i);
}
Choose all that apply:
+ ExplanationIn first iteration of for loop:
j = 0
So, control will come to default,
i = -1
Due to continue keyword program control will move to beginning of for loop
In second iteration of for loop:
j =2
So, control will come to case 2,
i+=2
i = i+2 = -1 +2 =1
Then come to case 4,
i%=2
i = i%2 = 1%2 = 1
j= -1
Due to continue keyword program control will move to beginning of for loop
In third iteration of for loop:
j = -1 +2 =1
So, control will come to case 1
i = 2
In the fourth iteration of for loop:
j = 1+ 2 =3
So, control will come to default,
so i = 1
In the fifth iteration of for loop:
j = 3 + 2 =5
So, control will come to default,
so i = 0
In the sixth iteration of for loop:
j = 5 + 2 =7
Since loop condition is false. So control will come out of the for loop.