SHARE
TWEET

OCA Nested Loops Array

AkanthaAnil Jul 19th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Test {
  2.      public static void main(String[] args) {
  3.          String [][] arr = {{"1", "2", "3"}, {"4", "5"}, {"6", "7"}};
  4.          for(int i = 0; i < arr.length; i++) {
  5.              for(int j = 0; j < arr[i].length; j++) {
  6.                  System.out.print(arr[i][j] + " ");
  7.                  if(arr[i][j].equals("2")) {
  8.                      break;
  9.  
  10. 1 2 3 4 5 6 7
  11.  
  12.  
  13. arr.length is 3, so outer loop executes 3 times. In 1st execution, i=0. For 1st execution of inner loop, i=0, j=0 and arr[0].length = 3. Inner loop runs for two iterations and breaks after printing "1 2 ". break; statement takes the control out of inner loop. Control goes to step expression (i++) of outer loop, i becomes 1. Inner loop prints "4 5 " on to the console. Outer loop executes one more time and inner loop prints "6 7 " on to the console. Hence output is "1 2 4 5 6 7 ".
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top