Comparison Between for loop while loop and do-while loop





for loop while loop do-while loop
1. A for loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop. 1. A while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop. 1. A do-while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the end of the loop.
2. A variable value is initialized at the beginning of the loop.2. A variable value is initialized at the beginning or before of the loop.2. A variable value is initialized before the loop or assigned inside the loop.
3. The statement block will not be executed when the value of the condition is false.3. The statement block will not be executed when the value of the condition is false.3. The statement block will not be executed when the value of the condition is false, but the block is executed at least once irrespective of the value of the condition.
4. A statement to change the value of the condition or to increment the value of the variable is given at the beginning of the loop. 4. A statement to change the value of the condition or to increment the value of the variable is given inside of the loop. 4. A statement to change the value of the condition or to increment the value of the variable is given inside of the loop.
5. A for loop is commonly used by many programmers.5. A while loop is widely used by many programmers.5. A do-while loop is used in some cases where the condition need to be checked at the end of the loop.