Loop Control Statements in Java Programming Language

Loop Control Statements in Java Programming Language

Loop Control Statements in Java are used to execute a particular code several.Statements are executed sequentially, and once the condition tests false,the loop is exited.

Types of Loop Control Statements in Java:

There are three types of Loop Control Statements in java.

  • 1.Java for loop
  • 2.Java for each loop also know as enhanced for loop.
  • 3.Labeled for loop

Java for Loop 

Java for loop is just like C++ for loop if you're familiar with the language

Syntax 


 for(variable initialization;condition to be tested;increment or decrement the variable)  
 {  
 //if condition is true,execute this code  
 }  


Example:



public class JavaForLoop {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        for(int marks=0;marks<=10;marks++)
        {
        
        System.out.println("Mark  is "+marks);
        }
        
  

Output


 Mark is 0  
 Mark is 1  
 Mark is 2  
 Mark is 3  
 Mark is 4  
 Mark is 5  
 Mark is 6  
 Mark is 7  
 Mark is 8  
 Mark is 9  
 Mark is 10  
 BUILD SUCCESSFUL (total time: 0 seconds)  


Java for each loop

For each loop in java is used to iterate through an array or collections likes lists etc:Arrays elements are used instead of index.

Syntax:


 for(certain variable:array/collection)  
 {  
 //execute this code block  
 }  

Example:


 
public class ForeachLoop {
    
    public static void main(String args[])
    {
    int marks[]={22,11,33,44,55,66,77,88,76,23,12,16};//initialize array
     for(int a:marks)
     {
         System.out.println("Mark  is "+a);
     }
    
    }
    
}


Output:


 Mark is 22  
 Mark is 11  
 Mark is 33  
 Mark is 44  
 Mark is 55  
 Mark is 66  
 Mark is 77  
 Mark is 88  
 Mark is 76  
 Mark is 23  
 Mark is 12  
 Mark is 16  

Labeled for Loop

Labeled for loop uses break and continue keywords to get out of the inner loop:

Syntax:

 labeledname;   
 for(initialize variable;test condition;increment or decrement variable){   
 // executed this code if true   
 }   

Example:


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


Output:


 1 1  
 1 2  
 1 3  
 2 1  
 BUILD SUCCESSFUL (total time: 0 seconds)  

if the above code we used break dd, execution of inner loop would stop but continue with the outer loop.

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


Output:


 run:  
 1 1  
 1 2  
 1 3  
 2 1  
 3 1  
 3 2  
 3 3  
 BUILD SUCCESSFUL (total time: 0 seconds)  

Infinitive For Loop

To create an infinitive for loop,we use two semicolons;;

Syntax:


 for(;;){   
 //execution code    
 }   

 
public class InfinitiveLoop {

   

    public static void main(String args[])

    {

    for(;;)

    {

    System.out.println("Infinitive loop Example");

    }

       

    }   

}

Output:


 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop Example  
 Infinitive loop ExampleBUILD STOPPED (total time: 2 seconds)  

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Loop Control Statements in Java Programming Language
Loop Control Statements in Java Programming Language
Loop Control Statements in Java Programming Language
Java Codes
http://www.download-all.net/2017/01/loop-control-statements-in-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/01/loop-control-statements-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy