Friday, November 17, 2017

Java For Loop | Simple For Loop | Java For-Each Loop | Labeled For Loop with Example

Java For Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated.

How for loop works?

1. The in statement articulation is executed just once. 
2. At that point, the test articulation is assessed. Here, test articulation is a boolean articulation. 
3. On the off chance that the test articulation is assessed to genuine, 
  • Codes inside the assemblage of for circle is executed. 
  • At that point, the refresh articulation is executed. 
  • Once more, the test articulation is assessed. 
  • On the off chance that the test articulation is valid, codes inside the group of for circle is executed and refresh articulation is executed. 
  • This procedure goes ahead to the point that the test articulation is assessed to false. 

4. In the event that the test articulation is assessed to false, for circle ends.

Java for Loop, Simple For Loop, Java for Each, Labeled For Loop, java for loop example etc.

Types of for loop in Java:
         1. Simple For Loop
         2. Java For-Each 
         3. Labeled For Loop

1. Simple For Loop with Example

The basis for the circle is same as C/C++. We can instate variable, check condition and augmentation/decrements esteem.

Syntax:
for(initialization;condition;incr/decr)
{  
//code to be executed  
}  
public class my
{  
public static void main(String[] args)
{  
                              for(int i=1;i<=5;i++)
{  
                              System.out.println(i);  
                              }  
 }  
}  

Output
1
2
3
4
5

2. Java For-Each Loop with Example

The for-each circle is utilized to navigate cluster or gathering in java. It is less demanding to use than basic for circle since we don't have to increase esteem and utilize subscript documentation.

Syntax:

for(Type var:array)
{  
//code to be executed  
}  
public class my
{  
public static void main(String[] args)
{  
                  int arr[]={1,2,3,4,5};  
                  for(int i:arr)
{  
                              System.out.println(i);  
                  }  
 }  
}  
Output
1
2
3
4
5

3. Java Labeled For Loop with Example  

We can have the name of each for the circle. To do as such, we utilize name before the for the circle. It is helpful on the off chance that we have settled for the circle with the goal that we can break/proceed in particular for the circle.

Syntax:
label name:  
for(initialization;condition;incr/decr)
{  
//code to be executed  
}  

Example:
public class my
{  
public static void main(String[] args)
{  
                  d:  
                  for(int i=1;i<=3;i++)
{  
                              a:  
                              for(int j=1;j<=3;j++)
{  
                                           if(i==2&&j==2)
            {  
                                               break d;  
                                          }             
                              System.out.println(i+" "+j);  
                              }  
                  }  
}  
}  
Output
11
12
13
21

In the event that you utilize break a , it will break inward circle just which is the default conduct of any circle.

public class LabeledForExample 
{  
public static void main(String[] args)
{  
            d:  
for(int i=1;i<=3;i++)
{  
a:  
for(int j=1;j<=3;j++)
{  
if(i==2&&j==2)
{  
break a;  
}  
System.out.println(i+" "+j);  
}  
}  
}  
}  

Output
11
12
13
21
31
32
33


1 comment:

  1. Awesome!! You got the best questions and answers for java interview. You're doing a great job.

    Thanks for sharing.
    http://www.flowerbrackets.com/java-for-loop-example/

    ReplyDelete

Top 200+ Core Java Interview Questions And Answers For 2019

Most Popular Java Interview Questions Given below is a comprehensive list of most important and commonly asked basic and advanced Java p...