As we discuss about the Looping in the Previous section, Looping is a process which is used to execute a set of statements repetitively when a certain condition true.
Java provides three ways to execute the loops. While all of the ways offer similar basic functionality, but differ in syntax and condition. |
while Loop :
While loop, besides the for loop, while loop divided into three parts first one initilazation, second used test condition infront of while and increment/decrement inside the body of loop.
Syntax: Initilazation while(Test Condition ) { Statements or Body of Loop; Increment/ Decrement; } |
1.Initialization: We initialize the variable in use here. It marks the beginning of a for loop. It is possible to use an already declared variable, or to declare a variable, local to loop only.
2. Test Condition: It is used to test a loop's exit condition. They have to return a boolean value. It is also an Entry Control Loop, since the condition is checked before the loop statements are executed.
3. Execution of statements: Once the condition is evaluated to true, the statements are executed in the body of the loop.
4.Increment / Decrement: Used for next iteration to change the variable.
5. Loop termination: When the state is incorrect the loop stops signaling the end of its cycle of life.
Flow Chart of While Loop :