Loops In Java…What is that?
Loops In Java are used for executing a block of statements continuously until a particular condition is satisfied.
For example, if we want to print numbers from 1-10 we have two ways.
1- To write System.out.println() and put the numbers and print it 10 times.
2-We can simply use loops which will automatically print the numbers from 1 to 100 or whatever is the last digit or condition.
Therefore we can say that loops execute the block of the statement until a particular condition is satisfied.
Let us write a simple program to print numbers from 1 to 100 by both the methods mentioned above.
There are 3 types of loops in java-:
1-While loop
2-For loop
3-Do while
For Loops In Java-:
these loops is a concise way of writing the loop structure. For loop starts with the for keyword and then we have to enter the initialization, condition, and then increment and decrement it. For example, Let print table of 2 with the help of for loop.
Source-geeks for geeks
While Loops In Java:-
While loop executes the codes repeatedly based on the boolean condition we give. In the while loop, we increment the or decrement code after writing the whole code.
Let us print all the odd numbers between 1-10.
So, as we can see that in the while loop we have to increment or decrement at last. Moreover, while loop is used for checking the condition if it is true or false, like in the above example the compiler first checked the condition given that if the integer n is less than 10 then print the value else don’t print the value.
Do-While Loops In Java
Do while loop executes the code block once and then checks if the given condition true. So, the Do while loop will execute the code block at least one time.
Syntax
Now, let’s print numbers from 1-10 using the do while loop.
Now let’s see if the code stills works if put n=12.
It works! This is because the compiler first executes the code present in do block and then checks for the condition given in while hence it prints 12.
After this compiler stops printing numbers as the given condition is not satisfied.
Let’s branch using Branching Statements
Why do we need branching statements?
Break and continue statements are known as branching statements or jump statements. These statements can be used inside any loop(for,do-while, while).
These statements are used to skip some statements or immediately terminate the loop when the condition is satisfied.
Break-
Break statement terminates from the loop immediately. When the compiler detects the break statement it stops the iteration of the loop there and returns to the first statement after the loop.
Syntax:-
Now let us see some examples for a better understanding of the break statement in the loop.
Suppose we have numbers from 1-20 and we want to print the first 10 numbers. So, we can do this by using break statement.
We can see that using the break statement we can print the first 10 numbers.
Continue Statement
Continue statement is used to skip the iteration of the loop.
For example, if we want to print numbers from 1-20 but we don’t want to print a particular number say 11. This can be done either by looping twice or by using the continue statement. Let’s code the above example.
Pros of using branch Statements
Branching statements optimize our code and make it look clean.
For example, if we write the above codes without using break and continue statements then our code length and time complexity will increase. Let’s try this to write the above code without using a continue statement for better understanding.
As we can see, the result is same but our code’s time complexity has increased from O(N) to O(N2).
Hence use of branching statements is a good practice as it reduces the complexity and makes our code look simple and readable by others.
Conclusion
Looping and Branching are important aspects in Java as both loops and branch statements reduce the time complexity and length of the code.
written by: Aakarsh Vats
Reviewed By: Soutik Maity
If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs