Loops(for loop, while loop, do while loop) - Basic Java Tutorials For Selenium WebDriver

¡Supera tus tareas y exámenes ahora con Quizwiz!

Example : int j=0; do{ System.out.println("Value Of Variable j Is "+j); j=j-1; }while(j>0); In above given example, while loop will be executed only one time.

do while Loop -Same as while loop, do while loop will be executed till the condition returns true.

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

for Loop There are three parts inside for loop of java software development language. - 1. Variable Initialization, -2. Condition To Terminate and - 3. Increment/Decrements variable. for loop will be terminated when condition to terminate will becomes false.

Example : int i = 0; while(i<=3){ System.out.println("Value Of Variable i Is "+i); i++; } In above given example of java software development language, while loop will be executed four times.

while Loop -Block of code which is written inside while loop will be executed till the condition of while loop remains true.

There is one difference between while and do while loop. -while loop will check condition at the beginning of code block so It will be executed only if condition (while(i<=3)) returns true. -do while loop will check condition at the end of code block so It will be executed minimum one time. After 1st time execution, it will check the condition and if it returns true then code of block will be executed once more or multiple time.

Difference between while and do while loop ( hint one checks condition at the beginning of code block and one will check the code at the end of the code block)

If you will forget to Increment or decrements variable value inside while loop block then block of code will be executed infinite time. Example : int i = 0; while(i<=3){ System.out.println("Value Of Variable i Is "+i); } Above given while loop will be executed infinite time because variable is not incremented inside while loop block.

Disadvantage of while or do while loop

Output of above given example will be as bellow. ***while loop example*** Value Of Variable i Is 0 Value Of Variable i Is 1 Value Of Variable i Is 2 Value Of Variable i Is 3 ***do while loop example*** Value Of Variable j Is 3 Value Of Variable j Is 2 Value Of Variable j Is 1 Value Of Variable j Is 0

What is the output??? public class Whileloop { public static void main(String[] args) { //while loop - will be executed till condition 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 by 1. } //do while loop - will be executed minimum one time without considering condition. System.out.println(""); System.out.println("***do while loop example***"); int j=3; //Variable initialization do{ System.out.println("Value Of Variable j Is "+j); j=j-1;//Decrementing value of j by 1; }while(j>=0); } }

When you will run above given example, you will get bellow given output. Value Of Variable i is 0 Value Of Variable i is 1 Value Of Variable i is 2 Value Of Variable i is 3 Value Of Variable j is 3 Value Of Variable j is 2 Value Of Variable j is 1 Value Of Variable j is 0 Value Of Variable k is 160

What is the output??? public class forloop { public static void main(String[] args) { for(int i=0; i<=3; i++){ //This loop will be executed 4 times System.out.println("Value Of Variable i is " +i); } System.out.println(""); int i=0; int k = 200; for(int j=3; j>=i; j--){ //This loop will be executed 4 times System.out.println("Value Of Variable j is " +j); k = k-10; } System.out.println(""); System.out.println("Value Of Variable k is " +k); } }

-sometimes you need to perform same action multiple times(Example : 100 or more times) on your web page. -the best practice, you need to use loops in this kind of situations.

loops in java


Conjuntos de estudio relacionados

Module 5: Cancer and the Immune System

View Set

Nervous/Musculoskeletal System Part 1 quiz

View Set

PN Comprehensive Online Practice 2020 A

View Set

Key to the Constitution Article 1 (Section 8)

View Set

Taxation of Personal Life Insurance

View Set