Learn the Use of Loops in Java - Used for Selenium Java Course

Loops in Java : In the previous post we have discussed in detail about OOPs concepts and the advantage of Object Oriented programming over procedure oriented programming. In the same post we have discussed about the advantages of OOPs concepts in Selenium.

Although Selenium supports many programming languages including Java, C#, Ruby, Python, PHP etc, learning basics of Java will help you very much in working with selenium. Selenium webdriver is written in java and without the basic knowledge of java you will not be able to create a single logical webdriver testcase. That is why most of the trainers are offering selenium training with java courses.

When you talk about java, if, if else and nested if else statements in Java are of great significance as they are useful to take decisions based on conditional match.

For example in simple “IF” statement part of code will be executed only when the “IF” specified condition returns true. Code will not be executed when the “IF” condition returns false.

Example: if (i<j)
System.out.println(“Value Of i(“+i+”) Is Smaller Than Value Of j(“+j+”).” );

In this example, the value of variable i should be less than the value of variable j for getting the message printed.

At the same time when you use “IF ELSE” statement, part of “IF” specified condition will be executed when the condition returns true and when it is false the “ELSE” specified condition will be executed.

if (i>=j)
System.out.println(“Value Of i(“+i+”) Is Greater Than Or Equals To Value Of j(“+j+”).” );
System.out.println(“Value Of i(“+i+”) Is Smaller Than Value Of j(“+j+”).” );

In this message the printing of the message in if block will be carried out when the value of variable i is equal to that of variable j or more than J and the message in else block will be done when the value of i is less than that of j.

“NESTED IF ELSE” statement is used when there is a need to check multiple conditions and to take a decision based on it.

if (k<i)
System.out.println(“Value Of k(“+k+”) Is Less Than Value Of i(“+i+”)” );
}else if (k>=i && k<=j)
System.out.println(“Value Of k(“+k+”) Is In Between Value Of i(“+i+”) And Value Of Value Of j(“+j+”)” );
System.out.println(“Value Of k(“+k+”) Is Greater Than Value Of j(“+j+”)” );

In this example

  • If block will be executed when the value of variable k is less than that of i
  • Else if block will be executed when the value of variable k is equal to or greater than that of variable i and less than or equal to the value of variable j
  • Else block will be executed when the value of k is greater than that of j

As in the case of statements loops in java is also of great significance and one should learn them thoroughly to work easily with selenium webdriver. For example, there may come instances when you will have to perform same actions for many times on the webpage. If you write many lines of codes to perform the same functions many times, your code size will increase. Besides it consumes a lot of time also. In such situations loops will come for your help.

There are many loops in java such as “for” loop, “while” loop and “do while” loop. Each loop has its own uses. For Loop in java has three parts inside and they are Variable Initialization, Condition to Terminate and Increment/ Decrement Variables. When the condition for termination becomes false the “for” loop will get terminated.

Example: for(int i=0; i<=3; i++){
System.out.println(“Value Of Variable i is ” +i);

While, do while loops in java are also very useful while working with selenium webdriver. While loop will allow the execution of block of code written inside the loop as long as the condition of the while loop remains true.

Example:        int i = 0;
System.out.println(“Value Of Variable i Is “+i);

At the same time do while loop will perform the same function but as long as the condition returns true. In other words, the while loop will check the condition at the beginning of the code block and execute when it is true at the same time, do while loop will check at the end. Even if the condition is false, do while loop has executed for once and it will stop execution if the condition is false.


int j=0;
System.out.println(“Value Of Variable j Is “+j);

These statements and loops in java are of great significance while working with Selenium and hence it is always better to learn basic java along with selenium.

Leave a Reply

Your email address will not be published. Required fields are marked *