Break, Continue and return statements Example in Selenium webdriver

Branching statements will transfer the control from one statement to another statement. the following are branching statements in java programming
1. break
2. continue
3. return

1. break:

break statement is used to stop the execution and comes out of loop. Mostly break statement is used in switch in order to stop fall through and in loops also we use break.

Example

package com.seleniumeasy.controlstatements;

public class BreakDemo {
public static void main(String[] args) {
	for(int i=1;i<=20;i++) {
		System.out.println(i);
		if(i==7)
			break;
	}
        }
}

Let us checkout selenium example for this break statement: -

boolean bValueExists = false;
// For each option in the list, first verify if it's the one that you want and then click on it
for (WebElement option : listOptions) {
if (option.getText().contains(sOptionToSelect)) {
option.click();
System.out.println("Selected option from the list")
bValueExists = true;
break;
}
}

In the above example, If we don't use Break statement, it will try to check all the list values and then comes out of the for loop. If say there are list of 20 values, and the One which we are looking is at 2nd position. Now when the condition is satisfied, we should come out of the loop instead of verifying all the remaining 18 values from the list.

You can check here for more detailed examples with selenium Working with Ajax / JQuery fields, Working with checkboxes etc.

2. Continue:

continue statement is also same as break statement the only difference is when break statement executes it comes out of loop where as continue comes out of loop and jumps to the conditional statement of loop. continue is used only in loops to jump from present iteration and executes for next iteration.

Example:

package com.seleniumeasy.controlstatements;

public class ContinueDemo {
	public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

3. return:

return statements are used in methods which returns a value or statement from current to calling method. return statement must be always last statement in the method

Example:

package com.seleniumeasy.controlstatements;

public class ReturnDemo {
	
	public static void main(String[] args) {
		int c;
		ReturnDemo rd =new ReturnDemo();
		c=rd.add(10,20);
		System.out.println(c);
	}
   public int add(int a, int b){
	int c=a+b;
	return c;
    }

}

Let us look into the below simple selenium example of 'return' :-

	/**
	 * This method is used to get the current url
	 * @return, returns current url
	 */
	public String getCurrentURL()
	{
		String strURL= null;
		try {
			strURL= driver.getCurrentUrl();
		}
		catch(Exception ex) 	{
			System.out.println("Exception occured while getting the current url : "+ex.getStackTrace());
			Assert.fail("Exception occured");
		}
		return strURL;
	}

The above method is to get current URL of the application. 'driver.getCurrentUrl();' will return URL as string and the same string will be returned to the calling method.

When ever working in a framework, for most of the methods, you will find return statements which can be re-used for multiple times. Each time any method calls, it will try to return the value. You can check here for more examples on return statements. Where we have applied for Working with Select examples, where some times we may return Boolean value like true/false when working with Window popups etc.

Java Tutorials for Selenium: 

Comments

Excellent Site. Please upload all the topics related to Core Java

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.