A loop is used in a program to change its flow by executing an instruction or group of instructions several times as directed by the programmer. This saves programmer’s time because if loops were not available he would have to write those instructions as many times as the loop iterated.
for (initialization; condition;
increment) {
instruction(s);
}
for(int i=0;i<10;i++){
System.out.println(“Hello”);
}
for(int i=0;i<10;i++){//loop 1
for(){
//our code statement/s
}
Scanner reader = new Scanner(System.in);// a reader object for getting input at run time from user.
//Use import java.util.Scanner; line at top of your program)
int myArray[]=new int[5];
int a;
for(int j=0;j<5;j++){ //using for loop to get value for each index from user
System.out.println(“Enter number”);
//get user input for a
a=reader.nextInt();
myArray[j]=a;
}
for(int j=0;j<5;j++){ //displaying elements of myArray
System.out.println(“”+myArray[j]);
}
}
int count;//a variable used to count number of 5′s
String index;//a variable for getting index of location of 5′s
for(int i=0;i<100;i++){
if(arr[i]==5){
count++;
index=”"+i+” “;//getting index number of location in arr at which a digit 5 is present
System.out.println(“”+count+” its index “+index);
}
}
while (condition) {
instruction(s);
}
class MyWhileLoop {
public static void main(String[] args){
int x = 1;
while (x < 11) {
System.out.println(“Hello”);
count++;
}
}
}
int count=1;
do {
System.out.println(“Hello”);
int count=100;
do {
System.out.println(“Hello”);
} while (count<11);
Java Loops:
Java has 4 basic kind of loops
1. For loops
2. While loops
3. Do-while loops
4. For each loops
2. While loops
3. Do-while loops
4. For each loops
Here we will only discuss the first 3 as they are most commonly used in every day programming.
For loops:
For loop is the most commonly type of loop used in all programming languages including Java. For loop has three parts which are:
1. Initialization: An integer in initialized
2. Condition: Its a condition on the initialized integer as long as this condition is TRUE loop iterates and continues but when this condition gets FALSE the loops breaks
3. Increment: The value of the loop increments by an amount defined in the loop each time the loop iterates or the condition given is TRUE
1. Initialization: An integer in initialized
2. Condition: Its a condition on the initialized integer as long as this condition is TRUE loop iterates and continues but when this condition gets FALSE the loops breaks
3. Increment: The value of the loop increments by an amount defined in the loop each time the loop iterates or the condition given is TRUE
Syntax:
for (initialization; condition;
increment) {
instruction(s);
}
Suppose we are given a task to print “Hello” 10 times, now there are two ways
1. Write System.out.println(“Hello”) 10 times which would be hectic
OR
2. Use a for loop to do this job as shown below
1. Write System.out.println(“Hello”) 10 times which would be hectic
OR
2. Use a for loop to do this job as shown below
for(int i=0;i<10;i++){
System.out.println(“Hello”);
}
Both of the methods will give same output but we can easily see that using for loop has saved us time which is every thing now-a-days. Anyway for loops can be nested which means there can be one for loop in another such as
for(int i=0;i<10;i++){//loop 1
for(int j=0;j<10;j++){//loop 2
System.out.println(“Hello”);
}
}
System.out.println(“Hello”);
}
}
In this example we used a nested loop(loop within a loop) which would print “Hello” 100 times as 10×10=100, now reader would ask why would it print “Hello” 100 times?. Well its because each single time loop 1 iterates loop 2 completes its all(10) iterations. So, as loop 1 eventually completes its 10 iterations the total times loop 2 was iterated is 100 as for each single iteration of loop 1 loop 2 iterated 10 times.
To execute an instruction infinite times(such instructions can be used in hardware projects or interrupts) we can use an infinite for loop
for(){
//our code statement/s
}
We can use for loop to access an array or initialize an array.
Example 1:
Scanner reader = new Scanner(System.in);// a reader object for getting input at run time from user.
//Use import java.util.Scanner; line at top of your program)
int myArray[]=new int[5];
int a;
for(int j=0;j<5;j++){ //using for loop to get value for each index from user
System.out.println(“Enter number”);
//get user input for a
a=reader.nextInt();
myArray[j]=a;
}
for(int j=0;j<5;j++){ //displaying elements of myArray
System.out.println(“”+myArray[j]);
}
}
Example 2:
Now suppose we are given an assignment to find all 5′s in an array of 100 elements. In this case we don’t know beforehand that how many 5′s are in our array and what if we were asked to get their index or location in this array also. We would do something like this
int count;//a variable used to count number of 5′s
String index;//a variable for getting index of location of 5′s
for(int i=0;i<100;i++){
if(arr[i]==5){
count++;
index=”"+i+” “;//getting index number of location in arr at which a digit 5 is present
System.out.println(“”+count+” its index “+index);
}
}
While & Do-while loops:
While loop:
While loop is very simple it has to check a condition which if TRUE the loop iterates else if FALSE the loop breaks.
Syntax:
while (condition) {
instruction(s);
}
Example:
Suppose once again we need to display “Hello” 10 times but this time with while loop this can be done as shown below
class MyWhileLoop {
public static void main(String[] args){
int x = 1;
while (x < 11) {
System.out.println(“Hello”);
count++;
}
}
}
The condition x<11 will be FALSE when value of x will be 11 and that will happen after the statement System.out.println(“Hello”); is executed 10 times. So “Hello” will be printed 10 times.
D0-while Loop:
A do-while loop is same as while loop except that it executes the instruction(s) written in ‘do’ braces without checking the condition of ’while’ for first time only.
Syntax:
do{
instruction(s);
} while (condition);
instruction(s);
} while (condition);
Example 1:
int count=1;
do {
System.out.println(“Hello”);
count++;
} while (count<11);Will print “Hello” 10 times.
} while (count<11);Will print “Hello” 10 times.
Example 2:
int count=100;
do {
System.out.println(“Hello”);
} while (count<11);
We can see that in this example even though condition count<11 is FALSE because count is initialized as 100 but “Hello” will be printed once as for the first time a do-while loop does not check condition and executes the statement(s) placed between braces of ‘do’. But before next iteration loop will break as this time condition will be checked.
0 comments :
Post a Comment