Day 16:

Looping Statements

Looping statement are the execution of the block of code repeatedly until we break it.

for Loop

Executes a block of code a specific number of times until the condition is true or a n number of times with the initialization, condition and increment/decrement.

Syntax:

  • Initialization condition: we need to initialize the variable to be used in the loop, It marks the start of a for loop. An already declared variable can be used or a variable can be declared new here.
  • Condition: It is used to define the exit condition for a loop. It must return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements.
  • Statement execution: Once the condition is evaluated to true, the statements in the loop body are executed.
  • Increment/ Decrement: It is used for updating the variable for next iteration.
  • Loop termination: When the condition becomes false, the loop terminates marking the end of its life cycle.

Example:


While Loop

Executes a block of code as long as the condition is true.

A while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition.

  • An entry-controlled loop is a loop construct where the condition is checked before the loop body is executed. In a while loop, the condition is checked before the loop body executes, and if it is false, the loop body does not run.?

Example:


Do while

Executes the loop body at least once before checking the condition. Do while loop is similar to while loop with only difference that it checks for condition after executing the statements, and therefore is an example of Exit Control Loop.?

Example:


For Each Loop/Enhanced For loop:

To iterate over the arrays or collections we use this For each loop. As name says it just loop arrays as single element.

Syntax:



Example:


For-each loop iterates over the array “


Let's run through the Jump statements tomorrow. Happy Learning




要查看或添加评论,请登录

Steffi Graph Preethi S的更多文章

  • Arrays

    Arrays

    Been so long since I posted, Happy that i'm able to be back with basic concepts again. Let's see what is Arrays: As we…

  • Day 19: StringBuffer and StringBuilder in Java:

    Day 19: StringBuffer and StringBuilder in Java:

    Mutable String: A String that can be modified or changed is known as mutable String. and are classes in Java used for…

  • Day 18: Strings:

    Day 18: Strings:

    String is a class in the java.lang package and is one of the most commonly used classes.

  • Day 17:

    Day 17:

    Static Keyword: Static is a keyword in Java. The keyword is used to indicate that a particular member (method…

  • Day 16: Jump Statements

    Day 16: Jump Statements

    Jump Statements These statements alter the normal flow of control by transferring execution. 1.

  • Day 15:

    Day 15:

    Java Control Statements: Java compiler executes the code from Top to Bottom. However Java provides the statement to…

  • Day 14: Encapsulation

    Day 14: Encapsulation

    Day 14: Encapsulation: Encapsulation in Java is integrating the data (fields) and the methods that operate on the data…

  • DAY 13 Polymorphism:

    DAY 13 Polymorphism:

    What is Polymorphism? In simple terms, polymorphism means "many forms," enabling a single method to work in different…

  • Abstract methods and abstract classes

    Abstract methods and abstract classes

    Day 12: What is Abstraction? Hiding the Implementation details. Interface allows 100% Abstraction, whereas Abstraction…

  • DAY11 Multiple Inheritance and Interfaces:

    DAY11 Multiple Inheritance and Interfaces:

    4. Multiple Inheritance Java does not support multiple inheritance with classes to avoid ambiguity caused by the…

社区洞察

其他会员也浏览了