MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java While Loops


  • In programming languages, loops are used to execute a particular statement/set of instructions again and again.

  • The execution of the loop starts when some conditions become true.

  • For example, print 1 to 1000, print multiplication table of 7, etc.

  • Loops make it easy for us to tell the computer that a given set of instructions need to be executed repeatedly.

  • Types of Loops :


    Primarily, there are three types of loops in Java:

    1.While loop
    2.do-while loop
    3.for loop
    Let's look into these, one by one.

    While loops :


    The while loop in Java is used when we need to execute a block of code again and again based on a given boolean condition.
    Use a while loop if the exact number of iterations is not known.

    If the condition never becomes false, the while loop keeps getting executed. Such a loop is known as an infinite loop.
    /*
    while (Boolean condition)
    
    {
    
                // Statements    -> This keeps executing as long as the condition is true.
    
    }
    */

    Example :


    int i=10;  
    while(i>0){  
    System.out.println(i);  
    i--;  
    }  

    Output

    10
    9
    8
    7
    6
    5
    4
    3
    2
    1

    Flow control of while loop :



    Example


    package com.company;
    
    public class cwh_21_ch5_loops {
        public static void main(String[] args) {
            System.out.println(1);
            System.out.println(2);
            System.out.println(3);
    
            System.out.println("Using Loops:");
            int i = 100;
            while(i<=200){
                System.out.println(i);
                i++;
            }
            System.out.println("Finish Running While Loop!");
    
    //        while(true){
    //            System.out.println("I am an infinite while loop!");
    //        }
        }
    }
    

    Output

    1
    2
    3
    Using Loops:
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    Finish Running While Loop!


    Conclusion

    In this page (written and validated by ) you learned about Java While Loops . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java do while loop.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.