Looping: ‘Executing a set of statements‘ process is known as looping. These looping statements are also called iterative statements.
do..while - Exit check conditional loop
While loop is a control statement. It is used for iterating a part of the program several times. When the number of iteration is not fixed then while loop is used.
Syntax
while(condition)
{
//loop code here
}
Ex1:
class while1
{
public static void main(String arg[])
{
int x=1;
while(x<10)
{
System.out.println(x);
x++;
}
}
}
Output:
H:\>javac while1.java
H:\>java while1
1
2
3
4
5
6
7
8
9
Ex2:
class while2
{
public static void main(String[] args)
{
int myNumber = 1;
while(myNumber != 20)
{
if((myNumber % 2) != 0)
{
myNumber++;
}
else
{
System.out.println(myNumber + " is even");
myNumber++;
}
}
}
Output:
H:\>javac while2.java
H:\>java while2
2 is even
4 is even
6 is even
8 is even
10 is even
12 is even
14 is even
16 is even
18 is even
Ex3:
Infinite while loop
class While3
{
public static void main(String[] args)
{
while(true)
{
System.out.println("infinitive while loop");
}
}
}
Note: Since it is an Infinite loop, it never stops. To stop it forcibly press CTRL+C
do..while - Exit check conditional loop
The do-while loop is used to execute a part of the program again and again. If the number of iteration is not fixed then the do-while loop is used. This loop executes at least once because the loop is executed before the condition is checked.
Syntax:
do
{
//code for execution
}while(condition);
Ex1:
class dowhile1
{
public static void main(String args[])
{
int a=1;
do
{
System.out.println(a); // it is excecuting without checking condition for first time
}while(false);
}
}
Output:
H:\>javac dowhile1.java
H:\>java dowhile1
1
Ex2:
public class dowhile2
{
public static void main(String args[])
{
int iter = 0;
do
{
System.out.print(iter + " ");
} while (iter++ < 10);
}
}
Output:
H:\>javac dowhile2.java
H:\>java dowhile2
0 1 2 3 4 5 6 7 8 9 10
Ex3:
class doWhile3
{
public static void main(String[] args)
{
int i=10;
do
{
System.out.print(" "+i);
i--;
}while(i>0);
}
}
Output:
H:\>javac doWhile2.java
H:\>java doWhile2
10 9 8 7 6 5 4 3 2 1
Ex:4
public class doWhileLoop
{
public static void main(String args[])
{
char char_arr[] = {'a', 'b', 'c', 'd'};
// array index begins with 0 int iter = 0;
do
{
System.out.print(char_arr[iter] + " ");
iter++;
}while( iter < char_arr.length );
}
}
Output:
H:\>javac doWhileLoop.java
H:\>java doWhileLoop
a b c d
For loop
The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied. Syntax:
for(initialization; condition ; increment/decrement)
{
statement(s);
}
Ex1:
class ForDemo
{
public static void main(String[] args)
{
for(int i=1; i<11; i++)
{
System.out.println("Count is: " + i);
}
}
}
Output:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
Syntax : an infinite loop can be created as follows
for ( ; ; )
{
// write your code here
}
Ex2:
class ForDemo
{
public static void main(String[] args)
{
int i=1;
for(;;)
{
System.out.println("Count is: " + i);
}
}
}
Output:
Count is: 1 will be displayed infinite times
Note: To interrupt press CTRL+C on key board
Enhanced For loop / for-each
The for statement also has another form designed for iteration through Collections and arrays. This form is sometimes referred to as the enhanced for statement / for-each , and can be used to make your loops more compact and easy to read. The keyword we use is still for.
Note : when we want to iterate on the elements inside an array we can simply use it.
Syntax:
for(<return_type> <var_name>:<array_name>)
{
// write your code here
}
Ex1:
class EnFor
{
public static void main(String args[])
{
int arr[]={18,33,60,7};
for (int num : arr)
{
System.out.println(num);
}
}
}
Output:
18
33
60
7
Ex2:
Enhanced for loop for string data type
class EnForStr
{
public static void main(String args[])
{
String arr[]={"Java","Oracle","Unix"};
for(String str : arr)
{
System.out.println(str);
}
}
}
Output:
I:\>javac EnForStr.java
I:\>java EnForStr
Java Oracle Unix
No comments:
Post a Comment