What is loop | Types of loop | When should you choose loop | when you decide to good for loop | What is the need of loop.
If you want to know about the loop than learn to hear. I am going to introduce the loop.
Q1.What is loop
Ans: Loop is a technique to execute a block of statements multiple times. we can if you want to execute the statement multiple times then keep this statement inside the loop. through the loop, we can reduce the length of the program and the complexity of the code. types of loop.
Types of the loop:
Basically, the loop is two types of 1.exit control loop and the other one is 2.entery control loop
1.Exit control loop-
The loop which is control after execution than goes for the exit control loop.
like- do-while loop
class A{
public static void main(String args[]){
int i=1;
do{
System.out.println("Hello");
}while(i++<10);
}
}
So, In this example, you know the syntax of the do-while loop.
2.Entry control loop:
In the entry control loop, you have to check the condition first then come inside the loop. if the condition is true.
like: while() loop and for() loop.
So, I am going to introduce both for() loop and while loop syntax.
for() loop: If you know the ending point of iteration or how many times you have to iterate than go for() loop.
class A{
public static void main(String args[]){
for(int i=0;i<10;i++){
System.out.println("Hello");
}
}
}
In this example, you already know how many times you have to iterate the loop then choose the for a loop.
While() loop: If you don't how many times you have to iterate the loop then go for a while() loop because all the conditions are we don't know about this.
class A{
public static void main(String args[]){
int i=10;
while(i!=0){
System.out.println("Hello");
i=i/2;
}
}
}
2.Entry control loop:
In the entry control loop, you have to check the condition first then come inside the loop. if the condition is true.
like: while() loop and for() loop.
So, I am going to introduce both for() loop and while loop syntax.
for() loop: If you know the ending point of iteration or how many times you have to iterate than go for() loop.
class A{
public static void main(String args[]){
for(int i=0;i<10;i++){
System.out.println("Hello");
}
}
}
In this example, you already know how many times you have to iterate the loop then choose the for a loop.
While() loop: If you don't how many times you have to iterate the loop then go for a while() loop because all the conditions are we don't know about this.
class A{
public static void main(String args[]){
int i=10;
while(i!=0){
System.out.println("Hello");
i=i/2;
}
}
}
Comments
Post a Comment