🚀 Day 5: My Java Learning Journey 🚀

🚀 Day 5: My Java Learning Journey 🚀


Today, I learned about looping statements, their various types, and the use of break and continue in Java. 📚🔄

These concepts are fundamental for controlling loops and optimizing code execution. 💡 Excited to keep building my Java skills!

Looping Statement: -

  • In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop.

In Java, there are three types of loops:

  • do while loop
  • while loop
  • for Loop

Do-while loop:

  • do-while loop is similar to while loop with only difference that it checks for condition after executing the statements.

Syntax:

do {   // code to executed
         //Update statement
 } while (condition);         

condition: it is an expression which is tested. if the condition is true, the loop body is executed and control goes to update expression. As soon as the condition becomes false, loop breaks automatically.

EG: i<=10

update Expression: Every time the loop body is executed, the this expression increments or decrements loop variable.

EG: i++

Example program for Do-while Loop:

Article content

While loop: -

  • A while loop is a control flow statement that allows code to be executed repeatedly based on a give boolean condition. the while loop can be thought of as a repeating if statement.

Syntax:

while (condition) {
  // code block to be executed
      update-expression  
 }         

Test Expression: In this expression, we have to test the condition. if the condition evaluates to true then we will execute the body of the loop and go to update expression. otherwise, we will exit from while loop.

Example: i<=10

Update Expression: After executing the loop body , this expression increments/decrements the loop variable by some value.

Example: i++;

Example program for While loop: -

Article content


For loop: -

  • For Loop Provides a concise way of writing the Loop structure. Unlike a while loop, a for statement consumes the initialization, condition and increment/decrement in one line thereby providing a shorter, easy to debug structure of looping.

Syntax:

for (initialization;  condition; increment/decrement) 
{ 
  // statement or  code block to be executed 
}        

Initialization: The initialization initializes and/or declares variables and executes only once.

condition: The condition is evaluated. if the condition is true, the body of the for loop is executed.

Increment/decrement: It increments or decrements the variable value. it is an optional condition.

Statement: The statement of the loop is executed each time until the second condition is false.

Example Program for For Loop:

Article content

Nested for Loop:

  • A Nested loop means a loop statement inside another loop statement. That is why nested loops are also called “loop inside loop“.

Syntax:

Nested For loop 

for ( initialization; condition; increment )
 {
      for ( initialization; condition; increment )
 { 
        // statement of inside loop 
}
// statement of outer loop 
}        


Example Program for Nested Loop: -

Article content

For-Each Loop:

A for-each loop is a loop that can only be used on a collection of items. It will loop through the collection and each time through the loop it will use the next item from the collection. It starts with the first item in the array (the one at index 0) and continues through in order to the last item in the array.

Syntax:

for (type variableName : arrayName) { // code block to be executed }

Example Program for For-Each Loop: -

Article content

Break Statement: -

  • Break Statement is a loop control statement that is used to terminate the loop. As soon as the break statement is encountered from within a loop, the loop iterations stop there, and control returns from the loop immediately to the first statement after the loop. 

Syntax: 

break;

Example Program for Break Statement: -

Article content

Continue statement: -

  • The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.
  • We can use Java continue statement in all types of loops such as for loop, while loop and do-while loop.

Syntax:

continue;

Example Program for Continue Statement: -

Article content

As you can see in the above output, 3 is not printed. It is because the loop is continued when it reaches to 3.



To view or add a comment, sign in

More articles by Sakthi Kumar M

Others also viewed

Explore content categories