- Declaration starts with the keyword for like a normal for-loop.
- Instead of initializing a loop counter variable, declare a variable that is of the same type as the base type of the array, followed by a colon, which is then followed by the array name in Java.
- In the body of the loop, you can use the loop variable you created rather than using an indexed array element.
- It’s commonly used to iterate over an array or a Collections class.
- Instead of initializing a loop counter variable, declare a variable that is of the same type as the base type of the array, followed by a colon, which is then followed by the array name in Java.
- In the body of the loop, you can use the loop variable you created rather than using an indexed array element.
- It’s commonly used to iterate over an array or a Collections class.
This brings us to the end of this article where we have learned about the for-each loop in Java. Hope you are clear with all that has been shared with you in this tutorial.
If you found this article on “for-each loop in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you found this article on “for-each loop in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
What is a Do while loop in Java and how to use it?
The Java do-while loop is used to iterate a set of statements until the given condition is satisfied. If the number of iteration is not fixed and you MUST have to execute the loop at least once, then use a do while loop. In this tutorial, you will learn all about do while loop in Java and how to use it.
Let us dive into the article and explore the following topics:
What is Do while loop in Java
Flow diagram
Syntax
Implementation through example
Infinite do while loop
Syntax
Example of infinitive do while loop
Let’s begin!
What is Do while loop in Java?
Do-while loop is similar to while loop, but it possesses one dissimilarity: In while loop, the condition is evaluated before the execution of loop’s body but in do-while loop condition is evaluated after the execution of loop’s body.
I will make the concept visually clear through this flow diagram.
The Java do-while loop is used to iterate a set of statements until the given condition is satisfied. If the number of iteration is not fixed and you MUST have to execute the loop at least once, then use a do while loop. In this tutorial, you will learn all about do while loop in Java and how to use it.
Let us dive into the article and explore the following topics:
What is Do while loop in Java
Flow diagram
Syntax
Implementation through example
Infinite do while loop
Syntax
Example of infinitive do while loop
Let’s begin!
What is Do while loop in Java?
Do-while loop is similar to while loop, but it possesses one dissimilarity: In while loop, the condition is evaluated before the execution of loop’s body but in do-while loop condition is evaluated after the execution of loop’s body.
I will make the concept visually clear through this flow diagram.
Let me explain the above-shown diagram:
1 First of all, it will execute a set of statements that are mentioned in your ‘do’ block.
2 After that, it will come to ‘while’ part where it checks the condition.
3 If the condition is true, it goes back and executes the statements.
4 If the condition is false, it directly exits the loop.
Now, moving on towards the syntax of do while loop in Java
Syntax:
1 First of all, it will execute a set of statements that are mentioned in your ‘do’ block.
2 After that, it will come to ‘while’ part where it checks the condition.
3 If the condition is true, it goes back and executes the statements.
4 If the condition is false, it directly exits the loop.
Now, moving on towards the syntax of do while loop in Java
Syntax:
In the above-mentioned code, you can see that the do while loop will execute the condition once, for sure.
Now, let’s hop onto our next segment, Infinite do-while loop in Java.
Infinite do-while loop in Java
Infinite do-while loop in Java is similar to the infinite while loop. You can refer a section of my previous blog –Infinite while Loop in Java.
Now, let’s hop onto our next segment, Infinite do-while loop in Java.
Infinite do-while loop in Java
Infinite do-while loop in Java is similar to the infinite while loop. You can refer a section of my previous blog –Infinite while Loop in Java.
What is a Switch Case In Java?
Java programming language has conditional and control statements which optimizes the logic while writing a program. Hustle free logic building using the switch case results in improved efficiency. Using a switch case in java optimizes the readability of the code while working on multiple test expressions. In this article, you will learn about switch case in java with various examples.
What Is A Switch Case In Java?
Java switch statement is like a conditional statement which tests multiple values and gives one output. These multiple values that are tested are called cases. It is like a multi-branch statement. After the release of java 7 we can even use strings in the cases. Following is the syntax of using a switch case in Java.
Java programming language has conditional and control statements which optimizes the logic while writing a program. Hustle free logic building using the switch case results in improved efficiency. Using a switch case in java optimizes the readability of the code while working on multiple test expressions. In this article, you will learn about switch case in java with various examples.
What Is A Switch Case In Java?
Java switch statement is like a conditional statement which tests multiple values and gives one output. These multiple values that are tested are called cases. It is like a multi-branch statement. After the release of java 7 we can even use strings in the cases. Following is the syntax of using a switch case in Java.
Rules To Remember
There are a certain rules one must keep in mind while declaring a switch case in java. Following are a certain points to remember while writing a switch case in java.
1 We cannot declare duplicate values in a switch case.
2 The values in the case and the data type of the variable in a switch case must be same.
3 Variables are not allowed in a case, it must be a constant or a literal.
4 The break statement fulfills the purpose of terminating the sequence during execution.
5 It is not necessary to include the break statement, the execution will move to the next statement if the break statement is missing.
6 The default statement is optional as well, it can appear anywhere in the block.
There are a certain rules one must keep in mind while declaring a switch case in java. Following are a certain points to remember while writing a switch case in java.
1 We cannot declare duplicate values in a switch case.
2 The values in the case and the data type of the variable in a switch case must be same.
3 Variables are not allowed in a case, it must be a constant or a literal.
4 The break statement fulfills the purpose of terminating the sequence during execution.
5 It is not necessary to include the break statement, the execution will move to the next statement if the break statement is missing.
6 The default statement is optional as well, it can appear anywhere in the block.
public class Example{
public static void main(String args[]){
int month= 7;
switch(month){
case 1 :
System.out.println("january");
break;
case 2:
System.out.println("february");
break;
case 3:
System.out.println("march");
break;
case 4:
System.out.println("april");
break;
case 5:
System.out.println("may");
break;
case 6:
System.out.println("june");
break;
case 7:
System.out.println("july");
break;
case 8:
System.out.println("august");
break;
case 9:
System.out.println("september");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("november");
break;
case 12:
System.out.println("december");
break;
default:
System.out.println("not valid");
}
}
}
Output: july
public static void main(String args[]){
int month= 7;
switch(month){
case 1 :
System.out.println("january");
break;
case 2:
System.out.println("february");
break;
case 3:
System.out.println("march");
break;
case 4:
System.out.println("april");
break;
case 5:
System.out.println("may");
break;
case 6:
System.out.println("june");
break;
case 7:
System.out.println("july");
break;
case 8:
System.out.println("august");
break;
case 9:
System.out.println("september");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("november");
break;
case 12:
System.out.println("december");
break;
default:
System.out.println("not valid");
}
}
}
Output: july
Nested Switch Case
Nested switch case incorporates another switch case in an existing switch case. Following is an example showing a nested switch case.
Nested switch case incorporates another switch case in an existing switch case. Following is an example showing a nested switch case.
Fall Through Switch Case
Whenever there is no break statement involved in a switch case block. All the statements are executed even if the test expression is satisfied. Following is an example of a fall through switch case.
Whenever there is no break statement involved in a switch case block. All the statements are executed even if the test expression is satisfied. Following is an example of a fall through switch case.