+ 2

Switch statement inside a nested loop. Dealing with multi dimensional array

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x[][] = { {1, 3, 5, 7, 9}, {2, 4, 6, 8, 0}, {12, 23, 34, 45, 56} }; for (int i = 0; i <= 2; i++) { for (int j = 0; j <= 4; j++) { switch (i) { case 0: System.out.print(x[i][j] + "\t"); if (j == 4) { System.out.println(); } break; case 1: System.out.print(x[i][j] + "\t"); if (j == 4) { System.out.println(); } break; }//endSwitch } }//end i int a[] = new int[2]; System.out.print("\nColumn (Horizontal): "); a[0] = input.nextInt(); System.out.print("Row (Vertical): "); a[1] = input.nextInt(); System.out.println("Number: " + x[a[0]][a[1]]); } } Can someone explain on why does every time i remove the "break" on case's body, the output will be doubled? Like, if i remove the break in "case 0" the out put will be 1 1 3 3 5 5 7 7 9 9 instead of 1 3 5 7 9. thank you.

6th Feb 2021, 5:21 AM
Kakai
Kakai - avatar
13 odpowiedzi
+ 5
Kakai Since there is no break in case 0 so your code will execute like this System.out.print(x[i][j] + "\t"); System.out.print(x[i][j] + "\t"); if (j == 4) { System.out.println(); } break; So according to this 1 1 then break 3 3 then break 5 5 then break And so on.
6th Feb 2021, 6:13 AM
A͢J
A͢J - avatar
+ 7
Kakai Break use to stop program there and move on for next checking. In switch case if you don't use break then next case will execute by default. If you don't use break in case 0 then case 1 will execute and so on.
6th Feb 2021, 5:50 AM
A͢J
A͢J - avatar
+ 5
Kakai It's working fine. What you get error?
6th Feb 2021, 5:45 AM
A͢J
A͢J - avatar
+ 5
Kakai No error but because of no break in case 0 output would be different https://code.sololearn.com/cojmY3INDUBF/?ref=app
6th Feb 2021, 5:56 AM
A͢J
A͢J - avatar
+ 4
Kakai Just take a paper and pen and see why this happens.
6th Feb 2021, 6:14 AM
A͢J
A͢J - avatar
+ 3
Do you know why we use break statement in switch ..? Break statement is for control the flow of execution. if you remove break statement at case 0 , then it doesn't stop, it continuing execution remaining statements until break appears.
6th Feb 2021, 5:54 AM
NavyaSri
NavyaSri - avatar
+ 2
Kakai Answer for your question why it's printing double times means... You write exactly same code in case 0 and case 1 to read array elements. So , when you didn't use break in case 0 then it will execute case1 too ..which has same code as case 0. it's printed whatever case 0 printed.
6th Feb 2021, 6:07 AM
NavyaSri
NavyaSri - avatar
+ 2
Kakai I am girl. by the way pleasure to help you.
6th Feb 2021, 6:13 AM
NavyaSri
NavyaSri - avatar
+ 2
Navya Sri and I Am AJ ! Thank you guys, ❤️. I got it now. Been tryin' to solve this for 1h hahaha.
6th Feb 2021, 6:17 AM
Kakai
Kakai - avatar
0
Anyways, i got what i want to create but i just want to understand that kind of error. Thank you.
6th Feb 2021, 5:22 AM
Kakai
Kakai - avatar
0
Yeah i know but why does when i remove the break in case's 0 the out put will be doubled? I want to understand that badly. Out put with break (fine) : 1 3 5 7 9 Out put without break: 1 1 3 3 5 5 7 7 9 9
6th Feb 2021, 5:47 AM
Kakai
Kakai - avatar
0
Try to run it without break in case 0, it'll get some logical errors.
6th Feb 2021, 5:49 AM
Kakai
Kakai - avatar
0
I Am AJ ! Look at the out put, you got 1 1 3 3 5 5 7 7 9 9. I want to understand that. Supposed to be if it will run two times, u must get 1 3 5 7 9 1 3 5 7 9. But in that case, why 1 1 3 3 5 5 7 7 9 9?
6th Feb 2021, 6:08 AM
Kakai
Kakai - avatar