Loop
In computer science, a for-loop is a control flow statement for specifying iteration, which allows code to be executed repeatedly. Wikipedia
In java three types of loop: for loop,while loop , do-while loop.
the syntax is easy. syntax of all three loops contains the name as a keyword.
all loop is different from each other.
basically two kind of loop
⇾ Entry control: in this case condition for entering inside the loop is checked first.
⇾ Exit control: in this case condition for entering inside the loop is checked after the first iteration means at the end.
for loop
≻ this loop is Entry control loop
syntax
for( initialization ; condition ; increment / decrement)
- initialization is done inside the bracket in for loop.
- condition or conditions will be checked. if the condition is satisfied then the body of the loop will be executed else loop will not execute.
- after that increment or decrement is performed on the variable.
separation of all things is done by ' ; '
example
for(int i = 0 ; i < 10 ; i++ ) {
System.out.println("value of i is " + i); // body of the loop
}
✱ firstly i is loop variable and it is initialized by 0.
✱ after that condition will be chacked is i is less than 10 or not.
✱ condition is true so the body part is executed.
✱ finally, the increment is done and after that again condition will be checked.
only first-time initialization will be done.
the variable i will be deleted by GC(garbage collector) of java because it is local variable.
✴️Flavours of for
❇️ int i = 0
for( ; i < 10 ; i++ ) {
System.out.println("value of i is " + i); //body of the loop
}
✳️
for( ; i < 10 ; i++ ) {
System.out.println("value of i is " + i);// body of the loop
}
✳️ infinity loop
for( ; ; ) {
System.out.println("i am infinity loop");
}
while loop
≻ this loop is Entry control loop
syntax
initialization
while(condition){
increment / decrement
}
- initialization is done before the 'while' keyword appears in code.
- condition or conditions will be checked. if the condition is satisfied then the body of the loop will be executed else loop will not execute.
- inside the loop, increment or decrement is performed on the variable.
example
int i = 0; // initialization
while (i < 10) { // condition
System.out.println("value of i is " + i);
i++; // increment
}
✴️Flavours of while
✳️ infinity loop
while( true ) {
System.out.println("i am infinity loop");
}
do-while loop
≻ this loop is Exit control loop
syntax
initialization
do{
increment / decrement
} while(condition)
- initialization is done before the 'do' keyword appears in code the same as while loop.
- inside the loop, increment or decrement is performed on the variable.
- condition or conditions will be checked. if the condition is satisfied then the body of the loop will be executed else loop will not execute.
example
int i = 0; // initialization
do{ System.out.println("value of i is " + i);
i++; // increment
}while (i < 10) ; // condition
✴️Flavours of do-while
✳️ infinity loop
do{
System.out.println("i am infinity loop");
}while( true );
🔯 When to use which loop
⇨ when we have a condition in that body part will have to executed once, we can use do-while loop. other cases we can use for or while loop.
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments
Please comment here...