The process of repeatedly executing a statements and is called as looping. The statements may be executed multiple times (from zero to infinite number). If a loop executing continuous then it is called as Infinite loop. Looping is also called as iterations.
In Iteration statement, there are three types of operation :
The for loop is entry controlled loop. It means that it provide a more concious loop control structure.
//Syntax: for(initialization;condition;iteration)//iteration means increment/ decrement { Statement block; }
Flowchart :
When the loop is starts, first part(i.e. initialization) is execute. It is just like a counter and provides the initial value of loop. But the thing is, I nitialization is executed only once. The next part( i.e. condition) is executed after the initialization. The important thing is, this part provide the condition for looping. If the condition will satisfying then loop will execute otherwise it will terminate.
Third part(i.e. iteration) is executed after the condition. The statements that incremented or decremented the loop control variables.
//For example : import java.io.*; class number { public static void main(String args[]) throws Exception { int i; System.out.println("list of 1 to 10 numbers"); for(i=1;i<=10;i++) { System.out.println(i); } } } // OutPut : C:\MCA>javac number.java C:\MCA>java number list of 1 to 10 numbers 1 2 3 4 5 6 7 8 9 10
Here we declare i=1 and then it check the condition that if i<10 then only loop will be executed. After first iteration the value of i will print and it will incremented by 1. Now the value of i=2 and again we have to check the condition and value of i will print and then increment I by 1 and so on.
The while loop is entry controlled loop statement. The condition is evaluated, if the condition is true then the block of statements or statement block is executed otherwise the block of statement is not executed.
//Syntax : While(condition) { Statement block; }
Flowchart :
For example : Write a program to display 1 to 10 numbers using while loop.
import java.io.*; class number { public static void main(String args[]) throws Exception { int i=1; System.out.println("list of 1 to 10 numbers"); while(i<=10) { System.out.println(i); i++; } } } // Output: C:\MCA>javac number.java C:\MCA>java number list of 1 to 10 numbers 1 2 3 4 5 6 7 8 9 10
In do-while loop, first attempt of loop should be execute then it check the condition.
The benefit of do-while loop/statement is that we get entry in loop and then condition will check for very first time. In while loop, condition will check first and if condition will not satisfied then the loop will not execute.
//Syntax: do { Statement block; } While(condition);
In program,when we use the do-while loop, then in very first attempt, it allows us to get enter in loop and execute that loop and then check the condition.
Following program show the use of do-while loop.
For example : Write a program to display 1 to 10 numbers using dowhile loop.
import java.io.*; class number { public static void main(String args[]) throws Exception { int i=1; System.out.println("list of 1 to 10 numbers"); do { System.out.println(i); i++; }while(i<=10); } } // O/P : list of 1 to 10 numbers 1 2 3 4 5 6 7 8 9 10
Statements or loops perform a set of operartions continually until the control variable will not satisfy the condition. but if we want to break the loop when condition will satisy then Java give a permission to jump from one statement to end of loop or beginning of loop as well as jump out of a loop.
“break” keyword use for exiting from loop and “continue” keyword use for continuing the loop.
Following statements shows the exiting from loop by using “break” statement.
//do-while loop: do { ……………… ……………… if(condition) { break;//exit from if loop and do-while loop } …………….. …………….. } While(condition); ……….. ……….. For loop: for(…………) { …………… ………….. if(…………..) break; ;//exit from if loop and for loop …………… …………… } …………… ………….. While loop: while(…………) { …………… ………….. if(…………..) break; ;//exit from if loop and while loop …………… …………… }
Following statements shows the continuing the loop by using “continue” statement.
//do-while loop: do { ……………… ……………… if(condition) { continue;//continue the do-while loop } …………….. …………….. } While(condition); ……….. ……….. For loop: for(…………) { …………… ………….. if(…………..) continue ;// continue the for loop …………… …………… } …………… ………….. While loop: while(…………) { …………… ………….. if(…………..) continue ;// continue the while loop …………… …………… } ……………. …………….
Labelled loop :
We can give label to a block of statements with any valid name.following example shows the use of label, break and continue.
For example :
Import java.io.*; class Demo { public static void main(String args[]) throws Exception { int j,i; LOOP1: for(i=1;i<100;i++) { System.out.println(““); if(i>=10) { break; } for(j=1;j<100;j++) { System.out.println(“$ ”); if(i==j) { continue LOOP1; } } } System.out.println(“ End of program “); } } // O/P: $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
All Rights Reserved. © 2024 BookOfNetwork