Page 260 - PowerPoint Presentation
P. 260

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               Example of nested for loop:

                  for (int i=1; i<=5; i++) {    OUTPUT:        for (int i=1; i<=5; i++) {    OUTPUT:
                        for (int j = 1; j<=5; j++) {     *  *  *  *  *      for (int j = 1; j<=5; j++) {    1  1  1  1  1
                        System.out.println(“ * “);    *  *  *  *  *      System.out.println(i);     2  2  2  2  2
                  }                               *  *  *  *  *   }                            3  3  3  3  3
                        System.out.println();     *  *  *  *  *       System.out.println();     4  4  4  4  4
                  }                               *  *  *  *  *   }                            5  5  5  5  5


                       The nested for loop is a loop within a loop, an inner loop within the body of an outer
               loop. How this works is that the first pass of the outer loop triggers the inner loop, which
               executes to completion. Then the second pass of the outer lop triggers the inner loop again.
               This repeats until the outer loop finished

               Java Array
                       An array is a fixed-length structure hat stores multiple values of the same type. You
               can group values of the same type within arrays. An item in an array is called an element.
               Every element can be access via an index. The first element in an array is addressed via 0
               index, the second via 1 and so on.
               Here is the graphical representation on an array with size 10.



                          0     1     2     3    4     5     6     7     8     9
               Single Dimension Array
                       Array Declarations and Array Initialization
                       -   int num[] = new int[10]
                       -   float marks[] = new float[5]
                       -   String NameofStudents[] = new String[“ “, “ “]

                       Let’s see the simple example of Java array, where we are going to declare, instantiate,
                       initialize and traverse an array.
                  // Java program to illustrate how to declare, instantiate, initialize and traverse the Java Array


                  class TestArray {
                  public static void main (String[]args){

                  int arr[] = new int[5]; // variable arr declaration as an array that can store 5 integers (array 0-4)

                  arr[0] = 0;   // variable initialization
                  arr[1] = 10;
                  arr[2] = 20;
                  arr[3] = 30;
                  arr[4] = 40;

                  for (int i=0; i<arr.length; i++) { // displaying the array
                        System.out.println(arr[i]); // prints 0 10 20 30 40 downwards
                        }
                  }

                  }





                                                                                                            36
   255   256   257   258   259   260   261   262   263   264   265