while, produce spell Loops - Basic Java Tutorials For Selenium WebDriver

As nosotros accept learnt inwards my previous post, loops(for loop, spell loop) inwards java software evolution linguistic communication or whatsoever other software programming languages are useful to execute block of code multiple times. You volition accept to role loops inwards your selenium webdriver software tests really frequently. We accept already learnt for loop with dissimilar examples inwards my previous post. Now permit me depict y'all while loop too create spell loop amongst practical examples inwards coffee software evolution language.

while Loop
Block of code which is written within spell loop volition last executed till the status of spell loop remains true.
Example :
        int i = 0;   while(i<=3){   System.out.println("Value Of Variable i Is "+i);   i++;  }
In higher upward given instance of  java software evolution language, spell loop volition last executed iv times.

do spell Loop
Same equally spell loop, create spell loop volition last executed till the status returns true.
Example :
 int j=0;  do{   System.out.println("Value Of Variable j Is "+j);   j=j-1;  }while(j>0);
In higher upward given example, spell loop volition last executed solely 1 time.

Difference betwixt spell too create spell loop
There is 1 divergence betwixt spell too create spell loop.
  • while loop volition banking corporation tally status at the outset of code block hence It volition last executed solely if status (while(i<=3)) returns true. 
  • do spell loop volition banking corporation tally status at the cease of code block hence It volition last executed minimum 1 time. After 1st fourth dimension execution, it volition banking corporation tally the status too if it returns truthful hence code of block volition last executed again or multiple time. 
Disadvantage of spell or create spell loop
If y'all volition forget to Increment or decrements variable value within spell loop block hence block of code volition last executed interplanetary space time.
Example :
        int i = 0;   while(i<=3){   System.out.println("Value Of Variable i Is "+i);      }
Above given spell loop volition last executed interplanetary space fourth dimension because variable is non incremented within spell loop block.

Bellow given amount instance of spell too create spell loops volition clear out your all doubts. Simple run it inwards your eclipse too verify result.
public shape Whileloop {   populace static void main(String[] args) {      //while loop - volition last executed till status returns true.   System.out.println("***while loop example***");   int i = 0; //Variable initialization   while(i<=3){    System.out.println("Value Of Variable i Is "+i);    i++;//Incrementing value of i past times 1.   }      //do spell loop - volition last executed minimum 1 fourth dimension without considering condition.   System.out.println("");   System.out.println("***do spell loop example***");   int j=3; //Variable initialization   do{    System.out.println("Value Of Variable j Is "+j);    j=j-1;//Decrementing value of j past times 1;   }while(j>=0);  } }

Output of higher upward given instance volition last equally bellow.
***while loop example*** Value Of Variable i Is 0 Value Of Variable i Is 1 Value Of Variable i Is two Value Of Variable i Is iii  ***do spell loop example*** Value Of Variable j Is iii Value Of Variable j Is two Value Of Variable j Is 1 Value Of Variable j Is 0

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM

http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post