0

*When i=2 , j=0 what will be the output?*

/* In my logic it will go switch(1) means case 1:() which is empty so it should go to default: (1+3)= 4 but on the output I am getting 3 why? And I have confusion ,here I can see when i=4 , j=1 , switch (4+1-1)=4, ( case 4: ) which is not present on the code. So now it should go to default: (5+3)=8 But on the output I am getting 7 why? Can you please explain it? */ Output: 1 3 5 7 here I am getting 8 from my logic of output tracing 9 12 14 17 20 23 x = 23Press any key to continue . . . */

4th Jan 2022, 5:26 PM
Rafid
Rafid - avatar
2 Respostas
+ 1
switch works like it first check for the matching case , if any match, then executes that case and does fall through means, that executes next succeding cases also, until a break statement encounters, or till end of switch. If there is no match then executes default case. For case 1, match then executes case1, case2, case3, now stops as it has break statement. In your code, when, i=1, j=0 does, case 0: x=x+1=>x=1 Next case 1: when I=2 and j=0 and x=x+2 so x=3 .. .. .. .. Hope you can find next... Hope it helps..
4th Jan 2022, 5:45 PM
Jayakrishna 🇮🇳
0
Contrary to what you think, you have defined the "case 1". Cases 1, 2 and 3 give the same result, since they are defined together without a "break" between them. if what you expect is for it to go to default, you must remove "case 1:"
4th Jan 2022, 5:42 PM
CGM
CGM - avatar