💎Common Loop Examples
For Loop
Print numbers from 1 to 10 using a for loop.
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}Print even numbers from 1 to 20 using a for loop.
for (int i = 2; i <= 20; i += 2) {
System.out.println(i);
}Print odd numbers from 1 to 20 using a for loop.
for (int i = 1; i <= 20; i += 2) {
System.out.println(i);
}Calculate the sum of numbers from 1 to 100 using a for loop.
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("Sum: " + sum);Print the multiplication table of a given number using a for loop.
Print the Fibonacci series up to a given number using a for loop.
Find the factorial of a given number using a for loop.
Print the reverse of a given string using a for loop.
Check if a given number is prime using a for loop.
Count the number of vowels in a given string using a for loop.
While Loop
Print numbers from 1 to 10 using a while loop.
Print even numbers from 1 to 20 using a while loop.
Print odd numbers from 1 to 20 using a while loop.
Calculate the sum of numbers from 1 to 100 using a while loop.
Print the multiplication table of a given number using a while loop.
Print the Fibonacci series up to a given number using a while loop.
Find the factorial of a given number using a while loop.
Print the reverse of a given string using a while loop.
Check if a given number is prime using a while loop.
Count the number of vowels in a given string using a while loop.
For-Each Loop
The foreach loop is used to iterate over elements of an array or a collection without using an index variable. The syntax for a foreach loop is as follows:
Here are some examples of using foreach loops in Java:
Print elements of an array using a foreach loop.
Print elements of a list using a foreach loop.
Calculate the sum of elements in an array using a foreach loop.
Print the multiplication table of a given number using a foreach loop.
Print the Fibonacci series up to a given number using a foreach loop.
The time and space complexity of foreach loops in Java are the same as for loops, which depend on the number of iterations and the size of the array or collection. The time complexity of a foreach loop is O(n), where n is the number of iterations. The space complexity of a foreach loop is O(1), as it only uses a fixed number of variables.
Switch Statement
The next important loop commonly used in interviews is the switch statement. The switch statement is used to perform different actions based on different conditions. It provides an alternative to using multiple if-else statements. The syntax for a switch statement is as follows:
Here are some examples of using switch statements in Java:
Print the name of the day based on the day number using a switch statement.
Calculate the number of days in a given month using a switch statement.
Perform different operations based on the operator using a switch statement.
The time and space complexity of switch statements in Java are constant, O(1), as the execution time and memory usage do not depend on the number of cases. However, the efficiency of a switch statement can be impacted by the number of cases and the complexity of the statements within each case.
Last updated
Was this helpful?