**When i=4 , j=1 , switch (4+1-1)=4, ( case 4: ) what will be the right output? ** | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

**When i=4 , j=1 , switch (4+1-1)=4, ( case 4: ) what will be the right output? **

/* 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 instead of 8 why? Can you please explain it? */ #include <stdio.h> main() { int i, j, x = 0; for (i = 0; i < 5; ++i) for (j = 0; j < i; ++j) { switch (i + j - 1) { case -1: case 0: x += 1; break; case 1: case 2: case 3: x += 2; break; default: x += 3; break; } printf("%d\n", x); } printf("\nx = %d", x); } Output: 1 3 5 7 9 12 14 17 20 23 x = 23Press any key to continue . . . */ https://code.sololearn.com/cYMktBgOzJ6h/?ref=app

4th Jan 2022, 6:07 PM
Rafid
Rafid - avatar
3 Answers
+ 3
Yes it goes to default but there x is already 14 so output 14+3=17 when i = 1, j= 0 , case = 0, x=x+1=1 when i = 2, j= 0 , case = 1, x=x+2=>x=3 when i = 2, j= 1 , case = 2, x=x+2=>x=5 when i = 3, j= 0 , case = 2, x=x+2=>x=7 when i = 3, j= 1 , case = 3, x=x+2=>x=9 when i = 3, j= 2 , case = 4, x=x+3=>x=9+3=12 when i = 4, j= 0 , case = 3, x=x+2=>x=14 when i = 4, j= 1 , case = 4, x=x+3=>14+3=17 when i = 4, j= 2 , case = 5, x=x+3=>x=20 when i = 4, j= 3 , case = 6, x=x+3=>x=23 RRR
4th Jan 2022, 6:20 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 3
Where is code!? continue with last question.. instead of new! don't change original post as it confuses readers... edit: added.
4th Jan 2022, 6:10 PM
Jayakrishna ๐Ÿ‡ฎ๐Ÿ‡ณ
+ 1
Insert your code! For that you must have it saved in the "playground" Using "printf" functions you can see the values โ€‹โ€‹of j and i at each moment to understand what is happening, like this: https://code.sololearn.com/cqa2gdb83AF5/?ref=app
4th Jan 2022, 6:13 PM
CGM
CGM - avatar