http://www.technicalpage.net/search/label/SQL

>> " for " loop in Java / Selenium


" for  "  loop in Java / Selenium

              for (int i = 0; i <= 5; i++) {
                     System.out.println("The value of i is : " + i);
              }
Answer :
The value of i is : 0
The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 5

              for (int i = 0; i <= 5; i = i + 2) {
                     System.out.println("The value of i is : " + i);

              }
Answer :
The value of i is : 0
The value of i is : 2
The value of i is : 4

              for (int i = 5; i > 0; i--) {
                     System.out.println("The value of i is : " + i);

              }
Answer :
The value of i is : 5
The value of i is : 4
The value of i is : 3
The value of i is : 2
The value of i is : 1

              for (int i = 5; i > 0; i = i - 2) {
                     System.out.println("The value of i is : " + i);

              }
Answer :
The value of i is : 5
The value of i is : 3
The value of i is : 1


" break  " in the for  loop 

If you want to break/stop the for loop in certain condition then you can use "break":

               for (int i = 0; i <= 5; i++) {
                   System.out.println("The value of i is : " + i);
                  
                   if(i==3){
                        break;
                   }       

            }
Answer :
The value of i is : 0
The value of i is : 1
The value of i is : 2
The value of i is : 3


" continue  " in the for  loop 

If you want to continue the for loop in certain condition then you can use "continue":

               for (int i = 0; i <= 5; i++) {
                   System.out.println("The value of i is : " + i);
                  
                   if(i==3){
                        continue;
                   }       

            }
Answer :
The value of i is : 0
The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 5
 
 
Another way of writing for loop:
 

             //Array

             String[] str = {"TX", "NY", "CA"};

             System.out.println("-------Array using For Loop--------");

             for (int i = 0; i < alist.size(); i++) {

                    System.out.println(str[i]);

             }

 

             // for loop another way

             System.out.println("-------Array using For Loop with semicolon--------");

             for (String x : str) {

                    System.out.println(x);

             }

            

             //Arraylist

             List<Integer> arlist = Arrays.asList(5,6,7);// ArrayList

             // 1. for loop

             System.out.println("-------ArrayList using For Loop--------");

             for (int i = 0; i < alist.size(); i++) {

                    System.out.println(arlist.get(i));

             }

 

             // for loop another way

             System.out.println("-------ArrayList using For Loop with semicolon--------");

             for (Integer x : arlist) {

                    System.out.println(x);

             }

 

Output:

-------Array using For Loop--------

TX

NY

CA

-------Array using For Loop with semicolon--------

TX

NY

CA

-------ArrayList using For Loop--------

5

6

7

-------ArrayList using For Loop with semicolon--------

5

6

7

No comments:

Post a Comment