Java Looping Statements: For, While, Do-While, and Enhanced For Loop

Day 18 : looping statement A statements are used to execute some set of instructions repeatedly is called a looping statement. we can iterate loop desired number of time based on requirement. In java 4 types of loops 1.for loop 2.while loop 3. do-while loop 4.for each loop or enhance for loop loops has 3 part ie. initialization, condition , updation. for each loop doesn't contain this parts. for loop: A statements are used to execute some set of instructions repeatedly. used when we know the number of iterations. In this loop first we initialize a variable then give a condition and the updation statement if we doens't specify the condition by default jvm adds true then loop goes infinite. Syntax. for(initialization; Condition; updation){ // Statement } ex. for(int i=1;i<=10;i++){ s.o.p(i); } while loop: used to execute some set of instructions repeatedly. We use while loop when we don’t know the number of iterations Initialization, condition and updation are not declared at the same line. It throws a compile time error if we are not specifying the condition. syntax: initialization; while(condition){ // statements; updation; } ex. int a =1; while(a<=10){ s.o.p("hello "+a); a++; } #corejava #java #loop #forloop #whileloop

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories