Automation Notes (16.03.2022)
Automation Notes (16.03.2022)
Loop:
while loop is preferred to use, If the number of iteration is not fixed. It is pre-test loop
it is also called as entry control loop
Syntax :
while(condition){
//code to be executed
}
Program:
public class WhileLoop1 {
public static void main(String[] args) {
//1 2 3 4 5
// for(int i=1;i<6;i++)
// {
// System.out.println(i);
// }
int i=1; //i=6
while(i<6) //6<6 x
{
System.out.println(i); //1 2 3 4 5
i++;
}
}
}
Program:
public class WhileLoop2 {
public static void main(String[] args) {
//2 4 6 8 10 Even number upto 50
int i=2;
while(i<=50)
{
System.out.print(i + " ");
i=i+2;
}
}
}
Program:
public class WhileLoop3 {
public static void main(String[] args) {
//1 8 27 64 125
int i=1; //i=6
while(i<=5) //6<=5
{
System.out.println(i*i*i); //1 8 27 64 125
i++;
}
}
}
Program:
public class WhileLoop4 {
public static void main(String[] args) {
//20 19 18 .........1
int i=20;
while(i>=1) //20>=1
{
System.out.print(i + " ");
i--;
}
}
}
Program:
public class WhileLoop5 {
public static void main(String[] args) {
//19 17 15.......1
int i=19;
while(i>=1)
{
System.out.print(i + " ");
i=i-2;
}
}
}