LOOP CONTROL:
There may be a situation when we need to execute a block of code several number of times and is often referred to as a loop. Java has very flexible three looping mechanisms. You can use one of the following three loops:
There may be a situation when we need to execute a block of code several number of times and is often referred to as a loop. Java has very flexible three looping mechanisms. You can use one of the following three loops:
- while Loop
- do...while Loop
- for Loop
while Loop:
A while loop is a control structure that allows you to repeat a task a certain number of times.
While loop always check before entering the loop is that the condition is to be true or not.
Syntax
while(boolean Experssion){
//statements//
}
do...while Loop:
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
In do while first time do loop goes but after completing the one time run they check the for loop is true or not if not true they go out from the loop
THE MAIN DIFFERENCE IS THAT BETWEEN While AND Do...While LOOP.
Syntax
do{
//statements//
}
while(boolean Expression);
For Loop:
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated.
Syntax
for(initialize;boolean expression;increment/decrement){
//statement//
}
No comments:
Post a Comment