Site icon TestingDocs.com

Loop Structures Overview

Introduction

Loop structures are used to perform repetitive tasks in automation code. Loop structures are a block of statements that are executed repeatedly based on loop condition(s).

Loop Structures

There are basic looping structures that we can use them in java code or automation code:

 

 

For Loop

For loop is executed for a fixed number of iteration. We can use this loop if we know the number of iterations beforehand. Sample Java program with a for loop:

https://www.testingdocs.com/questions/java-program-using-for-loop/

While Loop

While loop is entry controlled loop. The condition is executed first. If the condition is true then the body of the while loop is executed. The block of statements is executed until the loop condition becomes false.

Sample java program to demo the while loop can be found at:

https://www.testingdocs.com/questions/java-program-for-while-loop/

Do While Loop

Do While loop is exit controlled loop. The condition is executed in the last. The loop is executed once and the condition is checked. If the condition is true then the body of the do-while loop is executed again. The block of statements is executed until the loop condition becomes false.

Do while loop demo :

https://www.testingdocs.com/questions/java-program-for-do-while-loop/

Common Error

The common mistake automation testers do while working with loops is to fall into infinite loops. An infinite loop is a loop that may execute an infinite number of times.

Exit mobile version