Give an Example of While Loop in Java
Loops are control statements used to repeat a certain execution path while a given condition holds true. There are three loop structures in Java and most other programming languages: for, while, & do while.
Loops are an important part of program development because they provide a simple way of making iterations without having to repeat multiple selection statements.
1. For Loop
This is a counter-controlled iteration statement. The for loop requires an initialization of the counter and a condition for it to continue iterating while true.
The syntax for using a for statement is as follows:
for (initialization; condition; increment){ // statements}
All the expressions in the for statement are optional. The semicolons (;) are mandatory, though.
You can initialize the counter outside the for loop and then include other expressions inside it. See the example below:
int x=4;
for ( ;x<10;x++){
System.out.println("x"); // print out x
}
It's also possible to leave out the condition in your for loop. This will result in an infinite loop since the Java compiler will interpret the condition as always true.
Note: If you wish to leave the increment part out of the for header, you should include it in the for statement's body if needed.
At this point, it's important to mention that declaring the control variable in the for loop will give it local scope. That is, the variable will only be accessible within the for loop. Trying to use it outside that will give a compile-time error.
Though, if you declared the variable outside the for loop, then it would have global scope. In other words, you could be able to access it & the value assigned to it outside the for statement.
It is possible to have for loops inside of for loops. This is known as having nested loops.
for ( int x=2; x<9; x=x+2){
for ( int y=5;y>=3;y--){
System.out.println(x*y); // print product of x and y
}}
It's advisable not to have more than 3 nested loops. It becomes increasingly difficult for you to follow the logic or correct any errors as the number of for loops increase.
Notice the first for loop. The increment is x=x+2. The increment doesn't always have to be +1. It can be any value that you wish to have.
The increment can also be a "decrement ". See the nested for loop (y--). Since you'll be counting backward, remember to take extra care during initialization and when stating the loop-continuation condition.
2. While Loop
Unlike the for loop, the while statement can be used without a counter. The while statement is used to iterate through certain statements while a given condition holds true.
It first checks if a condition is true before executing the statement(s) in its body.
The syntax is as follows:
while (condition){ // statements}
If you wish to use the while loop as an alternative to the for statement, then the syntax is as shown below:
initialization;
while (condition){
//statements
increment;
}
If you don't provide a statement in the while body that will finally make it false, a logic error will occur. You'll get an infinite loop.
3. Do While
This is similar to the while statement. The difference is that the do..while statement must execute at least once, regardless of whether the condition to enter the loop was false.
It first begins by executing the statements given in the do{} body, and then checks if the loop-continuation condition is true. If the condition is found to be false, execution of the loop stops and program control is shifted to the next statements after the loop.
Below is the do..while syntax:
do{
// statements to execute
} while (loop-continuation condition);
Looping Back to Java Arrays
It's pretty common for programmers to have an off-by-one error when stating the loop-continuation condition. To avoid this, it's best to use the >=, <= operators rather than >,<.
You should also be mindful of the scope that the counter variable used. Negligence in use could cause logic errors in your code.
With your knowledge of For, While, and Do While loops as another notch on your programmer's belt, you should be gearing up to learn arrays in Java.
How to Create and Perform Operations on Arrays in Java
Learning Java? Let arrays handle your data with ease.
Read Next
About The Author
Jerome Davidson (33 Articles Published)
Jerome is a Staff Writer at MakeUseOf. He covers articles on Programming and Linux. He's also a crypto enthusiast and is always keeps tabs on the crypto industry.
More From Jerome Davidson
Give an Example of While Loop in Java
Source: https://www.makeuseof.com/for-while-do-while-java/
0 Response to "Give an Example of While Loop in Java"
Post a Comment