In Java we have three types of loops.We use loops in programming whenever we need
to execute the same statement several times.
while Loop
to execute the same statement several times.
- while loop
- do while loop
- for loop
while Loop
syntax
while(expression)
{
statement(s);
}
The conditional expression must return a boolean value.The block inside while loop will be executed when it gets a true value of the expression.After execution of the inside block it rechecks the conditional expression.
Whenever it gets a false value of the expression it stops execution.
break statement is also used to get out of the loop.
do while Loop
do while syntax
do{
statement(s);
}while(expression);
do while behaves in the same way as while loop the only difference is the conditional expression is checked after executing the loop.
for Loop
for syntax
for(initialization;condition;increment)
{
statement(s);
}
initialization part takes care of the initial value of a variable or start point of the variable.
conditional part is used to take decision whether the loop will be executed or terminated.
increment part is used to increment or decrement the initialized value of the variable.
Code Example :while(expression)
{
statement(s);
}
The conditional expression must return a boolean value.The block inside while loop will be executed when it gets a true value of the expression.After execution of the inside block it rechecks the conditional expression.
Whenever it gets a false value of the expression it stops execution.
break statement is also used to get out of the loop.
do while Loop
do while syntax
do{
statement(s);
}while(expression);
do while behaves in the same way as while loop the only difference is the conditional expression is checked after executing the loop.
for Loop
for syntax
for(initialization;condition;increment)
{
statement(s);
}
initialization part takes care of the initial value of a variable or start point of the variable.
conditional part is used to take decision whether the loop will be executed or terminated.
increment part is used to increment or decrement the initialized value of the variable.
package com.fastlearned; public class LoopExample { public void forLoop() { for(int i=10;i<=100;i=i+10) { System.out.println("inside for Loop "+i); } } public void whileLoop() { int j = 0; while(j<=10) { System.out.println("inside while Loop "+j); j++; } } public void dowhileLoop() { int z = 0; do{ System.out.println("inside do..while Loop "+z); z++; } while(z<=10); } public static void main(String[] args) { LoopExample loopExample = new LoopExample(); loopExample.forLoop(); loopExample.whileLoop(); loopExample.dowhileLoop(); } }Output :
inside for Loop 10 inside for Loop 20 inside for Loop 30 inside for Loop 40 inside for Loop 50 inside for Loop 60 inside for Loop 70 inside for Loop 80 inside for Loop 90 inside for Loop 100 inside while Loop 0 inside while Loop 1 inside while Loop 2 inside while Loop 3 inside while Loop 4 inside while Loop 5 inside while Loop 6 inside while Loop 7 inside while Loop 8 inside while Loop 9 inside while Loop 10 inside do..while Loop 0 inside do..while Loop 1 inside do..while Loop 2 inside do..while Loop 3 inside do..while Loop 4 inside do..while Loop 5 inside do..while Loop 6 inside do..while Loop 7 inside do..while Loop 8 inside do..while Loop 9 inside do..while Loop 10
No comments:
Post a Comment