Loops 123
Loops 123
Loops 123
o for loop
o while loop
o do-while loop
1. Initialization: It is the initial condition which is executed once when the loop
starts. Here, we can initialize the variable, or we can use an already initialized
variable. It is an optional condition.
2. Condition: It is the second condition which is executed each time to test the
condition of the loop. It continues execution until the condition is false. It
must return boolean value either true or false. It is an optional condition.
3. Statement: The statement of the loop is executed each time until the
second condition is false.
4. Increment/Decrement: It increments or decrements the variable value. It is
an optional condition.
Syntax:
for(initialization;condition;incr/decr){
Flowchart:
Example:
for(int i=1;i<=10;i++){
System.out.println(i);
Output:
1
2
3
4
5
6
7
8
9
10
Example:
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Pyramid Example 1:
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
System.out.println();//new line
Output:
*
* *
* * *
* * * *
* * * * *
Pyramid Example 2:
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){
System.out.print("* ");
System.out.println();//new line
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*
It works on elements basis not index. It returns element one by one in the
defned variable.
Syntax:
for(Type var:array){
//code to be executed
Example:
//Declaring an array
int arr[]={12,23,44,56,78};
for(int i:arr){
System.out.println(i);
Output:
12
23
44
56
78
Syntax:
for(;;){
//code to be executed
Example:
for(;;){
System.out.println("infnitive loop");
Output:
infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c
Syntax:
while(condition){
//code to be executed
}
Example:
int i=1;
while(i<=10){
System.out.println(i);
i++;
Output:
1
2
3
4
5
6
7
8
9
10
Syntax:
while(true){
//code to be executed
Example:
while(true){
Output:
Syntax:
do{
//code to be executed
}while(condition);
Example:
int i=1;
do{
System.out.println(i);
i++;
}while(i<=10);
Output:
1
2
3
4
5
6
7
8
9
10
Syntax:
do{
//code to be executed
}while(true);
Example:
do{
}while(true);
Output:
We can use Java break statement in all types of loops such as for loop, while
loop and do-while loop.
Syntax:
jump-statement;
break;
for(int i=1;i<=10;i++){
if(i==5){
break;
System.out.println(i);
}
}
Output:
1
2
3
4
Example:
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break;
System.out.println(i+" "+j);
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
//while loop
int i=1;
while(i<=10){
if(i==5){
i++;
System.out.println(i);
i++;
Output:
1
2
3
4
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
i++;
System.out.println(i);
i++;
}while(i<=10);
Output:
1
2
3
4
The Java continue statement is used to continue the loop. It continues the
current fow of the program and skips the remaining code at the specifed
condition. In case of an inner loop, it continues the inner loop only.
We can use Java continue statement in all types of loops such as for loop,
while loop and do-while loop.
Syntax:
1. jump-statement;
2. continue;
//for loop
for(int i=1;i<=10;i++){
if(i==5){
System.out.println(i);
Output:
1
2
3
4
6
7
8
9
10
Java Continue Statement in while loop
Example:
//while loop
int i=1;
while(i<=10){
if(i==5){
i++;
System.out.println(i);
i++;
Output:
1
2
3
4
6
7
8
9
10
//declaring variable
int i=1;
//do-while loop
do{
if(i==5){
i++;
System.out.println(i);
i++;
}while(i<=10);
Output:
1
2
3
4
6
7
8
9
10